/*********
*********/
// niet gebruiken: GPI00. (=flash)
// niet gebruiken: GPIO6, GPIO7, GPIO8, GPIO9, GPIO10, en GPIO11
#include <ezButton.h>
const int boardLedPin = 2; // the number of the LED pin
const int GPIO0 = 0; //button 1 input, tevens FLASH
const int GPIO2 = 2; //button 2 input
const int GPIO4 = 4; //button 3 input
const int GPIO5 = 5; //button 4 input
const int GPIO12 = 12; //buton 1 LED
const int GPIO13 = 13; //buton 2 LED
const int GPIO14 = 14; //buton 3 LED
const int GPIO16 = 16; //buton 4 LED
const int BUTTON_1 = GPIO0;
const int BUTTON_2 = GPIO2;
const int BUTTON_3 = GPIO4;
const int BUTTON_4 = GPIO5;
const int BUTTON_CTRL = 15;
const int LED_1 = GPIO12;
const int LED_2 = GPIO13;
const int LED_3 = GPIO14;
const int LED_4 = GPIO16;
const int GPIO15 = 15; // must be GND ?
int state = 0;
int prev_state = 0;
int buttonIndex = 0;
int buttonStates[4]; // To hold button states
unsigned long stateOnAtMillis[4]; // the timing to activate states, 0=none
unsigned long stateOffAtMillis[4]; // the timing to deactivate (blink) states, 0=none
const int blinking_ed_interval = 1500;
ezButton button1(BUTTON_1); // create ezButton object for button 1
ezButton button2(BUTTON_2); // create ezButton object for button 2
ezButton button3(BUTTON_3); // create ezButton object for button 3
ezButton button4(BUTTON_4); // create ezButton object for button 4
ezButton buttonCTRL(BUTTON_CTRL); // create ezButton object for button 4
void setup() {
Serial.begin(9600);
// initialize GPIO's.
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
button1.setDebounceTime(50); // set debounce time to 50 milliseconds
button2.setDebounceTime(50); // set debounce time to 50 milliseconds
button3.setDebounceTime(50); // set debounce time to 50 milliseconds
button4.setDebounceTime(50); // set debounce time to 50 milliseconds
buttonCTRL.setDebounceTime(50); // set debounce time to 50 milliseconds
state = -3;
Serial.println("Setup done.");
}
// the loop function runs over and over again forever
void loop() {
button1.loop(); // MUST call the loop() function first
button2.loop(); // MUST call the loop() function first
button3.loop(); // MUST call the loop() function first
button4.loop(); // MUST call the loop() function first
buttonCTRL.loop(); // MUST call the loop() function first
//show leds are working
if (state <0 ) {
state++;
digitalWrite(LED_1, HIGH);
delay(100);
digitalWrite(LED_2, HIGH);
delay(100);
digitalWrite(LED_3, HIGH);
delay(100);
digitalWrite(LED_4, HIGH);
delay(100);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
delay(250);
}
//combine buttons already pressed
int prev_pressed = 0;
for (int i=0; i<buttonIndex; i++)
prev_pressed = prev_pressed | buttonStates[i];
int buttons_pressed = readButtons() & (prev_pressed ^ 0x0F);
if (buttons_pressed > 0) {
//register the button pressed
if (buttonIndex < 4) {
buttonStates[buttonIndex] = buttons_pressed;
if (buttonIndex == 0) {
// showPressed(buttonStates[0], HIGH);
stateOnAtMillis[0] = millis(); //to activate LED of first button pressed
stateOffAtMillis[0] = 0; //and keep on
} else {
stateOnAtMillis[buttonIndex] = stateOnAtMillis[0] + buttonIndex *200; //to activate LED of second/next button pressed
stateOffAtMillis[buttonIndex] = stateOnAtMillis[buttonIndex] + 50; //and turn off after 100 millis
}
}
buttonIndex++;
state = buttons_pressed;
}
updateStatesOnOff(blinking_ed_interval);
if (state != prev_state) {
Serial.print("prev_pressed=");
Serial.println(prev_pressed);
Serial.print("buttons pressed:");
Serial.print(buttonStates[0]);
Serial.print(",");
Serial.print(buttonStates[1]);
Serial.print(",");
Serial.print(buttonStates[2]);
Serial.print(",");
Serial.println(buttonStates[3]);
Serial.print("state=");
Serial.println(state);
}
checkControl();
prev_state = state;
// delay(10);
}
int readButtons() {
int result = 0;
if(button1.isPressed())
result = 1;
if(button2.isPressed())
result = result + 2;
if(button3.isPressed())
result = result + 4;
if(button4.isPressed())
result = result + 8;
return result;
}
int showPressed(int actives, int onoff) {
if ((actives & 0x01) > 0)
digitalWrite(LED_1, onoff);
if ((actives & 0x02) > 0)
digitalWrite(LED_2, onoff);
if ((actives & 0x04) > 0)
digitalWrite(LED_3, onoff);
if ((actives & 0x08) > 0)
digitalWrite(LED_4, onoff);
return 0;
}
void updateStatesOnOff(int interval) {
unsigned long currentMillis = millis();
for (int i=0; i<buttonIndex; i++) {
if (stateOnAtMillis[i] > 0) {
if (stateOnAtMillis[i] < currentMillis) {
stateOnAtMillis[i] += interval; //set next
showPressed(buttonStates[i], HIGH);
}
}
if (stateOffAtMillis[i] > 0) {
if (stateOffAtMillis[i] < currentMillis) {
stateOffAtMillis[i] += interval; //set next
showPressed(buttonStates[i], LOW);
}
}
}
}
void checkControl() {
//check for control button to be pressed
if(buttonCTRL.isPressed()) {
if (buttonIndex > 0) {
buttonIndex--;
buttonStates[0] = buttonStates[1];
buttonStates[1] = buttonStates[2];
buttonStates[2] = buttonStates[3];
buttonStates[3] = 0;
stateOnAtMillis[0] = stateOnAtMillis[1];
stateOnAtMillis[1] = stateOnAtMillis[2];
stateOnAtMillis[2] = stateOnAtMillis[3];
stateOnAtMillis[3] = 0;
stateOffAtMillis[0] = 0;
stateOffAtMillis[1] = stateOffAtMillis[2];
stateOffAtMillis[2] = stateOffAtMillis[3];
stateOffAtMillis[3] = 0;
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
}
if (buttonIndex == 0) {
//reset, back to init
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
delay(250);
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_3, HIGH);
digitalWrite(LED_4, HIGH);
delay(100);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
delay(250);
for (int i=0; i<4; i++) {
buttonStates[i] = 0;
stateOnAtMillis[i] = 0;
stateOffAtMillis[i] = 0;
}
buttonIndex =0;
state = 0;
}
}
}