Arduino - Processing: How to change incoming data from arduino?

Hello, I am working on a project which will display an Ultrasonic Sensor’s distance, and also a Photo resistor’s value on processing. I am using an Arduino UNO 3, and Processing 3.4 on a Windows 7 PC. Used Arduino for 2 weeks only. Used Processing for about 1 year.


Arduino Code:


// Ultrasonic Display and Photoresistor

int incomingByte = 0;

#define led A0
#define buzz A1
#define res A2
#define echo A5
#define trig A4
long duration;
int distance;
int pRes;


void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT); // Declare the LED as an output
  pinMode(buzz, OUTPUT); // Declare the LED as an output
  pinMode(res, INPUT);
  pinMode(trig, OUTPUT); // Sets the trigPin as an Output
  pinMode(echo, INPUT); // Sets the echoPin as an Input
}

void  loop() {

  // Photoresistor and LED
  pRes = analogRead(res);
  if (pRes <= 20) { // if it's dark
    digitalWrite(led, HIGH); // Turn the LED on
  } else {
    digitalWrite(led, LOW); // Turn the LED on
  }


  // Ultrasonic Sensor
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;

  // Ultrasonic buzzer if statement

  if (distance < 15) { // if object is within 15 centimeters
    tone(buzz, 50); // buzzer
    delay(50);
  } else {
    noTone(buzz);
  }
 

  // Processing data display
  
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 2) {
      Serial.println(distance);
      delay(500);
    } else if (incomingByte == 3) {
      Serial.println(pRes);
      delay(500);
    }
  }
}

Processing Code:


// Processing output for Arduino Ultrasonic and Photoresistor


color backButton = color(252, 108, 133); // Used to change colors for back button
color colorChangeUltrasonic = color(252, 108, 133); // Used to change colors for Ultrasonic Option
color colorChangePhotoresistor = color(252, 108, 133); // Used to change colors for Photoresistor Option
int screenStatus; // used to define screen state

import processing.serial.*;
Serial port;
String val;

void setup() {
  size(1100, 800); // Sets the size to 1100-by-800 pixels.
  screenStatus = 1;
  String portName = Serial.list()[1]; // Matches with my Arduino 
  port = new Serial(this, portName, 9600); // baud rate and port
}

void draw() {

  if (screenStatus == 1) {
    controlPanel();
  } else if (screenStatus == 2) {
    doorSensor();
  } else if (screenStatus == 3) {
    nightLight();;
  }
  if (port.available() > 0) {
    val = port.readStringUntil('\n');
  }
  println(val);
}
void controlPanel() {
  port.clear();
  background(3, 190, 111);
  fill(colorChangeUltrasonic); 
  rect(150, 50, 500, 300); 
  fill(colorChangePhotoresistor);
  rect(150, 400, 500, 300); 
  fill(0);
  textSize(50);
  text("Door Sensor", 250, 225);
  text("Night Light", 250, 565);
  if (screenStatus == 1 && mouseX >=150 && mouseX<=650 && mouseY >=50 && mouseY <= 350) { 
    colorChangeUltrasonic = color(255, 0, 0);
    if (mousePressed) {
      screenStatus = 2;
      doorSensor();
    } else {
      colorChangeUltrasonic = color(252, 108, 133);
    }
  }
  if (screenStatus == 1 && mouseX >=150 && mouseX<=650 && mouseY >=400 && mouseY <= 700) { 
    colorChangePhotoresistor = color(255, 0, 0);
    if (mousePressed) {
      screenStatus = 3;
      nightLight();
    } else {
      colorChangePhotoresistor = color(252, 108, 133);
    }
  }
}

void doorSensor() {
  background(173, 216, 230);
  if (screenStatus == 2) {                         
    port.write(2);        
    if (val !=null) {
      fill(0);
      textSize(80);
      text("Door Sensor and Buzzer", 90, 100);
      textSize(50);
      text("Distance in cm from object: "+ val, 125, 300);
      text("Buzzer's Status: "+ val, 125, 400);
    }
    fill(backButton);
    rect(0, 700, 200, 100);
    fill(255, 255, 0);
    text("<<Back", 0, 767);
    if (mousePressed && mouseX >= 0 && mouseX <= 200 && mouseY >= 700 && mouseY <= 800) {
      screenStatus = 1;
    }
  } else { 
    port.write(3);
  }
}

void nightLight() {
  background(173, 216, 230);
  if (screenStatus == 3) {
    port.write(3);
    if (val !=null) {
      fill(0);
      textSize(80);
      text("Photoresistor and LED", 110, 100);
      textSize(50);
      text("Photoresistor's reading: "+ val, 205, 300);
      text("LED's Status: "+ val, 205, 400);
    }
    fill(backButton);
    rect(0, 700, 200, 100);
    fill(255, 255, 0);
    text("<<Back", 0, 767);
    if (mousePressed && mouseX >= 0 && mouseX <= 200 && mouseY >= 700 && mouseY <= 800) {
      screenStatus = 1;
    }
  } else { 
    port.write(2);
  }
}

