// Inclusione del file di intestazione della libreria JC_Button
#include <JC_Button.h>
#include "multbuttons.h"
// Definizione di costanti simboliche relative ai pin a cui sono connessi i pulsanti tattili.
#define PIN_YELLOW_BTN 2
#define PIN_GREY_BTN 3
#define PIN_BLUE_BTN 4
#define PIN_GREEN_BTN 5
#define PIN_RED_BTN 6
// Definizione di costanti simboliche relative ai pin a cui sono connessi i leds
#define YELLOW_LED 11
#define GREY_LED 7
#define BLUE_LED 9
#define GREEN_LED 10
#define RED_LED 8
#define LEN_OF(arr) sizeof(arr) / sizeof(arr[0])
// Dichiarazione di oggetti di classe (o tipo) Button offerto dalla libreria JC_Button.
// Un oggetto per pulsante, con pull-up interna abilitata.
Button yellowBtn(PIN_YELLOW_BTN);
Button greyBtn(PIN_GREY_BTN);
Button blueBtn(PIN_BLUE_BTN);
Button greenBtn(PIN_GREEN_BTN);
Button redBtn(PIN_RED_BTN);
Button buttons[] = {
Button(PIN_YELLOW_BTN),
Button(PIN_GREY_BTN),
Button(PIN_BLUE_BTN),
Button(PIN_GREEN_BTN)
};
//MButton yellowMBtn(PIN_YELLOW_BTN);
MButton yellowMBtn;
void printMessage(char *color) {
Serial.print(color);
Serial.println(" button was pressed!");
}
/*
void invertLedState(uint8_t pinLed, bool printState = false)
Inverte lo stato di pinLed e stampa opzionale lo stato del led.
Per stampare lo stato printState deve essere true, es:
invertLedState(YELLOW_LED, true);
Stampa: Yellow Led is ON state!
*/
void invertLedState(uint8_t pinLed, bool printState = false) {
bool currentLedState = digitalRead(pinLed);
digitalWrite(pinLed, !currentLedState);
if (printState) {
char *colorStr = nullptr;
char *ledState[] = {"OFF", "ON"};
switch (pinLed) {
case YELLOW_LED:
colorStr = "Yellow";
break;
case GREY_LED:
colorStr = "Grey";
break;
case BLUE_LED:
colorStr = "Blue";
break;
case GREEN_LED:
colorStr = "Green";
break;
case RED_LED:
colorStr = "Red";
break;
}
if (colorStr != nullptr) {
Serial.print(colorStr);
Serial.print(" Led is ");
Serial.print(ledState[!currentLedState]);
Serial.println(" state!");
}
}
}
void setup() {
Serial.begin(115200);
Serial.println(LEN_OF(buttons));
for (uint8_t i=0; i<LEN_OF(buttons); i++)
buttons[i].begin();
// Inizializza oggetti pulsanti tramite il metodo begin().
yellowBtn.begin();
greyBtn.begin();
blueBtn.begin();
greenBtn.begin();
redBtn.begin();
yellowMBtn.begin(PIN_YELLOW_BTN);
// Set pins as output to control led
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREY_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
}
// Example zero called in loop from case(0) of switch(nExample)
void exampleWasPressed() {
// Update buttons State
yellowBtn.read();
greyBtn.read();
blueBtn.read();
greenBtn.read();
redBtn.read();
uint8_t com;
com |= yellowBtn.read() << 1 | greyBtn.read() << 0;
// execute action polling button state of each object
if (yellowBtn.wasPressed()) {
if (com == 3) {
Serial.println(com);
} else {
// execut action if yellowBtn was pressed
printMessage("Yellow");
invertLedState(YELLOW_LED, true);
}
} else if (greyBtn.wasPressed() && com == 1) {
// execut action
printMessage("Grey");
invertLedState(GREY_LED, true);
} else if (blueBtn.wasPressed()) {
// execut action
printMessage("Blue");
invertLedState(BLUE_LED, true);
} else if (greenBtn.wasPressed()) {
// execut action
printMessage("Green");
invertLedState(GREEN_LED, true);
} else if (redBtn.wasPressed()) {
// execut action
printMessage("Red");
invertLedState(RED_LED, true);
}
}
void loop() {
const uint8_t nExample = 0;
switch (nExample) {
case 0:
exampleWasPressed();
break;
case 1:
break;
}
}