A
Arduinoβ€’6d ago
icyiscool

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
icyiscool
icyiscoolOPβ€’6d ago
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); }
Tuuli 🎹🎸🀘
If youre looking for help with troubleshooting this code you need to describe the problem that’s occurring vs what you expect to happen.
icyiscool
icyiscoolOPβ€’6d ago
buttion isnt doing anything need new code how to use buttion?
Tuuli 🎹🎸🀘
Check adafruits button tutorials.
icyiscool
icyiscoolOPβ€’6d ago
can you send it to me?
icyiscool
icyiscoolOPβ€’6d ago
Plz im to lazy😭
Tuuli 🎹🎸🀘
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 😎
. . . AARAV . . .
. . . AARAV . . .β€’6d ago
I recommend watching Paul Worthers YT series @icyiscool
Fireguy9143
Fireguy9143β€’6d ago
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.
icyiscool
icyiscoolOPβ€’5d ago
the what a what?
. . . AARAV . . .
. . . AARAV . . .β€’5d ago
Watch the tutorial I sent you , at least the first 10.
Tuuli 🎹🎸🀘
They are lazy... dont expect too much πŸ™‚
. . . AARAV . . .
. . . AARAV . . .β€’5d ago
At least this one
No description
. . . AARAV . . .
. . . AARAV . . .β€’5d ago
True
GrayConstruct
GrayConstructβ€’3d ago
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
icyiscool
icyiscoolOPβ€’2d ago
Thank you so much

Did you find this page helpful?