My bad, I didn’t write the logic of my code, here it is:
The Arduino first sets up and waits for any serial data to come in. Processing is supposed to read the number of cars in two lanes which are stored in the data.txt file as just two comma-separated integers in the setup(). It then parses it and sends it to Arduino through serial. Arduino gets these values and calculates the green light time for lane 1 based on number of cars in it using timer_function() and sends two comma-separated integers to processing in this format:
(is lane 1 light to be turned green? integer 1 for yes, 0 for no),(countdown duration)
and then waits to read another input from processing after that amount of delay. Processing parses the lane 1 and delay comma-separated string in serialEvent() and then updates the number of cars from the data.txt file and writes it back to Arduino for it to do the same thing all over, for turning lane 1 light red.
In draw(), I’ve shown the red and green lights using full stops, and there is a countdown timer according to the delay received, for lights to switch again.
The first read of data.txt in setup() was to start a communication with Arduino so that I can catch the reply in the serialEvent() and then send the new values from data.txt in it. Another program is updating that file.
Here is what the processing app is supposed to look like:
I’ll try the handshake protocol thing and these changes that you recommend, and reply again.
