A
Arduino3mo ago
minkus

Inconsistent LED brightness

I have three LEDs which are each operated by an individual button and two sets of code, one that keeps LEDs on after being pressed and another that keeps LEDs on only if the button is held. Set up is the same in both pictures but the 2nd and 3rd LEDs are much dimmer in the "left on" version. I've been searching all over and haven't found anything on this issue. Anyone have any ideas on how to resolve this?
No description
No description
11 Replies
MaderDash
MaderDash3mo ago
@minkus let's see your code please
minkus
minkusOP3mo ago
#define LED_PIN3 8 #define LED_PIN2 10 #define LED_PIN 12 #define BUTTON_PIN 7 #define BUTTON_PIN2 5 #define BUTTON_PIN3 3 byte lastButtonState1 = LOW; byte lastButtonState2 = LOW; byte lastButtonState3 = LOW; byte ledState1 = LOW; byte ledState2 = LOW; byte ledState3 = LOW; unsigned long debounceDuration = 50; // millis unsigned long lastTimeButtonStateChanged1 = 0; unsigned long lastTimeButtonStateChanged2 = 0; unsigned long lastTimeButtonStateChanged3 = 0; void setup() { pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT); pinMode(BUTTON_PIN2, INPUT); pinMode(BUTTON_PIN3, INPUT); } void loop() { if (millis() - lastTimeButtonStateChanged1 > debounceDuration) { byte buttonState1 = digitalRead(BUTTON_PIN); if (buttonState1 != lastButtonState1) { lastTimeButtonStateChanged1 = millis(); lastButtonState1 = buttonState1; if (buttonState1 == LOW) { ledState1 = (ledState1 == HIGH) ? LOW: HIGH; digitalWrite(LED_PIN, ledState1); } } } if (millis() - lastTimeButtonStateChanged2 > debounceDuration) { byte buttonState2 = digitalRead(BUTTON_PIN2); if (buttonState2 != lastButtonState2) { lastTimeButtonStateChanged2 = millis(); lastButtonState2 = buttonState2; if (buttonState2 == LOW) { ledState2 = (ledState2 == HIGH) ? LOW: HIGH; digitalWrite(LED_PIN2, ledState2); } } } if (millis() - lastTimeButtonStateChanged3 > debounceDuration) { byte buttonState3 = digitalRead(BUTTON_PIN3); if (buttonState3 != lastButtonState3) { lastTimeButtonStateChanged3 = millis(); lastButtonState3 = buttonState3; if (buttonState3 == LOW) { ledState3 = (ledState3 == HIGH) ? LOW: HIGH; digitalWrite(LED_PIN3, ledState3); } } } }
MaderDash
MaderDash3mo ago
@minkus ummm @minkus did you write the code? Or Ai helped? Hello?@minkus
minkus
minkusOP3mo ago
I found a video online that was for a single button, modified it myself for the additional two, and used ai to clean it up
MaderDash
MaderDash3mo ago
Ok well the issue is the ai. @minkus Do you see your void setup?
minkus
minkusOP3mo ago
ahh, missing the additional LEDs?
MaderDash
MaderDash3mo ago
Yup set them all to output see if that helps All the led pins to output of course.
minkus
minkusOP3mo ago
It's fixed, thank you! I saw that the other two were lit up and figured it was an issue with my circuit.
AnonEngineering
AnonEngineering3mo ago
iirc the pins default to INPUT, then writing them high sets them to INPUT_PULLUP, so then there would be a 20K current limiter
minkus
minkusOP3mo ago
Ahhh I see, good to know, thank you!

Did you find this page helpful?