Hi
I've done a function that detects if a button is pressed a single time or held for 3s.
The function is called multiply times to check if the button state has change (HIGH/LOW)
It works good when only being called from one button, but I need to have three different buttons (I call them Select, Up & Down)
The function has 2 variables that holds information for the next function-call. These two variables, needs to be used/updated only by the button that called the function. I thinks its buttonLastState and buttonMode that makes multiply buttons a problem.
So how do I save their values to the next run from a specific button?
My code:
const int upButton=5;
const int downButton=4;
const int selButton=3;
const int ledPhs=10; //Photos
const int ledInt=9; //Interval
const int ledBlb=8; //Bulb
const int ledStd=7; //StartDelay
const int ledSht=6; //Shoot
const int latchPin=12; //latch ST_CP of 74HC595
const int clockPin=13; //latch SH_CP of 74HC595
const int dataPin=11; //latch DS of 74HC595
//Variables for buttonfunction
int button=0;
//int buttonState=0;
int buttonPos=0;
int buttonMode=0; //0=LOW, 1=single push, 2=hold for 3s
//int buttonLastMode=0;
int buttonHold=0;
int buttonLastState=LOW;
long previousMillis=0;
long buttonHighMillis=0;
//For select button
int selButtonMode=0;
//int selButtonState=0;
//Variables for Up/down button
int upButtonMode=0;
int downButtonMode=0;
//Göra hold till en funktion? Behövs ju för up och down också!
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(selButton, INPUT);
pinMode(ledPhs, OUTPUT);
pinMode(ledInt, OUTPUT);
pinMode(ledBlb, OUTPUT);
pinMode(ledStd, OUTPUT);
pinMode(ledSht, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop(){
//select button
selButtonMode=buttonPress(digitalRead(selButton), 500, 3000);
if(selButtonMode==1){
Serial.println("Select singlepress");
}
else if(selButtonMode==2){
Serial.println("Select hold3s");
}
//up button, if commenting out this upButton-section, the code works good.
// As I see it, this next line updates variables that needs to be owned by selectButton's function-call
upButtonMode=buttonPress(digitalRead(upButton), 500, 3000);
if(upButtonMode==1){
Serial.println("Up singlepress");
}
else if(upButtonMode==2){
Serial.println("Up hold3s");
}
/*
//down button, inactivated, need to have selectButton working with upButton first
downButtonMode=buttonPress(digitalRead(downButton), 500, 3000);
if(downButtonMode==1){
Serial.println("Down singlepress");
}
else if(downButtonMode==2){
Serial.println("Down hold3s");
}
*/
}
//Function for detecting different types of buttonPress.
// buttonPress (int HIGH/LOW, int 500ms debounce, int 3000ms hold)
int buttonPress(int buttonState, int buttonInterval, int buttonHoldInterval){
unsigned long currentMillis = millis();
if(buttonState==HIGH && buttonLastState==LOW){
buttonHighMillis=millis();
}
else if(buttonState==HIGH && buttonLastState==HIGH){
if((millis()-buttonHighMillis)>buttonHoldInterval && buttonMode<2){
buttonMode=2;
}
else if((millis()-buttonHighMillis)>buttonHoldInterval && buttonMode==2){
buttonMode=3;
}
}
else if(buttonState==LOW && buttonLastState==HIGH){
currentMillis=millis();
if(currentMillis-buttonInterval<previousMillis && buttonMode<2){
buttonMode=1;
}
}
else if(buttonState==LOW && buttonLastState==LOW){
buttonMode=0;
}
buttonLastState=buttonState;
previousMillis=currentMillis;
return buttonMode;
}
}
Best Regards
Niclas Gustafsson