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!