Arduino Looping states in Void Loop help needed for a novice.

Hello everyone.

I am still very new to programming an Arduino. I have this code that changes states, but it does not repeat. It works once and I then need to go through all the counts again to repeat. How can I keep each state repeating without having to go through the count again.

Any help would be appreciated.

Code below.


int button = 4;
  int pin2 = 2;
   int pin7 = 7;
    int oldstate = LOW;
     int count = HIGH;

 void setup() {
  pinMode(button, INPUT);
   pinMode(pin2, OUTPUT);
    pinMode(pin7, OUTPUT);  
    }


 void loop() {

  int buttonstate = digitalRead(button);
   if (buttonstate == oldstate && buttonstate == HIGH){        
    if (count == 1){
     digitalWrite(pin2,HIGH);
        delay(400);
         digitalWrite(pin2,LOW);
          delay(400);
           digitalWrite(pin2,HIGH);
            delay(400);
             digitalWrite(pin2,LOW);
              delay(400);
              }
      
     if (count == 2){
      digitalWrite(pin7,HIGH);
        delay(400);
         digitalWrite(pin7,LOW);
          delay(400);
           digitalWrite(pin7,HIGH);
            delay(400);
             digitalWrite(pin7,LOW);
              delay(400);
              }

      
      if (count < 2);

      else
       count = 0;
        count++;
         delay(50);
         }
         
      oldstate = buttonstate;
      }

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like
    if (count < 2);

What is this line of code intended to do ?

2 is the maximum count. When I press the button for a 3rd time, it does not count 3, it goes back to count 1.

I think you meant something like

count++;
if (count > 2)
{
  count = 0;
}

If I do that, it will no go to the second state.

if (count > 2)
{

It will remain on
if (count == 1) {

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

1 Like

when i test this code on my hardware with a button normally HIGH, i see the LEDs toggling and the count toggling between 1 & 2

the above looks odd. shouldn't you check for buttonstate != oldstate?

it's conventional to wire buttons between the pin and ground and configure the pin as INPUT_PULLUP. the button pin goes LOW when pressed

Still very new to the whole thing. This was just an easier way for me to understand.

When I add the
if (buttonstate == oldstate != buttonstate == HIGH){
It no longer toggles through the states. It just moves back and forth.
I need to control the count, but also loop state 1 or 2.

what does this mean?
how is your button wired?

i see count changing values

I have 2 leds. When I press the button once it moves to state 1

if (count == 1){
     digitalWrite(pin2,HIGH);
        delay(400);
         digitalWrite(pin2,LOW);
          delay(400);
           digitalWrite(pin2,HIGH);
            delay(400);
             digitalWrite(pin2,LOW);
              delay(400);
              }

the led on pin 2 blinks.

When I press the button a second time it goes to state 2

 if (count == 2){
      digitalWrite(pin7,HIGH);
        delay(400);
         digitalWrite(pin7,LOW);
          delay(400);
           digitalWrite(pin7,HIGH);
            delay(400);
             digitalWrite(pin7,LOW);
              delay(400);
              }

The led on pin 7 blinks.
When I use the
if (buttonstate == oldstate && buttonstate == HIGH){
it stays on the state. It will run each command in the assigned state 1 or 2 depending on if I press the button once or twice, but when I change the && to !=
if (buttonstate == oldstate != buttonstate == HIGH){
then it does not change from the button press it moves through each state by its self.

if (buttonstate != oldstate && buttonstate == HIGH){

That works. Thank you. However, I still need to be able to loop the states.

when i tested your code, count toggles between 1&2

how is your button wired?

The toggle works perfectly. But the state only runs once. I am trying to get the state to repeat over and over again. So if I push the button once, and I go to state one

 if (count == 1) {
      digitalWrite(pin2, HIGH);
      delay(400);
      digitalWrite(pin2, LOW);
      delay(400);
      digitalWrite(pin2, HIGH);
      delay(400);
      digitalWrite(pin2, LOW);
      delay(400);
    }

repeats over and over again. I want this to repeat with out me having to go through each state again. I need it to loop.

this is still an unsufficient description of the functionality that you want to have.

As the number of things that happen is pretty small it will be no problem for you to describe

FOUR

things:

  1. the times when you the user presses a button
  2. what the code shall do after a press
  3. writing additional lines what happens if you do not press the button
  4. writing down a new line what will happen then

so that all describing lines add up to a description that shows all the different things that shall happen or that will happen if you press a button or you don't press a button.

A description what a bytstanding person could describe by simply watching you using the microcontroller to demonstrate what the program does.

Split you sketch into 2 stages like this pseudo code

start of loop()
//button handling
if the button becomes pressed
  increment count
  if count greater than 2
    reset count to zero
  end if
end if

//act on current state value
if state equals 0
  code for state 0
end if
else
if state equals 1
  code for state 1
end if
else
if state equals 2
  code for state 2
end if
end of loop()

  1. I would like to press it once for state 1. Twice for state 2.
  2. When pressed once I would like it to run the state 1 code I have written and then loop it till I press the button a second time. When pressed the second time I would like it to run the code in state 2 and loop it.
    3.If I press the button once and state one comes on, I want it to stay on sate one and loop over and over again. So if it is a blinking led for state one. It must stay on state one and loop state one forever, or till I press the button again to move to state two. When it moves to state two it then flashes the second led continuously till I press the pin to go back to sate one.
    It must maintain its state and loop forever.
    Please let me know if these 3 answer any questions. Thank you.

Is this a better description?
I want to build a light, but I would like it to have many modes like Christmas tree lights. So if I have 10 led's, I would like to click the button once and it moves to a list of coding on the first increment then the lights flash on and off, and repeat the same way until I press the button again and move to another the second state where it will flash in a different until I then press the button again and it moves back to its original state. Just like a set of Christmas tree lights with a controller.