Question about button click and hold


i want use button click control led1 , button hold control led2, program didn't work time. wondering if there wrong program.

const int buttonpin = 4;     
const int ledpin1 = 13 ;   
const int ledpin2 = 8 ;

int lastbuttonstate = low;   
boolean ledval1 = false;
boolean ledval2 = false;

long lastdebouncetime = 0;
long holdtime = 2000;
long debouncedelay = 50;   


void setup() {
  pinmode(buttonpin, input);
  pinmode(ledpin1, output);   
  pinmode(ledpin2, output);   
}
void loop() {
  int reading = digitalread(buttonpin);
   if (reading != lastbuttonstate)
   {lastdebouncetime = millis();}

  if (reading == high  && (millis() - lastdebouncetime) < holdtime && (millis() - lastdebouncetime) > debouncedelay  )
  {ledval2 = !ledval2; }
  else   if (reading == high  && (millis() - lastdebouncetime) > holdtime )
  {ledval1 = !ledval1; }

  digitalwrite(ledpin1,ledval1);
  digitalwrite(ledpin2,ledval2);
  lastbuttonstate = reading;
}

your code seems ok don't think doing think should

when toggle led fast not flashing. (it may dimmer eye sight tell led in on)

when use ! toggle led 50/50 chance when if no longer true led left in on or off state

if using input connected button 10k reisitor should added between pin , ground rail. (consider using input_pullup instead. can find details under learning tab)

i have included example code should trying ( think )

code: [select]
const int buttonpin = 4;
const int ledpin1 = 8 ;
const int ledpin2 = 13 ;

int lastbuttonstate = low;
boolean ledval1 = false;
boolean ledval2 = false;

unsigned long lastdebouncetime = 0;
unsigned long holdtime = 2000;
unsigned long debouncedelay = 50;
unsigned long previousmillis = 0;
byte  buttonpressed = 0;

void setup() {
  pinmode(buttonpin, input);
  pinmode(ledpin1, output);
  pinmode(ledpin2, output);
}
void loop() {
  unsigned long currentmillis = millis();
  int reading = digitalread(buttonpin);

  if ((reading == high) && (millis() - lastdebouncetime > debouncedelay)) {
    buttonpressed = 1;//used flag show button pressed 50ms
  }

  if (buttonpressed == 1) {//next section runs if flag made
    if (millis() - lastdebouncetime < holdtime) {//check see if been held 2 seconds
      ledval2 = !ledval2;//up 2 seconds this
      ledval1 = 0;//turn other led off
    } else {
      ledval1 = !ledval1;//after 2 seconds this
      ledval2 = 0;//turn other led off
    }
  }//end of  if (buttonpressed == 1) {

  if (reading == low) {//button not pressed
    lastdebouncetime = millis();
    digitalwrite(ledpin1, low);//make sure leds off
    digitalwrite(ledpin2, low);
    buttonpressed = 0;//remove flag
  }
  if ((buttonpressed == 1) && (currentmillis - previousmillis >= 50)) { //used flash leds
    //this added loop fast led flashes looks stays on
    digitalwrite(ledpin1, ledval1);
    digitalwrite(ledpin2, ledval2);
    previousmillis = currentmillis;
  }
}




Arduino Forum > Using Arduino > Programming Questions > Question about button click and hold


arduino

Comments