sending multiple Values to arduino

edited June 2017 in Arduino

Ciao, Guys.

Now I want to try Serial communication with Arduino using three kinds of sensor. There is no problem to check Values on Arduino, but I really don't know how can I read the data on Processing. I know this question is so stupid, please help me.

I using DHT sensor for Humidity and Temperature, a moisture sensor, and UV sensor. Here is my Arduino code.

#include "Adafruit_Sensor.h"
#include "DHT.h"
#include "DHT_U.h"
#include "DHT.h" 
#define DHTPIN 2 
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE); 



void setup() { 
  Serial.begin(9600); 
} 

void loop() { 
  delay(1500); 
 int h = dht.readHumidity(); //for DHT11
 int t = dht.readTemperature(); //for DHT11
 int m = analogRead(A0); //for moisture sensor
 int u = analogRead(A1);
// Serial.print("Humidity: "); 
 Serial.print(h); 
// Serial.print(" %\t"); 
// Serial.print("Temperature: "); 
 Serial.print(t); 
//  Serial.println(" C"); 
//  Serial.print("Moisture Sensor Value:");
  Serial.println(m);  
//  Serial.print("UV Value: ");
  Serial.println(u);
 
}

Answers

  • sorry, I don't know why the font is too big..

  • edited June 2017 Answer ✓

    The font is big due to markdown (check the Markdown link at the bottom f any comment box here or in any post)

    You can fix this by formatting your code properly in the forum. To do so, edit your post (gear icon on the top right corner of your post) and then highlight your code and hit ctrl+o. Ensure there is an empty line above and below your code.

    Antoher relevant post: https://forum.processing.org/two/discussion/comment/88115/#Comment_88115

    Kf

  • edited June 2017

    GoToLoop #kfrajer

    Thank you so much, guys. Following your advice, finally, I can get the data from Arduino.

    however, I have some problem with showing data on Processing windows(Java). now I can check data on Processing console, but it's only on the console. can you help me again, please?

    it's my Arduino Code.

    
    #include 
    #include 
    #include 
    #include "DHT.h" 
    #define DHTPIN 2 
    #define DHTTYPE DHT11 
    DHT dht(DHTPIN, DHTTYPE); 
    
    
    void setup() {
      Serial.begin(9600);
    }
     
    void loop() {
      const int humidity = dht.readHumidity();
      const int temperature = dht.readTemperature();
      const int moisture = analogRead(A0);  
      const int UV = analogRead(A1); 
     
      Serial.print(humidity);
      Serial.write('\t');
      Serial.print(temperature);
      Serial.write('\t');
      Serial.print(moisture);
      Serial.write('\t');
      Serial.print(UV);
      
     
      delay(500);
    }
    

    Processign Code.

    
    void setup() {
      println(Serial.list());
      surface.setResizable(true);
    
     
      myPort = new Serial(this, Serial.list()[3], 9600);
    }
    
    void draw() {
      // Expand array size to the number of bytes you expect
      byte[] inBuffer = new byte[3];
      while (myPort.available() > 0) {
        inBuffer = myPort.readBytes();
        myPort.readBytes(inBuffer);
        if (inBuffer != null) {
          String myString = new String(inBuffer);
          println(myString);
        }
      }
    
    
      textSize(20);
      text(myString, 50,50); //This part is not working
    
    }
    
    
  • Answer ✓

    You aren't using bufferUntil() + serialEvent() combo. L-)
    And at your Arduino's C code, the last sent data should be w/ println() instead of just print(). :-\"

  • edited June 2017

    GoToLoop

    Thanks, I'm a newbie on programming. Maybe it makes you little drawling. Can you explain about "befferUntil()+serialEvent() combo"?

    I wanna show that data using "int"

  • After you add println() in your ino code at the end of each package, then in Processing you do this (un-tested):

    Kf

    void draw() {
      // Expand array size to the number of bytes you expect
      byte[] inBuffer = new byte[32];
      while (myPort.available() > 0) {
        inBuffer = myPort.readBytesUntil('\n');
        if (inBuffer != null) {
    
          String[] myString = split(trim(inBuffer),'\t');
    
          //Process data only if it has four tokens
          if(myString.length==4){
            println(myString);
          } 
          else{
            println("Not valid number of tokens");
          }
        }
      } 
    
      textSize(20);  //Move to setup
      text(myString, 50,50); //This part is not working
    
    }
    
  • I changed my code on Processing, but in this case showing data is "null".... I don't know why...

    int lf = 10;      // ASCII linefeed
    
    void setup() {
      size(400,200);
      // List all the available serial ports:
      printArray(Serial.list());
      // Open the port you are using at the rate you want:
      myPort = new Serial(this, Serial.list()[3], 9600);
      myPort.bufferUntil(lf);
    }
    
    void draw() {
      background(0);
      while (myPort.available() > 0) {
      String value = myPort.readString();
      myPort.readString();
      }
      
      text("received: " + inString, 10,50);
      println(inString);
    }
    
    void serialEvent(Serial p) {
      inString = p.readString();
    }  
    
    
  • Answer ✓

    @yohan You are accessing your arduino data stream at two spots: line 15 and 24. It is better if you stick to one of the examples provided in previous posts or links that @GoToLoop shared before.

    Personally, I will handle everything in serialEvent() and do all the sketch update on draw().

    Kf

  • @kfrajer

    Finally, I can get information from Arduino to Processing, and It works. But, It has still a problem. The value on the con?ole in Processing is not steady, sometimes it is changed. At the first time it works, but after 3~4 second it was mixed. Can you help me, please?

    Arduino code

    
    #include 
    #include 
    #include 
    #include "DHT.h" 
    #define DHTPIN 2 
    #define DHTTYPE DHT11 
    DHT dht(DHTPIN, DHTTYPE); 
    
    
    void setup() {
      Serial.begin(9600);
    }
     
    void loop() {
      const int humidity = dht.readHumidity();
      const int temperature = dht.readTemperature();
      const int moisture = analogRead(A0);  
      const int UV = analogRead(A1); 
      //const int son = analogRead(A5);
     
      Serial.println(moisture);
      //Serial.write('\t');
      Serial.println(UV);
      //Serial.write('\t');
      Serial.println(temperature);
      //Serial.write('\t');
      Serial.println(humidity);
      //Serial.write('\t');
      //Serial.println(son);
     
      delay(500);
    }
    
    

    Processing code

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    
    String val;
    float number;
    int[] serialInArray = new int[4]; // Where we’ll put what we receive
    int serialCount = 0;     // A count of how many bytes we receive
    int temperature, humidity, moister, uv;
    
    void setup() {
      size(20, 20);
      String portName = Serial.list()[2];
      myPort = new Serial(this, portName, 9600);
      printArray(Serial.list());
    }
    
    void draw() {
      background(0);
      //println("ss");
      if ( myPort.available() > 0) {  // If data is available,
        val = myPort.readStringUntil('\n');         // read it and store it in val
        //println(val);
        
        if (val == null) {
          number = 0;
        } else {
          number = Float.parseFloat(val);
        }
        
        //if (val != null) {
          
          serialInArray[serialCount] = (int)number;
        
          serialCount++;
          
          if (serialCount > 3 ) {
            moister = serialInArray[0];
            uv = serialInArray[1];
            temperature = serialInArray[2];
            humidity = serialInArray[3];
            print("Moister: ");
            println(moister);
            print("UV: ");
            println(uv);
            print("Temperature: ");
            println(temperature);
            print("Humidity: ");
            println(humidity);
            println("------------");
            // Reset serialCount:
            serialCount = 0;
          }
        //}
      }
    }
    
    
    
Sign In or Register to comment.