If button pressed once...If button pressed twice?

Im not trying to get you to write the code for me. But i still cant see the problem?

What does this code do?

      ledEoff = ledEoff;

This?

  buttonPresses = buttonPresses;

Explain again, please, about the timing.

You detect that some value is odd, so you enter the odd block. It is time to toggle the state of the LED or it isn't. If it is, you toggle the state.

Before testing if it is time to toggle the state of the LED, you should test if it soon enough after the value changed to even be doing anything about the LED. Like so, in pseudo code:

Is number odd? How long has it been since the number became odd? If less than 5 seconds: Is it time to toggle the LED? If so: Toggle the LED. Else Don't do a darn thing Else How long has it been since the number became even? If less than 10 seconds: Is it time to toggle the LED? If so: Toggle the LED. Else Don't do a darn thing

Obviously, this depends on knowing when the number changed from odd to even or from even to odd.

Your right. I just need these leds to switch off after a certain time!! Think im going to start again...

The single and double presses are working fine.

Basically this is what Im trying to achieve but without using the ‚Äúdelay‚ÄĚ seeing that the ‚Äúdelay‚ÄĚ function interrupts button presses‚Ķ

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledOdd =  13;      // the number of the LED pin
const int ledEven = 50;
// variables will change:
int buttonstate = 0;   
// variable for reading the pushbutton status
int buttonPress = 0;

int buttonPresses = 0;
 // ledState used to set the LED


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledOdd, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  pinMode(ledEven, OUTPUT);
}

void loop(){
  // read the state of the pushbutton value:
  buttonstate = digitalRead(buttonPin);
buttonPresses = buttonPresses;
  if(buttonstate == HIGH) buttonPresses ++;
  boolean odd = buttonPresses % 2;
 
  if(odd){
//this will only happen if buttonPresses is odd
    digitalWrite(ledOdd, HIGH);
    delay(5000);
    digitalWrite(ledOdd, LOW);
  }
}



else{
  //this will only happen if buttonPresses is even
    digitalWrite(ledEven, HIGH);
    delay(10000);
    digitalWrite(ledEven, LOW);
    }
  }

I cant get to grip with using this millis thing. I cant work out how to turn the leds off indefinitely.

With this code i have managed to stop the led

/* Blink without Delay

 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.


 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen

 This example code is in the public domain.


 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 5000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
  digitalWrite(ledPin, HIGH);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is on turn it off:
    if (ledState == HIGH)
      ledState = LOW;
    

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

As soon as i enter this into my button presses code it doesent seem to work?

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledOdd =  13;      // the number of the LED pin
const int ledEven = 50;

// variables will change:
int buttonstate = 0;   
// variable for reading the pushbutton status
int buttonPress = 0;
int buttonPresses = 0;
int ledState = HIGH;
long interval = 5000;           // interval at which to blink (milliseconds)
long previousMillis = 0;        // will store last time LED was updated

unsigned long time;
unsigned long time2;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledOdd, OUTPUT);
  pinMode(ledEven, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  digitalWrite(ledOdd, HIGH);  
    digitalWrite(ledEven, HIGH);  

}

void loop()
{   
  // read the state of the pushbutton value:  
  buttonstate = digitalRead(buttonPin);
  if(buttonstate == HIGH) buttonPresses ++;
  boolean odd = buttonPresses % 2;

  if(odd)
  {
 unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is on turn it off:
    if (ledState == HIGH)
      ledState = LOW;
    

    // set the LED with the ledState of the variable:
    digitalWrite(ledOdd, ledState);
 }

 else
 {unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is on turn it off:
    if (ledState == HIGH)
      ledState = LOW;
    

    // set the LED with the ledState of the variable:
    digitalWrite(ledEven, ledState);}
 }
  }
}

I have changed the comment now. I changed the code because thats what i wanted. "If led On Turn Led Off".

In my void setup i put

digitalWrite(ledOdd, HIGH);
digitalWrite(ledEven, HIGH);

So the code runs fine when its on its own. But as soon as i include my button presses it doesent work? Let me make it clear what im trying to achieve now:

Button pressed Once Led Odd value is turned HIGH (for e.g 5 sec's) After 5secs passed led Off.

Button Presses again Led Even value is turned HIGH (for e.g 7 sec's) After 7 sec's passed led Off

And If pressed again it should roll over itself back to the beginning..

Hey swifftyb,

I’m not sure if you have manage to achieve what you set out to do

if buttonSwitch pressed once (turn on led1) for 5seconds
If buttonSwitch pressed twice (blink led1) for 10 seconds.

But if not I have put together this…

int switchPin = 3;
int ledPin = 13;


long interval = 1000;           // interval at 1 sec
long previousMillis = 0;        // will store last time LED was updated
int CountTime = 0;              // Default Count
int Countdown = 0;

int ButtonPressed = 0;          // Counts the amount of times the button has been pressed


boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = LOW;


void setup(){
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}



boolean debounce(boolean last){                      
  boolean current = digitalRead(switchPin);
  if (last != current){
    delay(5);
    current = digitalRead(switchPin);
  } 
  return current;
}



void loop(){
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)  {
    ButtonPressed++; 
    CountTime=0; 
  } 
  else
    if (ButtonPressed == 1) {
      Blink();
    }
    else
      if (ButtonPressed == 2) {
        LEDon();
      }
      else
        if (ButtonPressed > 2) {
          ledOn = LOW; 
          digitalWrite(ledPin, ledOn); 
          ButtonPressed = 1;                         // Go back to back to ButtonPressd 1 and blink
        }
  lastButton = currentButton;
}



void Blink()
{
  unsigned long currentMillis = millis();

  if((currentMillis - previousMillis > interval)) {  
    previousMillis = currentMillis;   
    if (ledOn == LOW)
      ledOn = HIGH; 
    else
      ledOn = LOW;
    digitalWrite(ledPin, ledOn); 
    
    Countdown = 10;                             // Change this to the length of time you want the Blink
    TIMINGS();                                 //This is used to track countdown
  }
}


void LEDon()
{
  unsigned long currentMillis = millis();
  
  if((currentMillis - previousMillis > interval)) {  
    previousMillis = currentMillis;        
    if (ledOn == LOW)
      ledOn = HIGH; 
    else
      ledOn = HIGH;
    digitalWrite(ledPin, ledOn);

    Countdown = 5;                              // Change this to the length of time you want the LED to stay on
    TIMINGS();                                  //This is used to track countdown
  }  

}


void TIMINGS(){

  int Countstop = Countdown - 1 ;
  if (CountTime > Countstop *2.1)
  {
    CountTime=100; 
    ledOn = LOW;
    digitalWrite(ledPin, ledOn);
  }
  if (CountTime < Countdown *2.2) 
  {                                        
    CountTime ++; 
    Serial.print(Countdown);
  }
}

Hope you find it useful!