I am new to Arduino and processing so sorry for any mistakes. I have a problem with sending multiple data to processing from Arduino. I can send 1 data easily ut I can't send more than 2 data can somebody help?!??. This is my Arduino code:
#include <DHT.h>
#define Type DHT11
int dhtpin =2;
float temopValC;
float temopValF;
DHT HT(dhtpin,Type);
#include <Servo.h>
int servopin=9;
int servopos;
Servo servo1;
int trigpin=10;
int ecopin=11;
float distance;
float spedd=343;
float timee;
void setup() {
Serial.begin(9600);
HT.begin();
delay(200);
pinMode(trigpin,OUTPUT);
pinMode(ecopin,INPUT);
servo1.attach(servopin);
}
void loop() {
temopValC=HT.readTemperature();
temopValF=HT.readTemperature(true);
spedd =331.3 +(0.6 *temopValC);
//Serial.println(spedd);
/*Serial.print("Temp C/F "); //temp val
Serial.print(temopValC);
Serial.print(" ");
Serial.println(temopValF);
delay (1005);*/
for(servopos=0;servopos < 180; servopos +=1 ){
digitalWrite(trigpin,LOW);
delayMicroseconds(2000);
digitalWrite(trigpin,HIGH);
delayMicroseconds(15);
digitalWrite(trigpin,LOW);
timee=pulseIn(ecopin,HIGH);
timee= timee /1000000.;
timee= timee /3600.;
distance= spedd * timee;
distance = distance /2;
distance = distance * 63360;//inch
distance =distance *2.54; //cm
distance= distance *2;
//Serial.print(distance);
//Serial.print(" ");
// delay(200);
servo1.write(servopos);
//delay(100);
// Serial.print(servopos);
//Serial.println(" ");
delay(100);
}
for(servopos=180;servopos >= 1; servopos -=1){
digitalWrite(trigpin,LOW);
delayMicroseconds(2000);
digitalWrite(trigpin,HIGH);
delayMicroseconds(15);
digitalWrite(trigpin,LOW);
timee=pulseIn(ecopin,HIGH);
timee= timee /1000000.;
timee= timee /3600.;
distance= spedd * timee;
distance = distance /2;
distance = distance * 63360;//inch
distance =distance *2.54; //cm
distance= distance *2;
// Serial.print(distance);
// Serial.print(" ");
//delay(200);
servo1.write(servopos);
//delay(100);
// Serial.print(servopos);
//Serial.println(" ");
// delay(100);
}
/*if(distance < 14.28){
Serial.println("..............o");
delay(800);
}
if(distance> 14.28){
Serial.println(" h ");
delay(800);
}
if(distance== 14.28){
Serial.println("i ");
delay(800);
}
*/
}
And this is my processing code for one data:
import processing.serial.*;
Serial mySerial;
String mystring= null;
int nl=10;
float myVal;
void setup(){
size(800, 600);
String myport= Serial.list() [2];
mySerial=new Serial(this,myport,9600);
}
void draw(){
while(mySerial.available() >0){
mystring =mySerial.readStringUntil(nl);
if(mystring != null){
background(0);
myVal=float(mystring);
println(myVal);
}
}
ellipse(250,myVal,10,10);
rect(0,0,20,10);
rect(0,40,20,10);
rect(0,80,20,10);
rect(0,120,20,10);
rect(0,160,20,10);
rect(0,200,20,10);
rect(0,240,20,10);
rect(0,280,20,10);
rect(0,320,20,10);
rect(0,360,20,10);
rect(0,400,20,10);
rect(0,440,20,10);
rect(0,480,20,10);
rect(0,520,20,10);
rect(0,560,20,10);
rect(0,600,20,10);
}
I want to change the x value of the ellipse with the second data fromthe Arduino.
Serial.println()
, a "carriage return character" then a "newline character" are sent at the end of the data. Processing’sfloat()
function will returnNaN
if it does not receive a valid number. I’m not 100% sure, but perhaps the "carriage return character" is causing thefloat()
function to returnNaN
. processing.org/reference/floatconvert_.html