0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« on: February 01, 2011, 12:59:45 pm » |
Hello all. Is there a simple way to use the button library http://www.arduino.cc/playground/Code/Buttonto do something if the button is held down for a specified amount of time? Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 240
Posts: 16460
Available for Design & Build services
|
 |
« Reply #1 on: February 01, 2011, 01:09:39 pm » |
Looks pretty straight forward - check for a button press, then check for a boolean stateChange after your time period - if not, do your action.
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #2 on: February 01, 2011, 08:11:26 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« Reply #3 on: February 08, 2011, 06:49:56 pm » |
Thanks.. Looks right but not working. I will have to go into it more when I get some time.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« Reply #4 on: February 09, 2011, 02:19:09 pm » |
hmm... it looks like stateChanged() is the problem.. it doesnt seem to work..
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #5 on: February 09, 2011, 02:25:57 pm » |
Could you test this: #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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« Reply #6 on: February 09, 2011, 02:32:55 pm » |
Could you test this: #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();
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #7 on: February 09, 2011, 03:21:17 pm » |
Sorry. One need to update the button first, by button.isPressed(); #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.
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #8 on: February 09, 2011, 03:57:11 pm » |
New version: #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)
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« Reply #9 on: February 09, 2011, 06:20:22 pm » |
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.
|
|
|
|
« Last Edit: February 09, 2011, 06:32:11 pm by Sannin »
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #10 on: February 09, 2011, 07:18:45 pm » |
Try switching from the heldFor to held, it should just be true once. Glad it worked 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« Reply #11 on: February 22, 2011, 03:12:58 pm » |
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.
|
|
|
|
« Last Edit: February 22, 2011, 06:22:49 pm by Sannin »
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #12 on: February 27, 2011, 07:54:48 pm » |
Please do post the code  I cannot reproduce the errors here.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 30
Arduino rocks
|
 |
« Reply #13 on: March 01, 2011, 01:52:50 pm » |
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? #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-------------------------------------
}
|
|
|
|
« Last Edit: March 01, 2011, 02:15:27 pm by Sannin »
|
Logged
|
|
|
|
|
|