So, what I wish to do is that whenever I click either screen, Door Sensor / Night Light, it displays the correct value from the arduino. If I click Door Sensor, it correctly displays the information coming from the ultrasonic sensor, however, when I go back and click Night Light, it still displays the information coming from the sensor instead of the photoresistor. Is there a way to change it so that the photoresistor’s value comes when I click night light, and the ultrasonic’s value when I click door sensor? I am relatively new to both processing and arduino, any help is appreciated!

i modified your processing code

// Processing output for Arduino Ultrasonic and Photoresistor


color backButton = color(252, 108, 133); // Used to change colors for back button
color colorChangeUltrasonic = color(252, 108, 133); // Used to change colors for Ultrasonic Option
color colorChangePhotoresistor = color(252, 108, 133); // Used to change colors for Photoresistor Option
int screenStatus; // used to define screen state

import processing.serial.*;
Serial port;
String val;

void setup() {
  size(1100, 800); // Sets the size to 1100-by-800 pixels.
  screenStatus = 1;
  String portName = "COM6"; // Matches with my Arduino
  port = new Serial(this, portName, 9600); // baud rate and port
}

void draw() {

  if (screenStatus == 1) {
    controlPanel();
  } else if (screenStatus == 2) {
    doorSensor();
  } else if (screenStatus == 3) {
    nightLight();
  } else {
  }
  // check();
}


int m;

void check() {
  if (port.available() > 0) {
    val = port.readStringUntil('\n');
    println(val);
    m++;
    println(m);
  }
}

void controlPanel() {
  port.clear();
  background(3, 190, 111);
  fill(colorChangeUltrasonic);
  rect(150, 50, 500, 300);
  fill(colorChangePhotoresistor);
  rect(150, 400, 500, 300);
  fill(0);
  textSize(50);
  text("Door Sensor", 250, 225);
  text("Night Light", 250, 565);
  if (screenStatus == 1 && mouseX >=150 && mouseX<=650 && mouseY >=50 && mouseY <= 350) {
    colorChangeUltrasonic = color(255, 0, 0);
    if (mousePressed) {
      screenStatus = 2;
      doorSensor();
    } else {
      colorChangeUltrasonic = color(252, 108, 133);
    }
  }
  if (screenStatus == 1 && mouseX >=150 && mouseX<=650 && mouseY >=400 && mouseY <= 700) {
    colorChangePhotoresistor = color(255, 0, 0);
    if (mousePressed) {
      screenStatus = 3;
      nightLight();
    } else {
      colorChangePhotoresistor = color(252, 108, 133);
    }
  }
}


int kee;

void sending(int mm) {
  if ( kee != mm ) {
    kee = mm;
    port.write(mm);
  }
}

void doorSensor() {
  background(173, 216, 230);
  if (screenStatus == 2) {                        
    sending(2);
    delay(500);
    check();
    if (val !=null) {
      fill(0);
      textSize(80);
      text("Door Sensor and Buzzer", 90, 100);
      textSize(50);
      text("Distance in cm from object: "+ val, 125, 300);
      text("Buzzer's Status: "+ val, 125, 400);
    }
    fill(backButton);
    rect(0, 700, 200, 100);
    fill(255, 255, 0);
    text("<<Back", 0, 767);
    if (mousePressed && mouseX >= 0 && mouseX <= 200 && mouseY >= 700 && mouseY <= 800) {
      screenStatus = 1;
    }
  } else {
    port.write(3);
  }
}

void nightLight() {
  background(173, 216, 230);
  if (screenStatus == 3) {
    sending(3);
    delay(500);
    check();
    if (val !=null) {
      fill(0);
      textSize(80);
      text("Photoresistor and LED", 110, 100);
      textSize(50);
      text("Photoresistor's reading: "+ val, 205, 300);
      text("LED's Status: "+ val, 205, 400);
    }
    fill(backButton);
    rect(0, 700, 200, 100);
    fill(255, 255, 0);
    text("<<Back", 0, 767);
    if (mousePressed && mouseX >= 0 && mouseX <= 200 && mouseY >= 700 && mouseY <= 800) {
      screenStatus = 1;
    }
  } else {
    port.write(2);
  }
}

it works as it should now, the trick is:

  1. to send to arduino and wait a little to read serial buffer

  2. not to send same thing to arduino repeatedly ( see the flags in the sketch for this )

  3. not to read same thing coming from arduino over and over

and last, you should not use blocking functions ( like delay() or delayMicrosec() ) on arduino side ( especially in a loop() )

Hello KASSIMSAMJI, I have tested the code. Having the delay in processing makes it take forever, so I took the liberty to remove it. Aside from that, it works great, except for the fact that it doesn’t update. When I enter either screen, the numbers stay static, and they don’t change even if i go back and click it again. It only changes when the incoming data from arduino is from a different source.

For instance,

Door Sensor ----> Control Panel -----> Door Sensor =does not change the value

Door Sensor —> Control Panel —> Night Light —> Control Panel —> Door Sensor = changes the value.

Is there any way to fix the value so it constantly updates?

In addition to this, is there a way to make sure that it updates correctly – as in if I do door sensor → control panel → door sensor the value changes?

Thanks for the code and your help,

  • Ryken