0

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.

  • where in the Arduino code do you print the first value? – Juraj Jun 14 at 16:21
  • It is unclear, what values you want to send. Also in your code you never send any values over Serial. There are some Serial calls in the comments. Please show an actually working sketch (meaning working with the first value sending). Then we can look into the rest. Also, please use the autoformat function of the Arduino IDE to get the indention right. That would make your code way more readable. – chrisl Jun 14 at 18:11
  • When you use Serial.println(), a "carriage return character" then a "newline character" are sent at the end of the data. Processing’s float() function will return NaN if it does not receive a valid number. I’m not 100% sure, but perhaps the "carriage return character" is causing the float() function to return NaN. processing.org/reference/floatconvert_.html – VE7JRO Jun 14 at 19:01
0

If you mean sending multiple integers, floats, or strings, you can use a CSV format.

CSV Format: sentData = variables,variables,variable3

You can then decode it with things similar to substring on the Processing side.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.