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

Hi,

I need help with some code.

I need the code to represent the following:

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

Any ideas?

Thanks

Thanks!
This is what i have so far.
So how do i record the time in millis?

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

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

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(5000);
}

if buttonState == HIGH) {
digitalWrite ledPin, HIGH);
delay(300);
digitalWrite ledPin, LOW);
delay(300);

else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Could you not just have the button be a set value, say 5 for the 5 seconds you need.

And then wait for say 200 ms after the button has been pressed to see how many times the button has been pressed.

Basically I am trying to say, have the button add 5000 to the delay part of the code so if it pressed twice it would ad 10,000 (10 seconds)

This would also let you have the button be pressed 3 times which would light the led for 15 seconds and so on.

Sorry if this post is totally useless and you don't understand, I could make a sketch if required.

Bert.

KE7GKP: HitTime = millis(); // record THIS hit time DiffTime = HitTime - PrevHit; // calculate the time since the LAST hit PrevHit = HitTime; // record this hit time so you can use it NEXT time

if DiffTime > 200 then.....

Im totaly lost as to where i should place this code in relation to the code i posted.

Aunother point in the right direction?

Have a variable called buttonPresses, or something similar. Each time the button is pressed, you add one to it: if(buttonstate == HIGH) buttonPresses ++; You can then use the modulo operator (http://arduino.cc/en/Reference/Modulo) to decide if buttonPresses is odd or even. boolean oddOrEven = buttonPresses % 2; oddOrEven will then be true (1, or high) if the number is odd. It will be false (0, low) if the number is even. Then you can use some simple if statements, remembering that they only operate if the statement inside the brackets is true: if(oddOrEven){ //this will only happen if buttonPresses is odd }

if(!oddOrEven){ //this will only happen if buttonPresses is even }

Although there is probably better ways, like the ones everyone else said, this would probably be the way I would use. Hopefully it helped! Onions.

1 Like

I am so lost. None of the code i write will compile.

Is anyone able to write me up an example so i can understand this fully?

Thanks

lol. i have got all of the sketches working. also rote a couple of my own, controlling relays in certain sequences ect.

i am just really stuck with this double press stuff.

when, do i have to define commands such as (hit, priv hit..) tried that and its still not working...

ok, this is the code i got

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin

// variables will change: int buttonState = 0; // variable for reading the pushbutton status int HitTime = 200; int buttonPress = 0; void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }

void loop(){ // read the state of the pushbutton value: buttonstate = digitalRead(buttonPin);

if(buttonstate == HIGH) buttonPresses ++; boolean oddOrEven = buttonPresses % 2;

if(oddOrEven){ //this will only happen if buttonPresses is odd digitalWrite(ledPin, HIGH); }

if(!oddOrEven){ //this will only happen if buttonPresses is even digitalWrite(ledPin, LOW); } }

I am gettin error message:

Button.cpp: In function 'void loop()': Button:44: error: 'buttonstate' was not declared in this scope Button:46: error: 'buttonPresses' was not declared in this scope Button:47: error: 'buttonPresses' was not declared in this scope

at the top of your sketch where your const int variable are add this:

int buttonstate = 0;
int buttonPresses = 0;

Then let us know what happens.

Wicked! I missed that totaly! Thanks. Ok this is the code i now have.

It compiles but the problem i have is that the led wont switch off after five seconds and i dont know how to only make it blink for 10 seconds? What am i missing?

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin

// variables will change: int buttonstate = 0; // variable for reading the pushbutton status int HitTime = 200; int buttonPress = 0; int buttonPresses = 0; void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }

void loop(){ // read the state of the pushbutton value: buttonstate = digitalRead(buttonPin); buttonPresses = 2;

if(buttonstate == HIGH) buttonPresses ++; boolean oddOrEven = buttonPresses % 2;

if(oddOrEven){ //this will only happen if buttonPresses is odd digitalWrite(ledPin, HIGH); delay(5000); digitalWrite(ledPin, LOW); }

if(!oddOrEven){ //this will only happen if buttonPresses is even digitalWrite(ledPin, HIGH); delay(300); digitalWrite(ledPin, LOW); delay(300); } }

Wicked, Ill carry on with this project tomorrow. Please tune in again if you can. Much appreciated

Sorry, I have this code now but the led automaticaly starts blinking again after its been on for five seconds. any suggestions?

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin

// variables will change: int buttonstate = 0; // variable for reading the pushbutton status int HitTime = 200; int buttonPress = 0; int buttonPresses = 0; int ledState = LOW; // ledState used to set the LED long previousMillis = 0; long interval = 1000;

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

void loop(){ // read the state of the pushbutton value: buttonstate = digitalRead(buttonPin); buttonPresses = 2;

if(buttonstate == HIGH) buttonPresses ++; boolean oddOrEven = buttonPresses % 2;

if(oddOrEven){ //this will only happen if buttonPresses is odd digitalWrite(ledPin, HIGH); }

if(!oddOrEven){ //this will only happen if buttonPresses is even unsigned long currentMillis = millis();

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

// if the LED is off turn it on and vice-versa: if (ledState == LOW) ledState = HIGH; else ledState = LOW;

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

Removed. - Didn't read your code properly.

if(oddOrEven){

Just for my education, what else could the number be?

Ok i know have the following code below.

Problems.

1) How do i get "led" to run for 5 seconds then turn off. 2) How do i get "ledPin" to run for 10 seconds then turn off.

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

int buttonPresses = 0;

int ledState = LOW; // ledState used to set the LED long previousMillis = 0; long previousMillis1 = 0; long interval = 1000; long interval1 = 1000;

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

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

if(oddOrEven){ //this will only happen if buttonPresses is odd unsigned long currentMillis = millis();

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

// if the LED is off turn it on and vice-versa: if (ledState == LOW) ledState = HIGH; else ledState = LOW;

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

if(!oddOrEven){ //this will only happen if buttonPresses is even unsigned long currentMillis = millis();

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

// if the LED is off turn it on and vice-versa: if (ledState == LOW) ledState = HIGH; else ledState = LOW;

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

// if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

This can be simplified:

   ledState = !ledState;
if(oddOrEven){
//this will only happen if buttonPresses is odd
}

if(!oddOrEven){
//this will only happen if buttonPresses is even

The variable name oddOrEven is a terrible name. From the way is is used, it should be called odd. Your first if test executes the body of the statement if the value is odd. Then, there is another if test. Under what circumstances can both blocks of code be executed? Under what circumstances can both blocks be skipped?

The answer is that only one block will be executed. There are no cases where both blocks can be executed, and there are no cases where neither block will be executed. You should, therefore, be using an if/else statement.

if(odd)
{
   // do the odd thing
}
else
{
   // do the even thing
}

1) How do i get "led" to run for 5 seconds then turn off. 2) How do i get "ledPin" to run for 10 seconds then turn off.

If I'm reading your code correctly, these names should be ledOdd and ledEven.

It might seem like I'm harping on trivial stuff, but I've been programming for 31+ years, and I KNOW how easy it is to screw things up when variable names do not say what they mean. If you put this code away for 6 months, then come back to it, will you remember that led is the odd LED pin and ledPin is the even LED pin? Will you remember that oddOrEven really means odd? Or will you think to yourself, as I often do when looking at code I wrote 10 years ago, "What idiot wrote this crap?"?

As for how to get the LEDs to stay on for longer, you are already using millis() to determine when to blink the LEDs. All you need to do is record when blinking started, and determine whether now is 5 seconds (or 10 seconds) later. If it is, don't blink the LED any more.

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 = LOW; int ledEoff = LOW; // ledState used to set the LED long previousMillis = 0; long interval = 1000; long intervalTen = 10000;

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 unsigned long currentMillis = millis();

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

// if the LED is off turn it on and vice-versa: ledState = !ledState;

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

else{ //this will only happen if buttonPresses is even unsigned long currentMillis = millis();

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

// if the LED is off turn it on and vice-versa: ledState = !ledState;

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

unsigned long currentMillis2 = millis(); if(currentMillis2 - previousMillis > intervalTen){ previousMillis = currentMillis2;

ledEoff = ledEoff;

digitalWrite(ledEven, ledEoff);} } }

Please post code correctly, using the # button.

      ledEoff = ledEoff;

Nothing wrong with this statement. It's just useless.

Was there a question with that last post, or a "Hey I got it working" statement that went missing?

Sorry. Dont know whats going on with my post.

Thanks paul, i have made the necessary changes but i still have a problem. I cant work out how to get the millis function to turn off the led's after 5 and ten seconds...

so far i have just tried to make ledeEven turn off after 10 seconds. Any advice?

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 = LOW;  
int ledEoff = LOW;
 // ledState used to set the LED
long previousMillis = 0;
long interval = 1000;
long intervalTen = 10000;

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
unsigned long currentMillis = millis();

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

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

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

else{
//this will only happen if buttonPresses is even
unsigned long currentMillis = millis();

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

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    // set the LED with the ledState of the variable:
    digitalWrite(ledEven, ledState);}
    
   unsigned long  currentMillis2 = millis();
    if(currentMillis2 - previousMillis > intervalTen){
      previousMillis = currentMillis2;
      
      ledEoff = ledEoff;
      
      digitalWrite(ledEven, ledEoff);}
  }
}

I suspect that, if you were to put each curly brace on it's own line, and properly indent your code, you would see the problem right away.

Like so:

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
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval)
    {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      ledState = !ledState;

      // set the LED with the ledState of the variable:
      digitalWrite(ledOdd, ledState);
    }
  }
  else
  {
    //this will only happen if buttonPresses is even
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval)
    {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      ledState = !ledState;

      // set the LED with the ledState of the variable:
      digitalWrite(ledEven, ledState);
    }
    
    unsigned long  currentMillis2 = millis();
    if(currentMillis2 - previousMillis > intervalTen)
    {
      previousMillis = currentMillis2;
      
      ledEoff = ledEoff;
      
      digitalWrite(ledEven, ledEoff);
    }
  }
}
  buttonPresses = buttonPresses;

More useless code.

You have code to turn toggle the LED state, for odd end numbers of switch presses. The code for even numbers of switch presses also contains a block of code whose purpose is not at all clear. Why is that code there, and what is it supposed to do?