Go Down

Topic: Start/stop loop via button, noob halp! (Read 643 times) previous topic - next topic

Natedogg_21

Nov 30, 2018, 04:13 am Last Edit: Nov 30, 2018, 04:47 am by Natedogg_21 Reason: because noob
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:




Code: [Select]

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)
  }
}

jremington

#1
Nov 30, 2018, 04:27 am Last Edit: Nov 30, 2018, 04:27 am by jremington
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().

Paul_KD7HB

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  

Natedogg_21

Thx for replies.

Quote
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.


Quote
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.

jremington

#4
Nov 30, 2018, 05:12 am Last Edit: Nov 30, 2018, 05:35 am by jremington
That could be because Paul_K's suggestion won't work, for several reasons.

Code: [Select]
  if(run = 255)

1. "=" should be "=="

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

Quote
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: https://www.baldengineer.com/blink-without-delay-explained.html

Natedogg_21

Quote
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.

Code: [Select]

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);


  }
}

jremington

#6
Nov 30, 2018, 05:33 am Last Edit: Nov 30, 2018, 05:39 am by jremington
Quote
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:
Code: [Select]
    if (run = 1)

Natedogg_21

#7
Nov 30, 2018, 05:49 am Last Edit: Nov 30, 2018, 05:50 am by Natedogg_21
Quote
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.

Code: [Select]

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);


  }
}

jremington

#8
Nov 30, 2018, 05:52 am Last Edit: Nov 30, 2018, 05:52 am by jremington
Code: [Select]
    if (run = 0)Still wrong.

Please IMPRINT "==" into your brain.

Natedogg_21

Thx for your replies.

Quote
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.

Code: [Select]

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);


  }
}

Natedogg_21

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;

leongjerland

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:
Code: [Select]
run = !run

Later you test for
Code: [Select]
if (run) {

or

if (run == true) {


Look at this example.

UKHeliBob

Quote
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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Natedogg_21

#13
Nov 30, 2018, 02:30 pm Last Edit: Nov 30, 2018, 02:30 pm by Natedogg_21
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.

Code: [Select]


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 {

  }

}

Go Up