// ======= ACS712 (Current Measurement) ========
#define ACS712_PIN A0
const float ACS712_SENSITIVITY = 66.0; // Sensibilité du capteur ACS712 en mV/A
const int numReadings = 10; // Nombre de lectures pour la moyenne
float readings[numReadings]; // Tableau pour stocker les lectures
int indexVal = 0; // indexVal pour parcourir le tableau
float total = 0; // Variable pour stocker la somme des lectures
// ======= INA219 (Voltage Measurement) =========
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
// ======== Adafruit SSD-1306 =====================
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "logo.h" // Include logo file
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// NeoPixel Library
#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define NUM_LEDS 10
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel ledStrip(NUM_LEDS, 6, NEO_GRB + NEO_KHZ800); // after initialization initialize led strip with number of led's defined in menu
void displayLogo() {
// Affiche le logo sur l'écran OLED pendant 3 secondes
display.clearDisplay();
display.drawBitmap(0, 0, epd_bitmap_logo_mmw_128x64, 128, 64, 1);
display.display();
delay(3000);
}
// =========== Encoder Pins =============
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
int counter = 0;
// Menu and other Variables
byte page = 1; // variable to hold page number
int ledNumber = 1; // Number of leds' to be tested
byte brightness = 10; // LED Brightness
bool automatic = true; // Selection test Mode automatic/manual
byte testNumber = 1; // test Number
byte prevPage = 0; // Variable to hold previous page number
bool ledInitialize = false;
void readEncoder() {
// This function will read encoder
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
if (page == 2) {
counter += 10; // increment counter with 10
counter = (counter > 100) ? 100 : counter;
} else
counter++; // Clockwise
if (page == 3)
automatic = false; // select manual mode
}
if (dtValue == LOW) {
if (page == 2) {
counter -= 10; // increment counter with 10
counter = (counter < 10) ? 10 : counter; // if counter get's below 0 set it to 0
} else {
counter--; // Counterclockwise
counter = (counter < 0) ? 0 : counter; // if counter get's below 0 set it to 0
}
if (page == 3)
automatic = true; // select automatic mode
}
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
void resetCounter() {
noInterrupts();
counter = 0;
interrupts();
}
void setup() {
Serial.begin(115200); // Initialize Serial communication at 115200 baud rate
// Initialize the INA219.
// By default the initialization will use the largest range (32V, 2A). However
// you can call a setCalibration function to change this range (see comments).
if (!ina219.begin()) {
Serial.println("Failed to find INA219 chip");
// while (1) { delay(10); }
}
// strip.begin();
// strip.show(); // Initialise toutes les LEDs à l'état éteint
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize LCD
display.setTextColor(WHITE, BLACK); //Sets the font display color
display.clearDisplay(); //cls
// Initialize encoder pins
pinMode(ENCODER_CLK, INPUT); // Set clock pin as input
pinMode(ENCODER_DT, INPUT); // Set data pin as input
pinMode(ENCODER_SW, INPUT_PULLUP); // Set enconder button pin as input with pullup
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING); // Attach falling interrupt to clock pin
displayLogo(); // Display logo on OLED
delay(3000); // Wait for 3 seconds
}
void loop() {
if (prevPage != page) {
// If previous page is not equal to current page update OLED
resetCounter(); // reset counter value
display.clearDisplay(); //cls
display.display();
display.setTextSize(1);
if (page == 1) {
counter = 1;
display.setCursor(14, 0);
display.print("Select # LED's");
display.setTextSize(2);
display.setCursor(50, 25);
display.print(ledNumber);
ledInitialize = false;
} else if (page == 2) {
counter = 10;
display.setCursor(14, 0);
display.print("Select Brightness");
display.setTextSize(2);
display.setCursor(50, 13);
display.print(brightness);
display.drawRoundRect(10, 32, 102, 9, 2, WHITE);
display.fillRect(11, 33, brightness, 7, WHITE);
} else if (page == 3) {
testNumber = 1; // Set test number to 1
display.setCursor(14, 0);
display.print("Select Test");
display.setCursor(18, 17);
display.println("Automatic");
display.setCursor(6, 17);
display.println(">");
display.setCursor(18, 35);
display.print("Manual");
// display.setCursor(6, 35); // arrow for manual mode
// display.print(">");
} else if (page == 4) {
if (automatic) {
display.setCursor(18, 18);
display.print("Starting Manual");
} else {
display.setCursor(12, 18);
display.print("Starting Automatic");
}
display.setCursor(55, 35);
display.print("Test");
display.display();
Adafruit_NeoPixel strip(10, LED_PIN, NEO_GRB + NEO_KHZ800); // In start initilize led strip with 10 leds
strip.begin();
strip.show(); // Initialise toutes les LEDs à l'état éteint
strip.setBrightness(map(brightness, 10, 100, 25, 255)); // 255 is the max brightness
chaseLEDs();
flashFirstLED();
Adafruit_NeoPixel ledStrip(ledNumber, LED_PIN, NEO_GRB + NEO_KHZ800); // after initialization initialize led strip with number of led's defined in menu
ledStrip.begin();
ledStrip.show(); // Initialise toutes les LEDs à l'état éteint
ledStrip.setBrightness(map(brightness, 10, 100, 25, 255)); // 255 is the max brightness
display.clearDisplay(); // clear display
display.setCursor(18, 18);
display.print("Initialized test");
display.setCursor(55, 35);
display.print("phases");
display.display();
startTests();
}
prevPage = page; // update prev page number
if (page < 3) {
display.setTextSize(1);
display.setCursor(0, 52);
Serial.println("calling");
display.print("Press Button for NEXT");
} else if (page == 3) {
display.setTextSize(1);
display.setCursor(0, 52);
display.print("Press Button to START");
}
display.display();
}
if (digitalRead(ENCODER_SW) == LOW) {
// check if encoder button was pressed
page += 1;
Serial.print("Change page");
Serial.println(page);
// page = (page > 3) ? 1 : page;
delay(100);
}
updateDisplay(); // udpate display
// float readCurrent = readCurrentFunc(); // Read current value
// float readVoltage = readVoltageFunc(); // Read voltage value from sensor
}
void updateDisplay() {
if (page == 1) {
ledNumber = getCounter();
display.setTextSize(2);
display.setCursor(50, 25);
display.print(ledNumber);
display.print(" ");
} else if (page == 2) {
brightness = getCounter();
display.fillRect(11, 33, brightness, 7, WHITE);
display.setTextSize(2);
display.setCursor(50, 13);
display.print(brightness);
display.print(" ");
} else if (page == 3) {
if (automatic) {
display.setCursor(6, 17);
display.println(">");
display.setCursor(6, 35); // arrow for manual mode
display.print(" ");
} else {
display.setCursor(6, 17);
display.println(" ");
display.setCursor(6, 35); // arrow for manual mode
display.print(">");
}
}
display.display(); // update display
}
void chaseLEDs() {
unsigned long startMillis = millis();
while (millis() - startMillis < 3000) {
for (int i = 0; i < 10; i++) {
strip.setPixelColor(i, 255, 0, 0); // RED color for chasing
strip.show();
delay(100);
// strip.setPixelColor(i, strip.Color(0, 0, 0)); // Turn off LED
}
for (int i = 9; i >= 0; i--) {
strip.setPixelColor(i, 0, 0, 0);
strip.show();
delay(100);
// strip.setPixelColor(i, strip.Color(0, 0, 0));
}
}
strip.clear();
}
void flashFirstLED() {
// unsigned long startMillis = millis();
// while (millis() - startMillis < 3000) {
for (byte j = 0; j < 2; j++){
for (int i = 0; i < 10; i++) {
strip.setPixelColor(i, 255, 0, 0); // RED color for chasing
strip.show();
delay(100);
// strip.setPixelColor(i, strip.Color(0, 0, 0)); // Turn off LED
}
for (int i = 9; i >= 0; i--) {
strip.setPixelColor(i, 0, 0, 0);
strip.show();
delay(100);
// strip.setPixelColor(i, strip.Color(0, 0, 0));
}
}
// }
strip.clear();
for (byte i = 0; i < 3; i++) {
strip.setPixelColor(0, 0, 255, 0); // GREEN color
strip.show();
delay(500);
strip.setPixelColor(0, 0, 0, 0); // Turn off LED
strip.show();
delay(500);
}
strip.clear();
}
void startTests() {
waitForNext(); // call the function to update display
display.setCursor(40, 22);
display.print("RED Test"); // Test 1 Red
display.display(); // update display
colorTest(strip.Color(255, 0, 0)); // Red
waitForNext(); // call the function to update display
display.setCursor(35, 22);
display.print("Green Test"); // Test 2 Green
display.display(); // update display
colorTest(strip.Color(0, 255, 0)); // Green
waitForNext(); // call the function to update display
display.setCursor(38, 22);
display.print("Blue Test"); // Test 2 Blue
display.display(); // update display
colorTest(strip.Color(0, 0, 255)); // Blue
waitForNext(); // call the function to update display
display.setCursor(35, 22);
display.print("White Test"); // Test 3 white
display.display(); // update display
colorTest(strip.Color(255, 255, 255)); // White
waitForNext(); // call the function to update display
display.setCursor(34, 22);
display.print("Purple Test"); // Test 1
display.display(); // update display
colorTest(strip.Color(128, 0, 128)); // Purple
// testNumber
}
void waitForNext() {
// in case of manual mode program has to wait for button press
display.clearDisplay(); // clear display
if ((testNumber == 1) && (!automatic)) {
display.setCursor(10, 22);
display.print("Validate to launch");
display.setCursor(34, 36);
display.print("the Tests");
display.display();
while (digitalRead(ENCODER_SW)) { ; } // wait for button press
testNumber = 2; // change test number not to show same message
} else if (!automatic) {
display.setCursor(10, 24);
display.print("Validate for Next");
display.setCursor(46, 38);
display.print("Test");
display.display();
}
if (!automatic) // in case of manual mode
while (digitalRead(ENCODER_SW)) { ; } // wait for button press
display.clearDisplay(); // clear display
display.setCursor(0, 0);
display.print("Voltage");
display.setCursor(85, 0);
display.print("Current");
display.setCursor(0, 52);
display.print("LEDs:");
display.print(ledNumber); // Display selectd number of leds
display.setCursor(68, 52);
display.print("Brigth:");
display.print(brightness); // Display selected brightness
display.display();
}
void colorTest(uint32_t color) {
display.setCursor(30, 36);
display.print("In Progress");
display.display();
// Calculate delay for each LED
int ledDelay = 5000 / ledNumber;
for (int i = 0; i < ledNumber; i++) {
float readCurrent = readCurrentFunc(); // Read current value
float readVoltage = readVoltageFunc(); // Read voltage value from sensor
ledStrip.setPixelColor(i, color);
ledStrip.show();
display.setCursor(7, 12);
display.print(readVoltage, 2);
display.setCursor(97, 12);
display.print(readCurrent, 2);
display.display();
delay(ledDelay);
}
delay(5000); // Pause for 5 seconds
// Turn off LEDs in reverse order quickly
for (int i = ledNumber - 1; i >= 0; i--) {
ledStrip.setPixelColor(i, 0);
ledStrip.show();
delay(50); // Quick turn off
}
display.setCursor(30, 36);
display.print(" ");
display.display();
display.setCursor(34, 36);
display.print("Finished");
display.display();
delay(2000); // wait for 2 second
}
float readVoltageFunc() {
// Lit le voltage du module INA219 et affiche la valeur sur l'écran OLED
float voltageINA219 = ina219.getBusVoltage_V();
// Serial.print("Voltage INA219: ");
// Serial.print(voltageINA219, 2);
// Serial.println(" V");
return voltageINA219;
}
float readCurrentFunc() {
// Lis la valeur analogique du capteur ACS712
int rawValueACS712 = analogRead(ACS712_PIN);
// Convertit la valeur analogique en tension en utilisant la référence de tension de l'Arduino (5V)
float voltageACS712 = (rawValueACS712 / 1023.0) * 5.0;
// Calcule le courant en A en utilisant la sensibilité du capteur ACS712
float currentACS712 = voltageACS712 / ACS712_SENSITIVITY;
// Ajoute la nouvelle lecture à la somme totale en soustrayant la lecture la plus ancienne du tableau
total = total - readings[indexVal];
readings[indexVal] = currentACS712;
total = total + readings[indexVal];
// Incrémente l'indexVal pour la prochaine lecture
indexVal = (indexVal + 1) % numReadings;
// Calcule la moyenne des lectures
float averageCurrent = total / numReadings;
// Affiche la moyenne du courant mesuré sur le moniteur série
// Serial.print("Courant ACS712 (moyenne) : ");
// Serial.print(averageCurrent, 2); // Affiche jusqu'à 2 décimales
// Serial.println(" A");
return averageCurrent;
}Loading
ssd1306
ssd1306