Start Loop Via Single input

Is there anyway of starting a loop that only requires one initial activation, e.g. pressing a button once as opposed to having to hold it?

Imagine this as a base but i dont want to have to hold the button down:

#include <Button.h>//Arduino Playground - HomePage
const byte ledPin = 13;
int Button = 12;
int val = 0;
int greenled = 8; //Led's and pins
int yellowled = 9;
int redled = 10;

void setup()
pinMode(greenled, OUTPUT); //Pinmodes of the leds
pinMode(yellowled, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(Button, INPUT);

void loop()
val = digitalRead(Button); // read input value
if (val == HIGH){
digitalWrite(greenled, HIGH); //Green on for 5 seconds
digitalWrite(greenled, LOW); //Green off, yellow on for 2 seconds
digitalWrite(yellowled, HIGH);
digitalWrite(yellowled, LOW); //yellow off, red on for 5 seconds
digitalWrite(redled, HIGH);
digitalWrite(yellowled, HIGH); //Yellow and red on for 2 seconds
digitalWrite(redled, LOW); //Red and Yellow off
digitalWrite(yellowled, LOW);

I realise this is a basic thing to most of you but im a newbie and need help :(!

Any help with this would be mega :slight_smile:



Add an extra variable boolean Started = false ;

val = digitalRead(Button);  // read input value
  if (val == HIGH){


 if ( digitalRead(Button) == HIGH ) Started = true ;  // Mark start when button is pushed
  if ( Started ){  // if button was pushed at some time since powerup ...

I think you will also find that all those delay() commands cause trouble. The Arduino can't detect the keypress while it is working its way through the delays. You need to change your code to the concept illustrated in the "blink without delay" example sketch.


Okay thanks :slight_smile: