Go Down

Topic: Button Library question. (Read 1 time) previous topic - next topic

Sannin

Hello all.

Is there a simple way to use the button library
http://www.arduino.cc/playground/Code/Button

to do something if the button is held down for a specified amount of time?
Thanks.

CrossRoads

Looks pretty straight forward - check for a button press, then check for a boolean stateChange after your time period - if not, do your action.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

AlphaBeta

Code: [Select]

if (button.stateChanged()) {
  if (button.isPressed()) {
    //the state has changed and the button is pressed, note the time
    pressedStartTime= millis();
  } else {
    //the state has changed and the button is released
    if (millis()-pressedStartTime > HOLD_TIME) {
      //the button was pressed for HOLD_TIME milliseconds
      //do something
    }
  }
}


Along those lines, not tested.

Sannin

Thanks.. Looks right but not working. I will have to go into it more when I get some time.

Sannin

hmm... it looks like stateChanged() is the problem.. it doesnt seem to work..

AlphaBeta

Could you test this:
Code: [Select]
#include <Button.h>
Button button = Button(12,PULLUP);

void setup(){
  pinMode(13,OUTPUT); //debug to led 13
}

void loop(){
  if(button.stateChanged()){
digitalWrite(13,!digitalRead(13));
  }
}


Wire your button directly between the pin and ground.

Sannin


Could you test this:
Code: [Select]
#include <Button.h>
Button button = Button(12,PULLUP);

void setup(){
  pinMode(13,OUTPUT); //debug to led 13
}

void loop(){
  if(button.stateChanged()){
digitalWrite(13,!digitalRead(13));
  }
}


Wire your button directly between the pin and ground.


Didn't work..

I did run the button.isPressed fine.

Wish this library did something like this

Button pressMe = button(2, PULLUP);
if (pressMe.held(1000)) someFunction();

AlphaBeta

Sorry. One need to update the button first, by button.isPressed();
Code: [Select]
#include <Button.h>
Button button = Button(12,PULLUP);

void setup(){
  pinMode(13,OUTPUT); //debug to led 13
}

void loop(){
  button.isPressed();
  if(button.stateChanged()){
    digitalWrite(13,!digitalRead(13));
  }
}

That works well here.

AlphaBeta

New version:
Code: [Select]
#include <Button.h>
Button button = Button(12,PULLUP);
int sleep = 20;

void setup(){
  pinMode(13,OUTPUT); //debug to led 13
}

void loop(){
  button.isPressed();
  if(button.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
    if(button.held(2000)){ //this is true once, after the button has been held for 2000 ms
      sleep *= 2;
    }
    digitalWrite(13,!digitalRead(13));
    delay(sleep);
  }
}

New lib attached (will upload as soon as it's working)

Sannin

#9
Feb 10, 2011, 12:20 am Last Edit: Feb 10, 2011, 12:32 am by Sannin Reason: 1
OMG you ROCK. I tested it and it works. =(
I am having the button send a serial code if held down for 2 seconds.
With this code though it will keep sending the serial until the button is let go.
I think I could use a variable to prevent it from getting sent more then once and will also use the same varible to prevent other buttons from sending serial untill that command is completed.

AlphaBeta

Try switching from the heldFor to held, it should just be true once.

Glad it worked 8)

Sannin

#11
Feb 22, 2011, 09:12 pm Last Edit: Feb 23, 2011, 12:22 am by Sannin Reason: 1
Hey AlphaBeta
Thanks for everything. Just got done building the whole project. Did find one bug at this point. I have 7 buttons so far. The press and hold function works the first time but after that the buttons will insta trigger and the hold will no longer work.

Edit: after some more testing the hold just seems intermittent. Works as intended sometimes and not others. I will post the code when I get a chance.

AlphaBeta

Please do post the code :)
I cannot reproduce the errors here.

Sannin

#13
Mar 01, 2011, 07:52 pm Last Edit: Mar 01, 2011, 08:15 pm by Sannin Reason: 1
Here is the code. The messrun variable is to stop people from running another button until the device I am talking to clears it.
Edit: there is some redundant code that will be removed. Also I was just thinking that I blindly used the example you made. Can I just use the
If held and take out the heldfor?
Code: [Select]
#include <Button.h>
// Variables
int Nion1 = 0;
int Nion2 = 0;
int Nion3 = 0;
int Nion4 = 0;
int Nion5 = 0;
int Nion6 = 0;
int Nion7 = 0;
int Nion8 = 0;
int Nion9 = 0;
int sleep = 20;
int messrun = 0;
const int MessLED = 12;
const int HotMicLED = 11;
String annledon;
String annledoff;
String micon;
String micoff;
long pressedStartTime = 0;
//long HOLD_TIME = 2000;
long previousMillis = 0;
long interval = 2000;
// Inputs
Button ann1 = Button(2,PULLUP);
Button ann2 = Button(3,PULLUP);
Button ann3 = Button(4,PULLUP);
Button ann4 = Button(5,PULLUP);
Button ann5 = Button(6,PULLUP);
Button ann6 = Button(7,PULLUP);
Button ann7 = Button(8,PULLUP);
Button ann8 = Button(9,PULLUP);
Button ann9 = Button(10,PULLUP);
// Outputs
const int Mic = 11;
const int ParkAnn = 12;


void setup()
{
 Serial.begin(19200);
 annledon = String("VACTFF.");
 annledoff = String("VACT00.");
 micon = String("VMCCFF.");
 micoff = String("VMCC00.");
 pinMode (MessLED, OUTPUT) ;
 pinMode (HotMicLED, OUTPUT);
 digitalWrite(MessLED, HIGH);
 digitalWrite(HotMicLED, HIGH);
 
}

char buffer[8];  
int index = 0;    
char lastRead;    

// Print contents of buffer then clear it
void dumpBuffer()
{
 buffer[index] == 0; // add a null terminator to the string in the buffer
 //Serial.println(buffer); // print the contents of the buffer
//-----------------------Serial String Compare---------------------------------
  if (annledon.compareTo(buffer) == 0){
digitalWrite(MessLED, LOW);
}

if (annledoff.compareTo(buffer) == 0){
digitalWrite(MessLED, HIGH);
messrun = 0;
}

if (micon.compareTo(buffer) == 0){
digitalWrite(HotMicLED, LOW);
}

if (micoff.compareTo(buffer) == 0){
digitalWrite(HotMicLED, HIGH);
Serial.print("SMOFFF");
}
//----------------------end Serial String Compare------------------------------
 index = 0; //reset the index
 for (int i = 0; i < 8; i++)
 {
   buffer[i] = 0; // clear the buffer
 }
}
   
void loop()
{
 

//---------------------Get Status From Nion------------------------------------
 if (millis() - previousMillis > interval) {
     previousMillis = millis();
     Serial.print("GACT.");
     Serial.print("GMIC.");
}
//--------------------end get status from Nion---------------------------------

 
 
//-------------------Read Serial from Nion-------------------------------------  
 if (Serial.available() > 0)
 {
   lastRead = Serial.read();
   if (index < 7)
   {
     buffer[index++] = lastRead;  // append to the buffer
   }
   else  // buffer overflow - message too long
   {
     //Serial.print("Overflow->");
     dumpBuffer();
   }
   if (lastRead == '.')  
   {
     if (index == 7)
     {
        //Serial.print("GOOD->");  // we have a complete message
     }
     else // message too short
     {
        //Serial.print("SHORT->");  // we have an incomplete message
     }
     dumpBuffer();
   }
 }
//----------------------end read serial from Nion-------------------------------  
 


//--------------------------buttons---------------------------------------------  
   //-------------------------button 1-----------------------------
ann1.isPressed();
 if(ann1.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann1.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SAN1FF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }
  //-------------------------button 2------------------------------
ann2.isPressed();
 if(ann2.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann2.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SAN2FF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }
  //-------------------------button 3------------------------------
ann3.isPressed();
 if(ann3.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann3.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SAN3FF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }

 //-------------------------button 4------------------------------
ann4.isPressed();
 if(ann4.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann4.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SAN4FF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }

 //-------------------------button 5------------------------------
ann5.isPressed();
 if(ann5.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann5.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SAN5FF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }

 //-------------------------button 6------------------------------
ann6.isPressed();
 if(ann6.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann6.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SAN6FF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }

 //-------------------------button 7------------------------------
ann7.isPressed();
 if(ann7.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann7.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SAN7FF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }

 //-------------------------button 8 (Mic on button)------------------------------
ann8.isPressed();
 if(ann8.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann8.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SMICFF.");
   messrun = 1;
   }    
  }
   delay(sleep);
 }

 //-------------------------button 9 (Cancel button)------------------------------
ann9.isPressed();
 if(ann9.heldFor(1000)){ //this is true as long as the button is pressed after 1000 ms
   if(ann9.held(2000)){ //this is true once, after the button has been held for 2000 ms
     sleep *= 2;
     if(messrun == 0){
   Serial.print("SCNLFF.");
   }    
  }
   delay(sleep);
 }


//--------------------------end buttons-------------------------------------




}




Go Up