Starting and stopping a loop with a Momentary Switch.

Hello,

I'm new to Arduino programming, I'm trying make a basic program that starts and stops on a button press.

The output when active i'm looking to get is 6 sec high every 60 seconds.

So far i have been trying to modify the code from Switching Things On And Off With An Arduino | Martyn Currey with no luck.

// Sketch: SwitchingThings_05
//
// An example of using a button switch as a toggle switch to turn a blinking LED on or off
//
// Pins
// D10 to resister and LED
// D2 to push button switch
//

// Define the pins being used
int pin_LED = 5;
int pin_switch = 2;

// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;

// variables to hold the times
unsigned long timeNow = 0;
unsigned long timePrev = 0 ;
unsigned int timeWait = 1000;

// variables used to control the LED
boolean flashingLEDisON = false;
boolean LEDstatus = LOW;
boolean keyPressed = false;

void setup()
{
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(FILE);
Serial.print("Uploaded: "); Serial.println(DATE);
Serial.println(" ");

pinMode(pin_LED, OUTPUT);
digitalWrite(pin_LED,LOW);

pinMode(pin_switch, INPUT);
}

void loop()
{
newSwitchState1 = digitalRead(pin_switch); delay(1);
newSwitchState2 = digitalRead(pin_switch); delay(1);
newSwitchState3 = digitalRead(pin_switch);

if ( (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3) )
{
if ( newSwitchState1 != oldSwitchState )
{
if ( newSwitchState1 == HIGH ) { keyPressed = true; } else { keyPressed = false; }
oldSwitchState = newSwitchState1;
}
}

if ( keyPressed )
{
// turn on or turn off the blinking LED
if ( flashingLEDisON == false)
{
flashingLEDisON = true;

}
else
{
flashingLEDisON = false;
// the LED may be on so to be safe we turn it off. If you wished you could check LEDstatus
LEDstatus = LOW; digitalWrite(pin_LED, LEDstatus);
}
keyPressed = false;
}

// if the blinking LED is on. See if it is time to blink it
if ( flashingLEDisON == true )
{
timeNow = millis();
if (timeNow-timePrev >= timeWait )
{
timePrev = timeNow;
if (LEDstatus == LOW) { LEDstatus = HIGH; } else { LEDstatus = LOW; }
digitalWrite(pin_LED, LEDstatus);
}
}
}

Any help would be much appreciated :slight_smile:

presumably you've added a pull-down resistor and have a switch connected between the digital pin and Vcc so that a button press is recognized as a HIGH. a more common approach is to configure the pin as INPUT_PULLUP and wire the switch between the pin and ground. pressing the switch causes the input to go LOW

seems like you're over complicating the need to debounce the input. adding a 10msec delay after reading the switch input should be enough and just compare the previous to new input state

So far i have been trying to modify the code from Switching Things On And Off With An Arduino – Martyn Currey with no luck.

Welcome to the forum. Please read the How to Use This Forum sticky.

You will find out how to post your code using the code tags </>.

You will also see some advice on how to flesh out the statement "with no luck" by adding some specifics about what the code does and does not do.

You will get much better help when you post a specific, well posed question with the code posted so that it appears in a scroll window like this

//Your code here