Array Problem because if Serial Monitor?

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)

Next,
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:

Button: 0
State: 0
Button: 1
State: 1
Button: 0
State: 1
Button: 1
State: 2
Button: 0
State: 2
Button: 1
State: 3
Button: 0
State: 3
Button: 1
State: 0
Button: 0
State: 0

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

  • JekoRhino/Happyboy1604

RGB_LED_swichmenu.ino (1.21 KB)

I'll attach my Code maybe I did something wrong

Yup, you attached your code instead of posting it.

Are you confused by the serial monitor output or by the reaction of the LEDs?

The serial monitor output looks fine. State indexes each time the button is pressed and when the state is 3 and the button is pressed, the state resets to 0.

The led to light is based on ledpos() which is state -1. What value is returned by ledpos() when you reach the end of your serial monitor output and state=0? (remember arrays are 0 to x, and don't have any negative indices)

AWOL:
Yup, you attached your code instead of posting it.

Yea I just tried to tell what I was trying to do. I know that the Code might be faster but welp...
I hope it wasn't to bad? sry

adwsystems:
Are you confused by the serial monitor output or by the reaction of the LEDs?

The serial monitor output looks fine. State indexes each time the button is pressed and when the state is 3 and the button is pressed, the state resets to 0.

The led to light is based on ledpos() which is state -1. What value is returned by ledpos() when you reach the end of your serial monitor output and state=0? (remember arrays are 0 to x, and don't have any negative indices)

Yea I see what you mean...
When State is 0 it will subtract 1 anyways so I get -1.
So I would make a line of Code like:

if(State == (1 || 2 || 3) ) { digitalWrite(ledPin[State-1], HIGH);}

some thing like that.

It's still wired that It works without the Serial Code.
Maybe It has no problem with -1 and gets into a wrong state when Serial stuff is running and Bricks it self.

I'll try to do something to avoid the state.

Any other suggestions? I'am really new to this and I guess that -1 just slipped my mind.
I should have thought a little bit longer about it, step aside and think everything trough.
But I had no time. I needed to catch a train.

Thanks allot ^^

Have a great day or night, doing what you do, when or where you are.

See ya~

  • JekoRhino

Once you try to access an "undefined memory location" such as [state-1], all bets are off on what will happen next. As you have shown, everything works fine right up until the exact point when you read [state-1].

Go ahead and modify your program to keep the array index inbounds and repost your code if the problem still exists.

The hardest thing to program is the start and the end, and you just learned that.