Processing with Arduino - void serialEvent

edited May 2016 in Arduino

Hello,

I have a problem with my Processing and Arduino code. I don't know if my problem come forum my Arduino or Processing code. I have a project, I have to display the temperature (°C), the height (kg), the sound (dB), the humidity and the counting bees. The counting bees ? Yeah, this is an interface that displays the values ​​that give integrated sensors in intelligent hive. Next, my interface is finished but I have one problem. I'm using a potentiometer to do a simulation with the interface like that : simulation. As you can see, we have the temperature. But now, I want to display the height, the sound, the counting bees and the humidity. With Arduino, I have a potentiometer and I do simulation, I'm using analogRead(A0) for the temperature, analogRead(A1) for the height and others... The problem is when I'm in A0 (in my arduino card), I have the temperature and and the other values. What I want is that when I 'm on A0, I only have the temperature and not other values. And when I'm on A1, I have only the height...

PS : You can check this thread first : https://forum.processing.org/two/discussion/15328/g4p-library-processing-with-arduino-isis-proteus-and-vspe#latest

My Processing code :

// Global variable created 
void serialEvent (Serial monPort)
{
  //Récupération sur le port série de la tension sous forme de chaine de caractères
  String listeInfo = monPort.readStringUntil('\n');

  if(listeInfo == null)
    return; //Aucune données reçues

  String[] tableauInfo = listeInfo.split(";"); //Chacun des 5 premières cases contient une info

  if (tableauInfo.length != 5)
    return; //Manque des données

    /*** TEMPERATURe ***/
    String tensionCar = trim(tableauInfo[0]); // Suppression des blancs au début et à la fin de la chaine de caractère
    int tensionNum = int(tensionCar);  // Conversion de la tension codée en CHAR en valeur décimale puis calcul température
    temperature = round(((500 * tensionNum) / (3.3 * 1024)) - 50.0);

    /*** MASSE ***/
    String masse2 = trim(tableauInfo[1]);
    int tension2Num = int(masse2);
    masse = round(((500 * tension2Num) / (3.3 * 1024)) - 50.0);

    /*** HUMIDITE ***/
    String humidite = (tableauInfo[2]);

    /*** COMPTAGE ***/
    String comptage = (tableauInfo[3]);

    /*** SON ***/ 
    String son = (tableauInfo[4]);


    //Suite du code...
}

My Arduino code :

int temperature;
int masse;
int humidite;
int comptage;
int son;

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

void loop()
  {
    //Lecture des données
    temperature = analogRead(A1);
    masse = analogRead(A2);
    humidite = analogRead(A3);  
    comptage = analogRead(A4); 
    son = analogRead(A5);

    //Écriture des données
    Serial.print(temperature);
    Serial.print(";");
    Serial.print(masse);
    Serial.print(";");
    Serial.print(humidite);
    Serial.print(";");
    Serial.print(comptage);
    Serial.print(";");
    Serial.println(son);
    Serial.print(";");

    delay(1000);
  }

I have to make this project for the 17/05, I hope to help in the meantime. (Can skype for more information) Regards,

Answers

  • edited May 2019 Answer ✓

    You'd be better off if you had followed the approaches from:
    https://Forum.Processing.org/two/discussion/14988/drawing-of-graphs-from-i2c-imu#Item_3

    /**
     * Efficient Serial Multi-Value Reading (v1.1.2)
     * GoToLoop (2015-Feb-18)
     *
     * Forum.Processing.org/two/discussion/14988/
     * drawing-of-graphs-from-i2c-imu#Item_3
     *
     * Forum.Processing.org/two/discussion/16618/
     * processing-with-arduino-void-serialevent#Item_1
     *
     * Discourse.processing.org/t/
     * using-two-different-readstringuntil-characters/10769/6
     */
    
    import processing.serial.Serial;
    
    static final int PORT_INDEX = 0, BAUDS = 9600;
    
    int[] vals = {};
    //float[] vals = {};
    
    void setup() {
      noLoop();
      final String[] ports = Serial.list();
      printArray(ports);
      new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      println(vals);
    }
    
    void serialEvent(final Serial s) {
      vals = int(splitTokens(s.readString()));
      //vals = float(splitTokens(s.readString()));
      redraw = true;
    }
    

    The model above expects to receive 1 TSV (Tab-Separated Values) row for each serialEvent().

    For the Arduino side, adapting the solution from this link below:
    https://Forum.Processing.org/two/discussion/14988/drawing-of-graphs-from-i2c-imu#Item_6

    Would get something like this for your particular case: *-:)

    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
      // Lecture des données:
      const int temperature = analogRead(A1);
      const int masse = analogRead(A2);
      const int humidite = analogRead(A3);  
      const int comptage = analogRead(A4); 
      const int son = analogRead(A5);
    
      // Écriture des données:
      Serial.print(temperature);
      Serial.write('\t');
      Serial.print(masse);
      Serial.write('\t');
      Serial.print(humidite);
      Serial.write('\t');
      Serial.print(comptage);
      Serial.write('\t');
      Serial.println(son);
    
      delay(1000);
    }
    
  • That works, thanks. But I have a problem. My problem is that when I'm on A1, I have all values ​​as I only want the temperature in A1, A2 only height etc ...

  • edited May 2016 Answer ✓

    There's nothing in your examples that alludes you need to send just 1 category of data.

    Your model clearly sends this sequence: temperature, masse, humidite, comptage, son.

    And apparently, all as int datatype too. I wonder whether any of them might be float instead? :-/

    If rather you just wanna send temperature, simply go w/ this:

    void loop() {
      const int temperature = analogRead(A1);
      Serial.println(temperature);
      delay(1000);
    }
    

    If it's something more complex, you're gonna need to come up w/ some logical model for it. 8-|

  • edited May 2016

    That's what I thought, or my teacher told me that it is possible is my concern... Thank you for your help and your patience ^_^ So my program is fully functional with your help ! I will divide the code in cases where at the end of my project if someone is interested in an interface connecting a hive that is managed from multiple sensors and XBEE.

    Regards,

  • edited May 2019

    @Itachi, since only 1 value is being sent out, there's no need for any array in order to receive it.
    Instead you can use the solution model from this old forum thread: *-:)
    https://Forum.Processing.org/two/discussion/14534/myport-available-always-0#Item_1

    /**
     * Efficient Serial Reading (v1.02)
     * GoToLoop (2016-Jan-19)
     *
     * Forum.Processing.org/two/discussion/14534/
     * myport-available-always-0#Item_1
     *
     * Forum.Processing.org/two/discussion/16618/
     * processing-with-arduino-void-serialevent#Item_5
     *
     * Discourse.Processing.org/t/
     * map-function-worked-in-processing-2-and-early-3-
     * but-broke-in-3-5-3-need-guidance/11371/2
     */
    
    import processing.serial.Serial;
    
    static final int PORT_INDEX = 0, BAUDS = 9600;
    String myString = "";
    
    void setup() {
      noLoop();
      final String[] ports = Serial.list();
      printArray(ports);
      new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      println(myString);
    }
    
    void serialEvent(final Serial s) {
      myString = s.readString().trim();
      redraw = true;
    }
    
  • @GoToLoop

    Everything works fine now, it was enough to send simulated values ​​from and set on Processing for example, the first simulated value is read by Arduino and Processing temperature on the window which comes to the analogRead(A1)... My problem was because I was using 5 potentiometers, or now I currently use 3 and it works.

    Your code with the vals is good.

Sign In or Register to comment.