Four mode light


I am making a light module and have used a switch case statement for each of the effects. I will be using a button switch to change through each of the modes. Since placing the button count code into the loop, the count cycles through 1,2,3.. Etc but the effects are no longer continuous..

Does anyone know how I could correct this in my code? :confused:


To start, you could post your code.

Shame you didn't use code tags.

if (Count > 4)
  Count = 0;

Have you deliberately not got a case 0 ?

      pinMode(9, LOW);
      // initialize serial communications at 9600 bps:

      digitalWrite(13, HIGH);     // turn the LED on (HIGH is the voltage level)

Interesting comment

case 4: //Ignore the button for 4 seconds

How is the button wired ?

I do not think that the hardware is the issue, but here anyway. Each function works on its own, it’s just adding the button count into the loop section that messes it up :confused:

Wherever I look in your code I come across anomalies between comments and code like this

 // initialize digital pin 11 as an output.
  pinMode(13, OUTPUT);
  // initialize digital pin 8 as an output.
  pinMode(6, OUTPUT);
  // initialize digital pin 8 as an output.
  pinMode(9, OUTPUT);

which does not inspire confidence. Then I find this

     pinMode(9, LOW);

which is an odd way to make a pin an input particularly when it has previous been set to be in output.

In your photo I cannot actually see what is connected where on the switch. Can you please provide a full description ?

Thank you, I have a layout diagram here. I have used different pins in the diagram for the two LED’s to run simple blinks but they work fine and are connected properly, but the hardware is correct and the effects all work individually with the button count. It’s just the coding in the loop that seems to be the problem. I’m not sure how to do this.

These lines

 while (digitalRead(buttonPin) == LOW);


 while (digitalRead(buttonPin) == HIGH);

will stall the program until the button is pressed/released so the code in the cases will only run once whatever the state of the button.

You need to change how you detect the change of state of the button to allow loop() to run freely and for the code in the current case to run repeatedly.

Look at the StateChangeDetection example in the IDE to see how to do it.