/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-blink-multiple-led
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-button-long-press-short-press
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-serial-monitor
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-blink-number-of-times
+ blink LED 10 times and then off
+ without using delay() function. This is a non-blocking example
https://github.com/RalphBacon/224-Superior-Serial.print-statements/tree/main
*/
#define DEBUG 0
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
#include <ezButton.h>
#include <ezLED.h> // ezLED library by ArduinoGetStarted.com
#define SHORT_PRESS_TIME 1000 // 1000 milliseconds
#define LONG_PRESS_TIME 1000 // 1000 milliseconds
#define NUM_LED 10
#define Led_Status_Pin 32
#define Button_ONOFF_Pin 12
#define Buzzer_Pin 15
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
long pressDuration = 0;
int BarGraph_Level = 0;
int Etat_BarGraph = 0;
#define PIN_LED_1 19 // The Arduino pin connected to LED 1 //OK
#define PIN_LED_2 21 // The Arduino pin connected to LED 2 //OK
#define PIN_LED_3 22 // The Arduino pin connected to LED 3 //OK
#define PIN_LED_4 2 // The Arduino pin connected to LED 3 //OK
#define PIN_LED_5 23 // The Arduino pin connected to LED 3 //OK
#define PIN_LED_6 4 // The Arduino pin connected to LED 3 //OK
#define PIN_LED_7 16 // The Arduino pin connected to LED 1 //OK
#define PIN_LED_8 17 // The Arduino pin connected to LED 2 //OK
#define PIN_LED_9 13 // The Arduino pin connected to LED 3 //OK
#define PIN_LED_10 18 // The Arduino pin connected to LED 3 //OK
ezButton Button_ONOFF(Button_ONOFF_Pin); // create ezButton object that attach to pin GPIO21
ezLED Led_Status(Led_Status_Pin); // create a LED object that attach to pin 9
bool Cmd_Rel_Batt = 0;
int Mask_Cmd_Rel_Batt = 32;
int Cmd_Rel_Batt_Masked = 0;
ezLED ledArray[NUM_LED] =
{
ezLED(PIN_LED_1), // create ezLED object that attach to pin PIN_LED_1
ezLED(PIN_LED_2), // create ezLED object that attach to pin PIN_LED_2
ezLED(PIN_LED_3), // create ezLED object that attach to pin PIN_LED_3
ezLED(PIN_LED_4), // create ezLED object that attach to pin PIN_LED_1
ezLED(PIN_LED_5), // create ezLED object that attach to pin PIN_LED_2
ezLED(PIN_LED_6), // create ezLED object that attach to pin PIN_LED_3
ezLED(PIN_LED_7), // create ezLED object that attach to pin PIN_LED_1
ezLED(PIN_LED_8), // create ezLED object that attach to pin PIN_LED_2
ezLED(PIN_LED_9), // create ezLED object that attach to pin PIN_LED_3
ezLED(PIN_LED_10), // create ezLED object that attach to pin PIN_LED_2
};
void F_On()
{
debugln(" => Turn LED ON");
Cmd_Rel_Batt = 1;
FCalcul_Cmde();
FBuzz_On();
Led_Status.blink(250, 750); // 250ms ON, 750ms OFF, blink immediately
}
void F_Off()
{
debugln(" => Turn LED OFF");
Cmd_Rel_Batt = 0;
FCalcul_Cmde();
FBuzz_Off();
Led_Status.cancel();
}
void F_Nack()
{
FBuzz_Nack();
Led_Status.blinkNumberOfTimes(50, 120, 3); // 250ms ON, 750ms OFF, repeat 3 times, blink immediately
}
void FSerial()
{
if (Serial.available())
{
// if there is data comming
String Serial_Command = Serial.readStringUntil('\n'); // read string until newline character
if (Serial_Command == "ON")
{
debug("echo: ");
debug(Serial_Command);
F_On();
}
else if (Serial_Command == "OFF")
{
debug("echo: ");
debug(Serial_Command);
F_Off();
}
else
{
debugln("Serial_Commandes: ON/OFF");
F_Nack();
}
}
}
void FCalcul_Cmde()
{
debugln(Cmd_Rel_Batt);
Cmd_Rel_Batt_Masked = Cmd_Rel_Batt * Mask_Cmd_Rel_Batt;
debugln(Cmd_Rel_Batt_Masked);
}
void FBuzz_On()
{
tone(Buzzer_Pin, 150);
delay (80);
noTone(Buzzer_Pin);
}
void FBuzz_Off()
{
tone(Buzzer_Pin, 100);
delay (120);
noTone(Buzzer_Pin);
}
void FBuzz_Nack()
{
tone(Buzzer_Pin, 200);
delay (40);
noTone(Buzzer_Pin);
delay (100);
tone(Buzzer_Pin, 200);
delay (40);
noTone(Buzzer_Pin);
}
void Cligno_BarGraph()
{
for (int i = 0; i < NUM_LED; i++)
ledArray[i].blinkNumberOfTimes(200, 200, 3);
/*ledArray[0].blinkNumberOfTimes(200, 200, 3);
ledArray[1].blinkNumberOfTimes(200, 200, 3);
ledArray[2].blinkNumberOfTimes(200, 200, 3);
ledArray[3].blinkNumberOfTimes(200, 200, 3);
ledArray[4].blinkNumberOfTimes(200, 200, 3);
ledArray[5].blinkNumberOfTimes(200, 200, 3);
ledArray[6].blinkNumberOfTimes(200, 200, 3);
ledArray[7].blinkNumberOfTimes(200, 200, 3);
ledArray[8].blinkNumberOfTimes(200, 200, 3);
ledArray[9].blinkNumberOfTimes(200, 200, 3);*/
}
void Test_Voyants()
{
Cligno_BarGraph();
}
void Confirmation_Voyants()
{
Cligno_BarGraph();
}
void FButton_ONOFF()
{
if (Button_ONOFF.isPressed())
{
pressedTime = millis();
isPressing = true;
isLongDetected = false;
}
if (Button_ONOFF.isReleased())
{
isPressing = false;
releasedTime = millis();
pressDuration = releasedTime - pressedTime;
Etat_BarGraph = ledArray[0].getState();
debug("Etat_BarGraph=");
debugln(Etat_BarGraph);
if (Etat_BarGraph == 3)
{
Confirmation_Voyants();
}
else
{
for (int i = 0; i < NUM_LED; i++)
{
ledArray[i].turnOFF(400);
}
}
if ( pressDuration < SHORT_PRESS_TIME )
{
debugln("A short press is detected");
debug(Etat_BarGraph);
for (int i = 0; i < NUM_LED; i++)
{
ledArray[i].turnOFF(400);
}
if(Cmd_Rel_Batt == 0)
{
F_Nack();
}
else if(Cmd_Rel_Batt == 1)
{
F_Off();
}
}
}
if (isPressing == true && isLongDetected == false)
{
pressDuration = millis() - pressedTime;
debug(pressDuration);
//Serial.println(pressDuration);
BarGraph_Level = map(pressDuration, 0, 1001, 0, NUM_LED);
debug(pressDuration);
debug(" BarGraph_Level= ");
debugln(BarGraph_Level);
/*Serial.println(" BarGraph_Level= ");
Serial.println(BarGraph_Level);*/
// loop over the LED array:
for (int thisLed = 0; thisLed < NUM_LED; thisLed++)
{
// if the array element's index is less than BarGraph_Level,
// turn the pin for this element on:
if (thisLed < BarGraph_Level)
{
ledArray[thisLed].turnON();
}
// turn off all pins higher than the BarGraph_Level:
else
{
ledArray[thisLed].turnOFF();
}
}
if ( pressDuration > LONG_PRESS_TIME )
{
debugln("A long press is detected");
isLongDetected = true;
F_On();
Confirmation_Voyants();
debug(ledArray[0].getState());
}
}
}
void setup()
{
Serial.begin(115200);
debugln("Debugging is enabled.\n");
debugln("Serial_Commandes: ON/OFF");
Button_ONOFF.setDebounceTime(50); // set debounce time to 50 milliseconds
Test_Voyants();
pinMode(Buzzer_Pin, OUTPUT);
}
void loop()
{
//initialisation
Button_ONOFF.loop(); // MUST call the loop() function first
Led_Status.loop(); // MUST call the Led_Status.loop() function in loop()
for (int i = 0; i < NUM_LED; i++)
ledArray[i].loop(); // MUST call the led.loop() function in loop()
FSerial();
FButton_ONOFF();
}ON/OFF