Using a momentary button as a switch to start and stop a program

Ok, i am sure i'm just missing something dumb but have tried everything i can think of and searched on line with no luck. what i'm trying to do is use a button press to turn on 3 leds. led 1 needs to remain on while the other 2 led's alternate turning on and off i have accomplished the led portion minus the button using this code ......

void setup() {
pinMode(3, OUTPUT); //led 1 blue
pinMode(4, OUTPUT); //led 2 green
pinMode(5, OUTPUT); //led 3 yellow
}

void loop() {
digitalWrite(3, HIGH); //led 1 on
digitalWrite(4, LOW); //led 2 off
digitalWrite(5, HIGH); //led 3 on
delay(975);
digitalWrite(4, HIGH); //led 2 on
digitalWrite(5, LOW); //led 3 off
delay(1100);
}

now the problem is i cant figure out how to start that code after a button press and stop after the same button is pressed again. I have a button wired to pin 2.

i appreciate any help given, coding is not my strong point so if the code i have is sloppy please let me know how to improveit. thank you.

try this out, it is straight forward and simple it uses an interrupt to capture your button pressing so it is extremely Responsive and it also has not delay() blocking functions
Try it out

volatile int Go = LOW; // Starts in th off state
volatile int X;
void setup() {
  pinMode(3, OUTPUT); //led 1 blue
  pinMode(4, OUTPUT); //led 2 green
  pinMode(5, OUTPUT); //led 3 yellow
  attachInterrupt(digitalPinToInterrupt(2),
  []() { // inline Lambda Function Google: Lambda Anonymous Function
    static unsigned long DebounceTimer;
    if ( millis() - DebounceTimer >= (100)) { // Debounce Go
      DebounceTimer = millis();
      Go = (Go == HIGH) ? LOW : HIGH; // Toggle Go
      X = 0; // Reset your LED Timer counter to start the swquence from the beginning
    }
  } // End of Lambda Funciton
  , CHANGE ); //End of attachInterrupt()
}

void loop() {
  static unsigned long LEDTimer;
  if ( millis() - LEDTimer >= (1)) { // 1 milisecond 
    LEDTimer += (1);
    digitalWrite(3, Go);
    digitalWrite(4, (X > 975) ? Go : LOW);
    digitalWrite(5, (X <= 975) ? Go : LOW);
    X++;
    if (X > (975 + 1100)) X = 0;
  }
}

Thank you Thank you Thank you does exactly what i need it to do.

That, is a pretty ugly and complicated piece of code to do something simple.... Using the interrupts to read buttons is nonsens to start with... Just poll them...

But it's a wise lesson, don't use delays() :wink:

That, is a pretty interesting piece of code to do something simple . . .
Retrofitting a piece of code with 'button press' functionality is well achieved using an interrupt. That way, you don't have to look too carefully for blocking constructs (delays etc.) in the existing code.
The OP may however, if this is school work, have some difficulty explaining to teacher how it all works and the design decision to use anonymous functions.

6v6gt:
That, is a pretty interesting piece of code to do something simple . . .
Retrofitting a piece of code with 'button press' functionality is well achieved using an interrupt. That way, you don't have to look too carefully for blocking constructs (delays etc.) in the existing code.
The OP may however, if this is school work, have some difficulty explaining to teacher how it all works and the design decision to use anonymous functions.

Agreed :slight_smile: lol I didn't want to make it simple just in case but no one was helping 2 hours later. so I enjoyed making this with:
Lambda
2 kinds of Blind without delay (one with windup issues but is extremely precise another without for the debounce)
interrupts
static variables
volatile variables
Conditional Operator ( ? : )
I gave a pointer as to where to look for the most confusing piece of code :slight_smile:
If (it is an assignment) his teacher will have fun!
Else he can post here and I will explain and share :slight_smile:
Z

I've just noticed that your are using an integer to hold what is effectively a boolean value.

volatile int Go = LOW; // Starts in the off state
. . .
. . .
Go = (Go == HIGH) ? LOW : HIGH; // Toggle Go
. . .

This gives you another interesting possibility for assigning the logical not of a variable to itself, while still avoiding the use of the more popular ! (not) operator.

Go = (Go - 1 ) * -1

:slight_smile: