/*
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/tutorials/arduino-blink-multiple-led
https://arduinogetstarted.com/library/button/example/arduino-single-button-debounce
https://arduinogetstarted.com/tutorials/arduino-led-library
*/
#include <ezButton.h>
#include <ezLED.h> // ezLED library by ArduinoGetStarted.com
#define POTENTIOMETER_PIN 36 // ESP32 pin GPIO36 (ADC0) connected to Potentiometer pin
#define Led_Status_Pin 32
#define Button_Charge_Pin 12
#define Button_DeCharge_Pin 14
#define NUM_LED 10 // three LEDs
#define PIN_LED_1 7 // The Arduino pin connected to LED 1
#define PIN_LED_2 8 // The Arduino pin connected to LED 2 7 = D0
#define PIN_LED_3 15 // The Arduino pin connected to LED 3
#define PIN_LED_4 2 // The Arduino pin connected to LED 3
#define PIN_LED_5 0 // The Arduino pin connected to LED 3
#define PIN_LED_6 4 // The Arduino pin connected to LED 3
#define PIN_LED_7 16 // The Arduino pin connected to LED 1 8 = D1
#define PIN_LED_8 17 // The Arduino pin connected to LED 2
#define PIN_LED_9 5 // The Arduino pin connected to LED 3
#define PIN_LED_10 18 // The Arduino pin connected to LED 3
ezButton Button_Charge(Button_Charge_Pin); // create ezButton object that attach to pin GPIO21
ezButton Button_DeCharge(Button_DeCharge_Pin); // create ezButton object that attach to pin GPIO21
ezLED Led_Status(Led_Status_Pin); // create a LED object that attach to pin 9
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
};
int sensorReading_new = 0;
int sensorReading_old = 0;
int Etat_Charge = 0;
int ledLevel = 0;
void setup()
{
Serial.begin(9600);
Button_Charge.setDebounceTime(50); // set debounce time to 50 milliseconds
Button_DeCharge.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void FBarGraph()
{
for (int thisLed = 0; thisLed < NUM_LED; thisLed++)
ledArray[thisLed].cancel();
for (int thisLed = 0; thisLed < NUM_LED; thisLed++)
{
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel)
{
ledArray[thisLed].turnON();
}
// turn off all pins higher than the ledLevel:
else
{
ledArray[thisLed].turnOFF();
}
}
if (Etat_Charge == 1) // => OK
{
if (ledLevel == 10)
{
ledArray[9].blink(200, 200); // 500ms ON, 500ms OFF, blink immediately
}
else
{
ledArray[ledLevel].blink(200, 200); // 500ms ON, 500ms OFF, blink immediately
}
}
else if (Etat_Charge == 2) // => OK
{
if (ledLevel == 0)
{
ledArray[0].blink(200, 200); // 500ms ON, 500ms OFF, blink immediately
}
else if (ledLevel == 1)
{
ledArray[0].blink(200, 200); // 500ms ON, 500ms OFF, blink immediately
}
else
{
ledArray[ledLevel - 2].blink(200, 200); // 500ms ON, 500ms OFF, blink immediately
}
}
}
void loop()
{
Button_Charge.loop(); // MUST call the loop() function first
Button_DeCharge.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()
sensorReading_new = analogRead(POTENTIOMETER_PIN);
// map the result to a range from 0 to the number of LEDs:
ledLevel = map(sensorReading_new, 0, 4095, 0, NUM_LED);
if (sensorReading_new != sensorReading_old)
{
Serial.print("sensorReading= ");
Serial.print(sensorReading_new);
Serial.print(" ledLevel= ");
Serial.println(ledLevel);
sensorReading_old = sensorReading_new;
FBarGraph();
}
if (Button_Charge.isPressed())
{
Etat_Charge = 1;
Serial.println("Charge");
FBarGraph();
}
if (Button_DeCharge.isPressed())
{
Etat_Charge = 2;
Serial.println("DeCharge");
FBarGraph();
}
}
charge - décharge