Working on a code to take input from an Arduino and display the data using Processing.

edited July 2017 in Arduino

I have a code written in Arduino that turns the led off and on using if statements. It displays in the serial monitor the status of the LED and its "input". It displays 'LED is ON.' 'LED is OFF.' or 'Invalid' depending on what would be the input from the user. Currently it's automated so I can leave it alone and it scrolls through on off and then invalid every two seconds so I can work on the Processing code. In Processing, I can bring in the data and display each result once but the code either gets stuck at the end of the loop and draw doesn't seem to run again or there is something wrong with my serial event and my string input from the Arduino doesn't update. I have attached the Processing code if that will help in understanding what I am trying to do.

   import processing.serial.*;
Serial port;

String serialMonitor = "LED is ON!";       //Determining empty string allows loop to run the first time
String txt = "Unknown!";
String data;
String ledOn = "LED is ON!";
String ledOff = "LED is OFF!";
String invld = "Invalid!";
PFont font;              //Activates font


void setup() {
  size(400, 400);                                          //Determines window size
  port = new Serial(this, "COM4", 9600);                   //
  port.bufferUntil('!');                                   //
  font = loadFont("AmerTypewriterITCbyBT-Medium-48.vlw");     //
  textFont(font, 48);                                     //
  frameRate(60);
}

void draw() {
  background(0);

 if (serialMonitor.equals(ledOn) == true) {
     rectMode(CENTER);
  text(serialMonitor, 200, 200, 300, 50);
  fill(234, 255, 45);                                   //When LED is on
 }

if (serialMonitor.equals(ledOff) == true) {
     rectMode(CENTER);
  text(serialMonitor,200, 200, 300, 50);
  fill(42, 20, 168);                                        //When LED is off  
 }

 if (serialMonitor.equals(invld) == true) {
     rectMode(CENTER);
  text(serialMonitor, 200, 200, 300, 50);
  fill(230, 11, 11);                                  //When input is Invalid
 }

 else {
     rectMode(CENTER);
  text(txt, 200, 200, 300, 50);
  fill(237,233,220);
 }
}

void serialEvent (Serial port) {
serialMonitor = port.readStringUntil('!');        //Reads incoming data string
}

Answers

  • Please post correctly formatted code, not screenshots. Code allows forum members to read, run, and check your work, giving you feedback.

  • Check the following code and see if it works. Notice that I have nested if statements. In your case, the if-else statement at the end will take precedence no matter the status of the first two if statements.

    Kf

    import processing.serial.*;
    
    final String ledOn = "LED is ON!";
    final String ledOff = "LED is OFF!";
    final String invld = "Invalid!";
    final String txt = "Unknown!";
    
    Serial port;
    
    String serialMonitor = txt;       //Determining empty string allows loop to run the first time
    String data;
    
    PFont font;              //Activates font
    
    
    void setup() {
      size(400, 400);                                          //Determines window size
      port = new Serial(this, "COM4", 9600);                   //
      port.bufferUntil('!');                                   //
      font = loadFont("AmerTypewriterITCbyBT-Medium-48.vlw");     //
      textFont(font, 48);                                     //
      frameRate(60);
      rectMode(CENTER);
    }
    
    void draw() {
      background(0);
    
      if (serialMonitor.equals(ledOn) == true) {
        fill(234, 255, 45);                                   //When LED is on
        text(serialMonitor, 200, 200, 300, 50);
      } else if (serialMonitor.equals(ledOff) == true) {
        fill(42, 20, 168);                                        //When LED is off
        text(serialMonitor, 200, 200, 300, 50);
      } else if (serialMonitor.equals(invld) == true) {
        fill(230, 11, 11);                                  //When input is Invalid
        text(serialMonitor, 200, 200, 300, 50);
      } else {
        fill(237, 233, 220);
        text(txt, 200, 200, 300, 50);
      }
    }
    
    void serialEvent (Serial port) {
      serialMonitor = port.readStringUntil('!');        //Reads incoming data string
    }
    
  • kfrajer I tried out your code and it only displays the Unknown result. But thank you for the suggestion, any other ideas?

  • Can you post your ino code?

    You could also monitored the incoming messages. I'd do something like:

    void serialEvent (Serial port) {
      serialMonitor = port.readStringUntil('!');        //Reads incoming data string
      println("Received "+serialMonitor+" @"+millis());
    }
    

    This will tell you at least if you are receiving the messages.

    Kf

  • This is my code that sends the information to the serial monitor. If I open the serial monitor while it is running I can see each result pop up for two seconds before the next comes up.

    int ledPin = 13;
    int val = 0;
    
    void setup() {
    Serial.begin (9600);
    pinMode(ledPin, OUTPUT);
    }
    
    void loop(){
      if (val == 1){
        digitalWrite(ledPin, HIGH);
        Serial.println("LED is ON.");
        val = 0;
        delay(2000);
       }
      else if (val == 0){
        digitalWrite(ledPin, LOW);
        Serial.println("LED is OFF.");
        val = 2;
        delay(2000);
      }
      else{
        Serial.println("Invalid.");
        val = 1;
        delay(2000);
      }
    }
    
  • In your ino you have a dot. In Processing you are using an exclamation mark for the end of the line. Is that something you should correct?

    Kf

  • Yes, I changed the exclamation points at one time to periods in order to check if they were somehow influencing the code and forgot to change them back. Now that I have changed everything to exclamation points and input the code to monitor the messages received by processing I see that processing is receiving the messages from the Arduino correctly every two seconds. However, it still gets stuck on displaying the unknown. If I start the code at certain moments it will display "LED is OFF!" and then refresh to "Unknown" and get stuck there but is still receiving new inputs.

  • I have also recently put in a line to update the background one value brighter every time the draw function loops. The background brightness does increase meaning the draw is looping through, the serialMonitor is reading the correct data meannig my variable is updating, but still, the words will not change so I'm thinking it is somewhere in my if statement but I have no idea where.

  • Sorry, not much time for testing but I suggest you add noLoop() in your setup and redraw(); in your serial event. This will execute the content of draw only when you get new data. Check more in the reference: https://processing.org/reference/redraw_.html

    Please also edit your ino post and fix char marking the end of the data package to avoid confusion for future forum goers.

    An example of redraw by @GoToLoop: https://forum.processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1

    Kf

Sign In or Register to comment.