I'm having a hard time finding out why this will give the correct values for presses, but not releases and holds.
I ether missed an important concept or am having a timing issue.
//getvalueTesting
//4 buttons wired pins 2-5
//pin to tactile button to 330k to ground
//uses internal resistence to VCC
//
//goal is an expample that gathers debounced state information
//and combines every switch state within a time window
//into an int value
//ie, buttonstate[4]={1,2,5,1} --> int=1251
byte buttons[]={
2,3,4,5};
#define NUMBUTTONS sizeof(buttons)
#define BOUNCE 15// time to debounce
#define REGULAR 225
boolean regularEvent[]={
false,false,false,false};
#define HOLD 450
boolean holdEvent[]={
false,false,false,false};
#define LONG 900
boolean longEvent[]={
false,false,false,false};
#define WINDOW 1000
long past=-1;
long diff=-1;
int windowCount=0;
unsigned long releaseTime[]= {
-1,-1,-1,-1};
unsigned long pressTime[]= {
-1,-1,-1,-1};
boolean value[]= {
LOW,LOW,LOW,LOW};
boolean lastValue[]= {
LOW,LOW,LOW,LOW};
boolean pressed[]= {
false,false,false,false};
void setup()
{
Serial.begin(9600);
delay(2000);
//wait to bring up serial monitor
//------------input setting
for (byte set=0;set<NUMBUTTONS;set++)
{
//sets the button pins
pinMode(buttons[set],INPUT);
digitalWrite(buttons[set],HIGH);
}
}
void loop()
{
while(windowCount<=WINDOW)
// the time window is for the purpose of compiling a
// unique value from the button over a specified period of time
{
unsigned long currentTime=millis();
diff=currentTime-past;
windowCount+=diff;
for(int i=0;i<NUMBUTTONS;i++)
{
value[i]=digitalRead(buttons[i]);
if(value[i]==LOW && lastValue[i]==HIGH && (millis()-releaseTime[i]) > BOUNCE)
{
pressTime[i] = millis();
pressed[i]=true;
regularEvent[i]=false;
holdEvent[i]=false;
longEvent[i]=false;
Serial.print(i);
Serial.println("-pressed");
}
else if (value[i] == HIGH && lastValue[i] == LOW && (millis() - pressTime[i]) > BOUNCE)
{
if(pressed[i])
{
releaseTime[i] = millis();
Serial.print(value[i]);
//!! I only get the value 1-released
//inconceivable!
//I dont think this means what I think it means
Serial.println("-Release");
}
}
if (value[i]==LOW && (millis()-pressTime[i]) >= REGULAR)
{
if(!regularEvent[i])
{
//quick press
regularEvent[i]=true;
//!!only getting 0-regular press in output
Serial.print(value[i]);
Serial.println("-regular press");
}
if((millis()-pressTime[i])>= HOLD)
{
if (!holdEvent[i])
{
holdEvent[i]=true;
//!!only getting 0-hold event in output
Serial.print(value[i]);
Serial.println("-hold event");
}
}
}
lastValue[i]=value[i];
}
past=currentTime;
}
//--outside time window
Serial.print("window-");
Serial.println(windowCount);
windowCount=0;
}
with that code, here is what a sample of the output looks like this
window-1001
3-pressed
0-regular press
0-hold event
window-1001
1-Release
window-1001
fine, except the 0 and 1 button were never pressed
more description of purpose is in the code
I tried the bounce library... and I just found that I probably need get more familiar with the core concepts then try to take a shortcut. (and it was working inconsistently but that would be a topic for another thread)
Any ideas on why holds and releases result in static values?