Stoplight help
So i was makeing a stoplight and I need it so when I push my analog buttio it swices bwteen modes green, red, normal (red then yellow then green)
19 Replies
int red = 9;
int yellow = 8;
int green = 7;
int button = 10;
int mode = 0; // 0 = normal, 1 = green only, etc.
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // ms
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT_PULLUP); // safer: uses internal pull-up resistor
}
void loop() {
int reading = digitalRead(button);
// Debounce the button
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// When button is pressed (goes LOW since we use INPUT_PULLUP)
if (reading == LOW && lastButtonState == HIGH) {
mode++;
if (mode > 1) mode = 0; // only two modes for now, add more if needed
}
}
lastButtonState = reading;
// --- handle modes ---
if (mode == 0) {
show_normal();
} else if (mode == 1) {
show_green();
}
}
void show_normal() {
digitalWrite(red, HIGH);
delay(2000);
digitalWrite(red, LOW);
digitalWrite(yellow, HIGH);
delay(1000);
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(2000);
digitalWrite(green, LOW);
}
void show_green() {
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(2000);
}
If youre looking for help with troubleshooting this code you need to describe the problem thatβs occurring vs what you expect to happen.
buttion isnt doing anything need new code how to use buttion?
Check adafruits button tutorials.
can you send it to me?
No
Plz im to lazyπ
https://support.discord.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline#h_01GY0DAKGXDEHE263BCAYEGFJA also format your code please. Volunteer helpers are more lazy than you π€£
Announcing you want someone else to do your project for you here is a sure way to get no help π
I recommend watching Paul Worthers YT series @icyiscool
You can use the serial monitor to print your button state at different parts of the loop, as well as print notice that a function is going to execute.
the what a what?
Watch the tutorial I sent you , at least the first 10.
They are lazy... dont expect too much π
At least this one

True
So your problem is the long delays. You can't Caryville a button push when show_normal has 5 seconds of delays before it gets back up to checking for button.
Some options are to quote your own delay function that checks button every 10ms or so switch to the intermediate coding style of using millis instead of delay. There's a great tutorial from adafruit on multitasking arduino
Also recommend simplifying your button denouncing work a library like bounce2 or ezbutton which does similar to your code but with 2 lines of code in your sketch which would look something like:
button.update();
if(button.pressed())
mode=(mode+1) % 2; //wraps back to 0 after 1
You can look at the examples in the library for full code
Thank you so much