// definitie van de statussen
#define BTN_SETUP 0
#define BTN_READ 1
#define BTN_START_TIMER 2
#define BTN_WACHT 3
#define BTN_VOLDOENDE_INGEDRUKT 4
#define BTN_INGEDRUKT 5
#define BTN_LOSGELATEN 6
//declaratie van de pinnen
#define PIN_BUT1 7
#define PIN_BUT2 6
#define PIN_LED0 13
#define PIN_LED1 10
//declaratie constanten
#define INTERVAL 10 //tijdsduur in msec
#define MAX_AANTAL_BUTTONS 8
//declaratie variabelen
typedef struct {
byte btnState;
unsigned long timer;
bool toggle;
byte pin;
byte btnOldState;
} Button;
//Button but1;
Button buttonlijst[MAX_AANTAL_BUTTONS] ;
//Declaratie van de fucnties
void printButton ( Button knop);
void addButton(byte pinNr, byte btnNr);
void checkButtons();
Button getButton(byte buttonNr);
void setup( ) {
pinMode(PIN_BUT1, INPUT );
pinMode(PIN_BUT2, INPUT);
pinMode(PIN_LED0, OUTPUT);
pinMode(PIN_LED1, OUTPUT);
Serial.begin(115200);
addButton(7, 0);
addButton(6, 1);
}
void loop( ) {
checkButtons();
// getButton(1).toggle;
digitalWrite(PIN_LED1, getButton(1).toggle);
digitalWrite(PIN_LED0, getButton(0).toggle);
}
void addButton(byte pinNr, byte btnNr) {
Button but; // maak een nieuwe knop met naam 'but'
// geef nu een pinNr, een timer, een btnstate en een buttonoldstate aan deze nieuwe button
but.pin = pinNr;
but.timer = millis();
but.btnState = BTN_SETUP;
but.btnOldState = BTN_SETUP;
but.toggle = false;
buttonlijst[btnNr] = but;
}
void checkButtons() {
for (int i = 0; i < sizeof(buttonlijst) / sizeof(Button); i++)
{ if (buttonlijst[i].btnState != buttonlijst[i].btnOldState) {
Serial.print( " de status is ");
Serial.println (buttonlijst[i].btnState);
buttonlijst[i].btnOldState = buttonlijst[i].btnState;
}
switch (buttonlijst[i].btnState) {
case BTN_SETUP://0
buttonlijst[i].btnState = BTN_READ;
break;
case BTN_READ : //1
if (digitalRead(buttonlijst[i].pin) == LOW) {
buttonlijst[i].btnState = BTN_START_TIMER;
}
break;
case BTN_START_TIMER: //2
buttonlijst[i].timer = millis();
buttonlijst[i].btnState = BTN_WACHT;
break;
case BTN_WACHT: //3
if ( digitalRead(buttonlijst[i].pin) == HIGH) {
buttonlijst[i].btnState = BTN_SETUP;
} else {
if ((millis() - buttonlijst[i].timer) > INTERVAL) {
buttonlijst[i].btnState = BTN_VOLDOENDE_INGEDRUKT;
}
}
break;
case BTN_VOLDOENDE_INGEDRUKT://4
buttonlijst[i].toggle = !buttonlijst[i].toggle;
buttonlijst[i].btnState = BTN_INGEDRUKT;
break;
case BTN_INGEDRUKT: //5
if ( digitalRead(buttonlijst[i].pin) == HIGH) {
buttonlijst[i].btnState = BTN_LOSGELATEN;
}
break;
case BTN_LOSGELATEN: //6
buttonlijst[i].btnState = BTN_SETUP;
break;
default:
Serial.println("er is een foute status opgetreden");
break;
}
}
}
void printButton (Button knop) {
Serial.println(knop.btnState);
Serial.println(knop.pin);
}
Button getButton(byte buttonNr) {
return buttonlijst[buttonNr];
}