#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int outputs[] = { 13, 14, 27, 23, 16, 17, 18, 19 };
const int inputs[] = { 25, 26, 33, 34, 35, 36, 39, 32 };
const char* wireNames[] = { "A", "B", "C", "D", "E", "F", "G", "H" };
const char* wireNamesLower[] = { "a", "b", "c", "d", "e", "f", "g", "h" };
#define sys 12
int line = 0, column = 0; // Start at top left
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(sys, INPUT_PULLUP);
Serial.begin(115200);
delay(1000);
Serial.println("Testing Cable Connections...\n");
}
void loop() {
if (digitalRead(sys) == 0) {
lcd.clear();
line = 0;
column = 0;
testCable();
delay(3000);
test_diode();
delay(3000);
test_resistance();
delay(3000);
}
}
void testCable() {
for (int i = 0; i < 8; i++) {
pinMode(outputs[i], OUTPUT);
pinMode(inputs[i], INPUT);
}
int goodCount = 0;
int cutCount = 0;
int inversedCount = 0;
for (int i = 0; i < 8; i++) {
digitalWrite(outputs[i], LOW); // Initialize all outputs to LOW
}
for (int i = 0; i < 8; i++) {
digitalWrite(outputs[i], HIGH);
delay(100);
int detected = -1;
for (int j = 0; j < 8; j++) {
if (digitalRead(inputs[j]) == HIGH) {
detected = j;
break;
}
}
// Move cursor and print wire status
lcd.setCursor(column, line);
if (detected == -1) {
Serial.print("Wire ");
Serial.print(wireNames[i]);
Serial.println(": ❌ Cut or Disconnected");
lcd.print(wireNames[i]);
lcd.print(": Cut");
cutCount++;
} else if (detected == i) {
Serial.print("Wire ");
Serial.print(wireNames[i]);
Serial.println(": ✅ Correct");
lcd.print(wireNames[i]);
lcd.print(": OK");
goodCount++;
} else {
Serial.print("Wire ");
Serial.print(wireNames[i]);
Serial.print(": ⚠️ Inversed or Shorted -> Detected at ");
Serial.println(wireNamesLower[detected]);
lcd.print(wireNames[i]);
lcd.print("->");
lcd.print(wireNamesLower[detected]);
inversedCount++;
}
// Update cursor position
line++;
if (line > 3) { // LCD has only 4 rows, go back to row 0 and shift column
line = 0;
column += 10; // Shift right to next section
if (column > 10) {
column = 0; // Reset if it goes too far
}
}
delay(1000); // Short delay to allow user to see message before next
digitalWrite(outputs[i], LOW);
}
// Optional: Display summary at the end
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Good: ");
lcd.print(goodCount);
lcd.setCursor(0, 1);
lcd.print("Cut: ");
lcd.print(cutCount);
lcd.setCursor(0, 2);
lcd.print("Inversed: ");
lcd.print(inversedCount);
}
void test_diode() {
Serial.begin(115200); // Initialisation de la communication série
#define SHUNT_PIN1 13 // Première borne de la diode (anode)
#define SHUNT_PIN2 14 // Deuxième borne de la diode (cathode)
const float VREF = 3.3; // Tension de référence de l'ESP32
const int ADC_MAX = 4095; // Résolution ADC (12 bits)
const float SEUIL_DIODE = 0.2; // Seuil pour détecter le courant (0.7V pour diode classique, 0.2V pour Schottky)
int rawV1 = analogRead(SHUNT_PIN1);
int rawV2 = analogRead(SHUNT_PIN2);
// Convertir les valeurs ADC en tension
float V1 = (rawV1 * VREF) / ADC_MAX;
float V2 = (rawV2 * VREF) / ADC_MAX;
// Détermination du sens du courant
if (V1 > V2 + SEUIL_DIODE) {
Serial.println("➡️ Sens du courant : NORMAL (GPIO 13 ➝ GPIO 14)");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Sens courant:NORMAL ");
} else if (V2 > V1 + SEUIL_DIODE) {
Serial.println("⬅️ Sens du courant : INVERSE (GPIO 14 ➝ GPIO 13)");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Sens courant:INVERSE ");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Aucun courant");
Serial.println("⚠️ Aucun courant détecté");
}
}
void test_resistance() {
const float R2 = 10000.0; // Résistance R2 en ohms (10 kΩ)
const float Vin = 3.3; // Tension d'entrée en volts (ESP32 utilise 3.3V)
const int pinADC = 34; // Broche ADC 34 de l'ESP32
Serial.begin(115200); // Initialisation de la communication série
// Calibration et amélioration de l'ADC
analogReadResolution(12); // Définir la résolution à 12 bits (max pour ESP32)
analogSetPinAttenuation(pinADC, ADC_11db);
// Fonction pour lire la moyenne des valeurs ADC (10 échantillons pour réduire le bruit)
int adcValue = adcReadAvg(pinADC, 10);
// Conversion de la valeur ADC en tension
float VADC = (adcValue / 4095.0) * Vin; // ADC 12 bits → 4095 max
// Affichage des valeurs pour diagnostic
Serial.print("ADC Value: ");
Serial.println(adcValue); // Affiche la valeur brute lue par l'ADC
Serial.print("VADC: ");
Serial.print(VADC); // Affiche la tension calculée en volts
Serial.println(" V");
if (VADC == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Verify wiring");
Serial.println("Erreur : VADC est 0, vérifiez le câblage !");
return;
}
float R1 = (R2 * (Vin - VADC)) / VADC;
// Affichage de la valeur de R1
Serial.print("La valeur de R1 est : ");
Serial.print(R1);
Serial.println(" ohms");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("R1 : " + String(R1));
}
int adcReadAvg(int pin, int samples) {
int sum = 0;
for (int i = 0; i < samples; i++) {
sum += analogRead(pin);
delay(5); // Petit délai pour éviter les interférences
}
return sum / samples;
}