I’m using processing with an Arduino UNO for my application. I’m making an adaptive traffic light system and for that, I’ve implemented a primitive graphic display in processing. In this, the processing code reads the number of cars in two lanes from a text file and send over this data to the Arduino, which then calculates the red light time for each lane and sends back the time to the processing program for displaying the timer.
Here is the processing code:
import processing.serial.*;
Serial port;
int lane1_on = 1;
float delay_light = 0;
int[] data_arr = {}; //no. of cars in lane 0 and lane 1 from text file
String data_str = "";
int index = 0;
PFont font;
int[] no_of_vehicles;
boolean flag = true;
String[] stuff = {};
void setup()
{
size(1000,1000);
font = loadFont("AgencyFB-Bold-200.vlw");
textFont(font, 200);
stuff = loadStrings("data.txt");
data_arr = int(split(stuff[0], ','));
port = new Serial(this, "COM4", 9600);
port.bufferUntil('\n');
port.write(data_arr[0]);
port.write(data_arr[1]);
}
void draw()
{
background(255,255,255);
fill(0, 0, 0);
text("lane 1", 50, 300);
fill(0, 0, 0);
text("lane 2", 600, 300);
fill(0, 0, 0);
text("timer", 325, 700);
if(lane1_on==1){
//lane1
fill(0, 0, 0);
text(".", 50, 40);
fill(0, 255, 0);
text(".", 50, 120);
//lane2
fill(255, 0, 0);
text(".", 750, 40);
fill(0, 0, 0);
text(".", 750, 120);
} else {
//lane1
fill(255, 0, 0);
text(".", 50, 40);
fill(0, 0, 0);
text(".", 50, 120);
//lane2
fill(0, 0, 0);
text(".", 750, 40);
fill(0, 255, 0);
text(".", 750, 120);
}
fill(0, 0, 0);
text(delay_light, 150, 900);
delay(1000);
if(delay_light>0){
delay_light--;
}
}
void serialEvent (Serial port)
{
if (port.available() > 0){
data_str = port.readStringUntil('\n');
println(data_str);
data_str = data_str.substring(0, data_str.length() - 1);
index = data_str.indexOf(",");
lane1_on = int(data_str.substring(0, index));
println(lane1_on);
delay_light = float(data_str.substring(index+1, data_str.length()));
stuff = loadStrings("data.txt");
data_arr = int(split(stuff[0], ','));
port.write(data_arr[0]);
port.write(data_arr[1]);
}
}
Here is the Arduino code:
int cars_in_lane1;
int cars_in_lane2;
int delays[] = {1,1}; //[lane1_delay, lane2_delay]
int lane1 = 1;
int lane2 = 2;
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0);
cars_in_lane1 = Serial.parseInt();
while (Serial.available() == 0);
cars_in_lane2 = Serial.parseInt();
delays[0] = timer_function(cars_in_lane1);
Serial.print("1,");
Serial.println(delays[0]);
delay((delays[0])*1000);
while(Serial.read() >= 0);
delays[1] = timer_function(cars_in_lane2);
Serial.print("0,");
Serial.println(delays[1]);
delay((delays[1])*1000);
}
float timer_function(int no_of_cars){
if(no_of_cars <= 15){
return 30;
} else {
return (2*(no_of_cars-15)) + 30;
}
}
With this, my timer is stuck at 0 count and doesn’t even start as it doesn’t receive back anything from the Arduino, as I’ve checked from console logging in the processing code in the SerialEvent() function.
I don’t want to place the port.write() in the draw() function as it will send the data every time draw() loops and fill up the buffer with redundant data. But, I only seem to get back any data from the arduino when I use it this way, as I have checked by console logging a message in my serialEvent() function. I even tried using a flag as in this code, inside draw() :
if (flag){
stuff = loadStrings("data.txt");
data_arr = int(split(stuff[0], ','));
port.write(data_arr[0]);
port.write(data_arr[1]);
flag = false;
}
so that the port.write() sends data only once to the Arduino. But this gives the same result of Arduino not sending anything back. It seems to not write to the port every time it is run, even inside the draw() function.
I tried running the Arduino code alone and sending it the same data through the serial monitor and it works correctly.
I think my problem may be similar to this thread - Write and read to Serial port at the same time
What can be done to solve this and why is processing behaving like this?
Btw, I’ve also posted this on StackOverflow, you can check that thread here - https://stackoverflow.com/questions/62411094/why-doesnt-the-port-write-work-when-placed-inside-the-setup-function-proce