Hi I'm new to Arduino. I'm currently going through the "30 Days Lost in Space" kit.
I hope to use Arduino to control my LEGO City. In the code below, I have the basic stoplight code. Using the same Arduino board, how do I then add code for the stoplight for the cross traffic? (when I'm at a green light, others are at a red light)
Also, How would I add flashing lights for a railroad crossing?
I would think that there is a way to have multiple loops on the same board?
Thjanks,
Veronica
int red2 = 6;
int yellow = 5;
int green2 = 4;
void setup() {
// put your setup code here, to run once:
pinMode(red2, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(red2, HIGH);
digitalWrite(green2, LOW);
digitalWrite(yellow, LOW);
delay(6000);
digitalWrite(red2, LOW);
digitalWrite(green2, HIGH);
digitalWrite(yellow, LOW);
delay(6000);
digitalWrite(red2, LOW);
digitalWrite(green2, LOW);
digitalWrite(yellow, HIGH);
delay(2000);
}
To doing that you have to get rid the delays first. It is impossible to have multiple loops in the code if any of the loops contains delay() operators.
Look to the "BlinkWithoutDelay" IDE example to see how to program arduino processes without using of delays().
The only place I ever saw the yellows connected together was Leominster, MA. Two cross streets controlled by standard lights. After cycling traffic so each lane had a green light for a time, ALL the red lights turned on and all the yellows started blinking. Then the pedestrians could cross any way they wanted. Many crossed diagonally. After their time was over, the standard cycling resumed.
I saw lights work that way in the 50's. They were mostly controlled with eagle stepper switches in our area. This was Ohio, Michigan, and Indiana I am familiar with.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for advice on (nor for problems with) your project.
Arduino have no operating system, like a pc or laptop does, to allow multiple processes to run at the same time (or apparently at the same time) by sharing a percentage of the CPU time between the different processes.
No doubt someone will comment that some Arduino compatible boards, such as those based on ESP32 chip, do have an operating system (a Real Time Operating System or RTOS) and so can do what you describe. But, since you forgot to mention it (most beginners do forget), we are all assuming you are using an Uno, and Uno does not have an RTOS.
But that does not mean that your Uno, your code, cannot run multiple tasks at the same, or appear to do so! It just takes a little more learning and understanding.
I would consider spending some time on line or with the Arduino Cookbook and learn about functions (void loops). Once you have these understood you can then call them as needed while not using the delay() function.
That website has been pointed out many times. It may be very good mostly, but this
It is important to have the loop() function in the sketch, even if it is empty, because without it the microcontroller on the Arduino board will try to execute whatever it finds next in memory after the statements in the setup() function have been executed. The microcontroller will try to execute whatever it finds in memory as an instruction, but the loop() function prevents it from doing this by keeping program execution in the loop.
was enough for me to stop reading and throw the link in the trash. I would advise anyone using that website to not rely on it alone. I don't know if it is filled with nonsense like the above or if the writer was just having a bad day or actually thinks it works that way.