Hey I’m new here and I was having some issues with Arrays, Serial Monitoring.
I tried to make a basic Programm using the Arduino IDE.
My Goal is to have 4 States:
0 = nothing (no pin)
1 = LED 1(array 0, pin 9)
2 = LED 2(array 1, pin 10)
3 = LED 3(array 2, pin 11)
Using a Button(pin 2) to change the State.
Seems simple right?
I declared a Array of 3 and gave each the corresponding Pin.
Next I defined them further in setup() to be Outputs with a for-Loop.
Made two If Statements:
One is when the Button is Pressed.
Two is when the Button is Released.
(I used an integer to save the previous State for that)
On the Rising Edge aka when the Button is Pressed a Integer is increased by 1, if how ever the
Value if the Integer is bigger than 3 ( > 3) the Value is set to 0.
Following that I want the LED to light up.
On the Falling Edge aka when the Button is Released the LED turns off.
Now pressing the Button I get 4 States that never leave these bounds (0, 1, 2, 3, 0, 1,…)
and 3 LEDs that are supposed to light up at the corresponding State and turn off after Button release.
Running the Programm:
State is 0
Button is Pressed → State changes to 1 → LED on Pin 9 Lights up.
Button is Released → LED Pin 9 turns off.
Button is Pressed → State changes to 2 → LED on Pin 10 Lights up.
Button is Released → LED Pin 10 turns off.
Button is Pressed → State changes to 3 → LED on Pin 11 Lights up.
Button is Released → LED Pin 11 turns off.
Button is Pressed → State changes to 0.
Button is Released.
Button is Pressed.
Button is Released.
Wait why didn’t the State change?
I had all kinds of Problems so I added Serial Monitoring (Serial.begin(9600);, Serial.print(…)…)
while debugging the Code and get my Brain wrapped around Code that Loops.
Let me give you a short version (- duplicates) of the Serial Monitor out put.
Serial Monitor out put:
Now I was confused. Why does this happen.
I put different code in place thinking that the Problem was because of ledPin[state-1].
Made a function to subtract 1 from an given Integer. Somehow helped it.
Changed things around and it didn’t work again.
I removed everything from Serial Monitoring because, I don’t know.
ledPin[state-1] works again.
It works now but I can’t use Serial Monitoring or it won’t register Button Presses.
Why is that?
I’ll attach my Code maybe I did something wrong. I added some comments (they might help).
Serial Monitoring is still where It was but commented out(just as a reference).
I hope somebody knows why that is and might be able to provide a solution to that problem.
Thanks for reading
RGB_LED_swichmenu.ino (1.21 KB)