Start/stop loop via button, noob halp!

Can someone please explain what I've missed here, I'm brand new to coding. I want the loop to start right away on power up and stop when input 0 goes low, then start again if input 0 goes low again. Just a toggle for the loop to run/stop via an input is what I'm after. Right now the button will stop but not restart the loop.

this is what I have:

int run;
int buttonPin;

void setup()

{ run = 255;
  buttonPin = 0;

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(0, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

}

void loop()
{

  if (digitalRead(buttonPin) == LOW)
  {
    run = digitalRead(buttonPin);

    if (run = 255)
    {
      run = 0;
    }
    else
    {
      run = 255;
    }

  }
  if (run > 0)
  {
    digitalWrite(7, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(7, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(6, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(6, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(5, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(5, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(4, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(4, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(3, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(3, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(3, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(3, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(4, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(4, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(5, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(5, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(6, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(6, LOW);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(7, HIGH);
    delay(25); // Wait for 100 millisecond(s)
    digitalWrite(7, LOW);
    delay(750); // Wait for 1000 millisecond(s)
  }
}

Please edit your post to add code tags, as described in the "How to use this forum" post.

Also format your code in the Arduino editor, using CRTL-T. That way you can easily see the if and loop structure.

Finally, NOTHING happens during delay(), which includes reading a button. If you want a program to be responsive to button pushes, don't use delay().

It doesn't work because you never put the value you read into "run".

if(digitalRead(buttonPin) == LOW)
{
if(run = 255)
{
run = 0;
}
else
{
run = 255;
}

Should be:
run = digital Read(buttonPin);

if(run = 255)
{
run = 0;
}
else
{
run = 255;
}

Paul

Thx for replies.

If you want a program to be responsive to button pushes, don't use delay().

I don't know how else to sequence the lights without the delay so I guess I'll live with it till I figure out another way.

Should be:
run = digital Read(buttonPin);

if(run = 255)
{
run = 0;
}
else
{
run = 255;
}

Paul

I added the run = digital Read(buttonPin); but it's still acts the same, it will stop but not restart.

That could be because Paul_K's suggestion won't work, for several reasons.

  if(run = 255)
  1. "=" should be "=="

  2. since digitalRead() returns 0 or 1, it makes no sense to set run to 255.

I don't know how else to sequence the lights without the delay

Use millis() for timing. This forum and the web provide many tutorials on how to do that.

For example: Blink without delay() explained line-by-line

  1. "=" should be "=="

  2. since digitalRead() returns 0 or 1, it makes no sense to set run to 255.

Changed = to be == and it now won't stop the loop. Also changed the 255 to 1.

int run;
int buttonPin;

void setup()

{ run = 1;
  buttonPin = 0;

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(0, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

}

void loop()
{

  if (digitalRead(buttonPin) == LOW)
  {


    if (run = 1)
    {
      run = 0;
    }
    else
    {
      run = 1;
    }

  }
  if (run > 0)
  {
    digitalWrite(7, HIGH);
    delay(25); 
    digitalWrite(7, LOW);
    delay(25); 
    digitalWrite(6, HIGH);
    delay(25); 
    digitalWrite(6, LOW);
    delay(25); 
    digitalWrite(5, HIGH);
    delay(25); 
    digitalWrite(5, LOW);
    delay(25); 
    digitalWrite(4, HIGH);
    delay(25); 
    digitalWrite(4, LOW);
    delay(25); 
    digitalWrite(3, HIGH);
    delay(25); 
    digitalWrite(3, LOW);
    delay(25); 
    digitalWrite(3, HIGH);
    delay(25); 
    digitalWrite(3, LOW);
    delay(25); 
    digitalWrite(4, HIGH);
    delay(25); 
    digitalWrite(4, LOW);
    delay(25); 
    digitalWrite(5, HIGH);
    delay(25); 
    digitalWrite(5, LOW);
    delay(25); 
    digitalWrite(6, HIGH);
    delay(25); 
    digitalWrite(6, LOW);
    delay(25); 
    digitalWrite(7, HIGH);
    delay(25); 
    digitalWrite(7, LOW);
    delay(750); 


  }
}

it now won't stop the loop

To figure out why, print out values of run at various places in the program.
Edit:
But here is a hint: what is the very first thing that happens after the "if" statement detects that the button is pressed?

Did you post the latest version of your program in reply #5?

If statement format is still wrong in the following:

    if (run = 1)

Did you post the latest version of your program in reply #5?

I thought I did but I was changing it so much I guess I didn't.

I made some progress, it now stops while the button is pressed but start as soon as released. If I can get it to toggle all will be well.

int run;
int buttonPin;

void setup()

{ run = 1;
  buttonPin = 0;

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(0, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

}

void loop()
{
  run = digitalRead(buttonPin);
  if (digitalRead(buttonPin) == LOW)
  {


    if (run == 1)
    {
      run = 0;
    }
    if (run = 0)
    {
      run = 1;
    }


  }
  if (run > 0)
  {
    digitalWrite(7, HIGH);
    delay(25);
    digitalWrite(7, LOW);
    delay(25);
    digitalWrite(6, HIGH);
    delay(25);
    digitalWrite(6, LOW);
    delay(25);
    digitalWrite(5, HIGH);
    delay(25);
    digitalWrite(5, LOW);
    delay(25);
    digitalWrite(4, HIGH);
    delay(25);
    digitalWrite(4, LOW);
    delay(25);
    digitalWrite(3, HIGH);
    delay(25);
    digitalWrite(3, LOW);
    delay(25);
    digitalWrite(3, HIGH);
    delay(25);
    digitalWrite(3, LOW);
    delay(25);
    digitalWrite(4, HIGH);
    delay(25);
    digitalWrite(4, LOW);
    delay(25);
    digitalWrite(5, HIGH);
    delay(25);
    digitalWrite(5, LOW);
    delay(25);
    digitalWrite(6, HIGH);
    delay(25);
    digitalWrite(6, LOW);
    delay(25);
    digitalWrite(7, HIGH);
    delay(25);
    digitalWrite(7, LOW);
    delay(750);


  }
}

    if (run = 0)Still wrong.

Please IMPRINT "==" into your brain.

Thx for your replies.

Still wrong.

Please IMPRINT “==” into your brain.

The button now does nothing at all…I don’t know the significance of the double = but it seems to break the button functionality all together. I could use a toggle button as leave as is with the single = but I’d rather understand what I need to change to get it to toggle.

int run;
int buttonPin;

void setup()

{ run = 1;
  buttonPin = 0;

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(0, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

}

void loop()
{
  run = digitalRead(buttonPin);
  if (digitalRead(buttonPin) == LOW)
  {


    if (run == 1)
    {
      run = 0;
    }
    if (run == 0)
    {
      run = 1;
    }


  }
  if (run > 0)
  {
    digitalWrite(7, HIGH);
    delay(25);
    digitalWrite(7, LOW);
    delay(25);
    digitalWrite(6, HIGH);
    delay(25);
    digitalWrite(6, LOW);
    delay(25);
    digitalWrite(5, HIGH);
    delay(25);
    digitalWrite(5, LOW);
    delay(25);
    digitalWrite(4, HIGH);
    delay(25);
    digitalWrite(4, LOW);
    delay(25);
    digitalWrite(3, HIGH);
    delay(25);
    digitalWrite(3, LOW);
    delay(25);
    digitalWrite(3, HIGH);
    delay(25);
    digitalWrite(3, LOW);
    delay(25);
    digitalWrite(4, HIGH);
    delay(25);
    digitalWrite(4, LOW);
    delay(25);
    digitalWrite(5, HIGH);
    delay(25);
    digitalWrite(5, LOW);
    delay(25);
    digitalWrite(6, HIGH);
    delay(25);
    digitalWrite(6, LOW);
    delay(25);
    digitalWrite(7, HIGH);
    delay(25);
    digitalWrite(7, LOW);
    delay(750);


  }
}

It stops while button is held (but no toggle) with this just as it did with the single =:
if (run == 1)
{
run == 0;
}
if (run == 0)
{
run == 1;

You have to detect when the button's state changes to "pressed".
Then switch the value of "run".
If you declare "run" as bool, use:

run = !run

Later you test for

if (run) {

or

if (run == true) {

Look at this example.

I don't know the significance of the double =

Don't do anything until you understand the difference between = and ==

It is so fundamental that you won't get far without knowing the difference

leongjerland Thank You! I checked that example and was able to cut a bit of code from there and now it’s working as intended.

UKHeliBob I did some reading on = Vs ==. Here I thought I was comparing values while actually assigning values…Noobs will be noobs.

Thanks for all the replies. I’ll leave this here for the next guy trying to make this work.

const int  buttonPin = 0;   

int buttonPushCounter = 0;   
int buttonState = 0;      
int lastButtonState = 0;    

void setup() {

  pinMode(buttonPin, INPUT);
  pinMode(0, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  Serial.begin(9600);
}


void loop() {
  
  buttonState = digitalRead(buttonPin);

  
  if (buttonState != lastButtonState) {
    
    if (buttonState == HIGH) {
      
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      
      Serial.println("off");
    }
    
    delay(50);
  }
  
  lastButtonState = buttonState;


  
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(7, HIGH);
    delay(25); 
    digitalWrite(7, LOW);
    delay(25); 
    digitalWrite(6, HIGH);
    delay(25); 
    digitalWrite(6, LOW);
    delay(25); 
    digitalWrite(5, HIGH);
    delay(25); 
    digitalWrite(5, LOW);
    delay(25); 
    digitalWrite(4, HIGH);
    delay(25); 
    digitalWrite(4, LOW);
    delay(25); 
    digitalWrite(3, HIGH);
    delay(25); 
    digitalWrite(3, LOW);
    delay(25); 
    digitalWrite(3, HIGH);
    delay(25);
    digitalWrite(3, LOW);
    delay(25); 
    digitalWrite(4, HIGH);
    delay(25); 
    digitalWrite(4, LOW);
    delay(25); 
    digitalWrite(5, HIGH);
    delay(25); 
    digitalWrite(5, LOW);
    delay(25); 
    digitalWrite(6, HIGH);
    delay(25); 
    digitalWrite(6, LOW);
    delay(25); 
    digitalWrite(7, HIGH);
    delay(25); 
    digitalWrite(7, LOW);
    delay(750); 
  } else {

  }

}