Pages: [1]   Go Down
Author Topic: engine start stop with push and push+hold  (Read 1300 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys this is first post and first time using an arduino board
My plans were to use a start stop button with LED's to display the modes im in.

After searching the web for an already made sketch that i could  use for my build i came across one made by
Jeff Saltzman.
Code:
/* 4-Way Button: Click, Double-Click, Press+Hold, and Press+Long-Hold Test Sketch
By Jeff Saltzman
Oct. 13, 2009
 
To keep a physical interface as simple as possible, this sketch demonstrates generating four output events from a single push-button.
 1) Click: rapid press and release
 2) Double-Click: two clicks in quick succession
 3) Press and Hold: holding the button down
 4) Long Press and Hold: holding the button down for a long time
*/

#define buttonPin 19 // analog input pin to use as a digital input
#define ledPin1 17 // digital output pin for LED 1
#define ledPin2 16 // digital output pin for LED 2
#define ledPin3 15 // digital output pin for LED 3
#define ledPin4 14 // digital output pin for LED 4

// LED variables
boolean ledVal1 = false; // state of LED 1
boolean ledVal2 = false; // state of LED 2
boolean ledVal3 = false; // state of LED 3
boolean ledVal4 = false; // state of LED 4

//=================================================

void setup()
{
 // Set button input pin
 pinMode(buttonPin, INPUT);
 digitalWrite(buttonPin, HIGH );
 // Set LED output pins
 pinMode(ledPin1, OUTPUT);
 digitalWrite(ledPin1, ledVal1);
 pinMode(ledPin2, OUTPUT);
 digitalWrite(ledPin2, ledVal2);
 pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin3, ledVal3);
 pinMode(ledPin4, OUTPUT);
digitalWrite(ledPin4, ledVal4);
}

void loop()
{
 // Get button event and act accordingly
 int b = checkButton();
 if (b == 1) clickEvent();
 if (b == 2) doubleClickEvent();
 if (b == 3) holdEvent();
 if (b == 4) longHoldEvent();
}

//=================================================
// Events to trigger by click and press+hold

void clickEvent() {
 ledVal1 = !ledVal1;
 digitalWrite(ledPin1, ledVal1);
}

void doubleClickEvent() {
 ledVal2 = !ledVal2;
 digitalWrite(ledPin2, ledVal2);
}

void holdEvent() {
 ledVal3 = !ledVal3;
 digitalWrite(ledPin3, ledVal3);
}

void longHoldEvent() {
 ledVal4 = !ledVal4;
 digitalWrite(ledPin4, ledVal4);
}
/*
MULTI-CLICK: One Button, Multiple Events
 
Oct 12, 2009
Run checkButton() to retrieve a button event:
 Click
 Double-Click
 Hold
 Long Hold
*/

// Button timing variables
int debounce = 20; // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250; // max ms between clicks for a double click event
int holdTime = 2000; // ms hold period: how long to wait for press+hold event
int longHoldTime = 5000; // ms long hold period: how long to wait for press+hold event

// Other button variables
boolean buttonVal = HIGH; // value read from button
boolean buttonLast = HIGH; // buffered value of the button's previous state
boolean DCwaiting = false; // whether we're waiting for a double click (down)
boolean DConUp = false; // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true; // whether it's OK to do a single click
long downTime = -1; // time the button was pressed down
long upTime = -1; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false; // when held, whether to wait for the up event
boolean holdEventPast = false; // whether or not the hold event happened already
boolean longHoldEventPast = false;// whether or not the long hold event happened already

int checkButton()
{
int event = 0;
 // Read the state of the button
 buttonVal = digitalRead(buttonPin);
 // Button pressed down
 if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce) {
 downTime = millis();
 ignoreUp = false;
 waitForUp = false;
 singleOK = true;
 holdEventPast = false;
 longHoldEventPast = false;
 if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true) DConUp = true;
 else DConUp = false;
 DCwaiting = false;
 }
 // Button released
 else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce) {
if (not ignoreUp) {
 upTime = millis();
 if (DConUp == false) DCwaiting = true;
 else {
 event = 2;
 DConUp = false;
 DCwaiting = false;
 singleOK = false;
 }
 }
 }
 // Test for normal click event: DCgap expired
 if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true) {
 event = 1;
 DCwaiting = false;
 }
 // Test for hold
 if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
 // Trigger "normal" hold
 if (not holdEventPast) {
 event = 3;
 waitForUp = true;
 ignoreUp = true;
 DConUp = false;
 DCwaiting = false;
 //downTime = millis();
 holdEventPast = true;
 }
 // Trigger "long" hold
 if ((millis() - downTime) >= longHoldTime) {
 if (not longHoldEventPast) {
 event = 4;
 longHoldEventPast = true;
 }
 }
 }
 buttonLast = buttonVal;
 return event;
}

Below is a link off the button running with the sketch uploaded
http://s414.beta.photobucket.com/user/charged_z/media/IMG_3512.mp4.html


my problem is i would like to be able to scroll between the acc and on mode with separate clicks.
first press ACC LED turns on
second press turns off ACC and the ON LED switches on
third press will switch the button off
press and hold will switch the engine on and off.
could someone please help me sort this out.
Regards James


Moderator edit: [code] [/code] tags added.
« Last Edit: November 13, 2012, 02:16:42 am by Coding Badly » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're not really using buttons are you?

Quote
first press ACC LED turns on
second press turns off ACC and the ON LED switches on
third press will switch the button off
press and hold will switch the engine on and off.
How do you switch a button off?
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4803
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Momentary switch?
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 146
Posts: 5469
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
first press ACC LED turns on
second press turns off ACC and the ON LED switches on
third press will switch the button off
press and hold will switch the engine on and off.
You're not really using buttons are you?

I dunno...but that's an awful user interface design! Good taste prevents me from helping in this project.


Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys thanks for the replys!!!
It is a momentary switch im using, if this sketch is not 2 user friendly I'd be happy to try and make my own I just thought that this would be a nice base to start with.
Regards James
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

anyone got any suggestions???
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4803
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An engine cut-off that takes a second or more to effect is probably -a very bad idea-. I hope it doesn't take a lawyer in court to get that across. What you modify, you may become some or all responsible for.



Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: