#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Définition de la taille de l'écran OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Déclaration de l'adresse I2C de l'écran OLED et de la broche de réinitialisation
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Broche pour le bouton
const int buttonPin = 6; // Broche à laquelle le bouton est connecté
const int pin1 = 3; // Broche connectée à la résistance de 1kΩ
const int pin2 = 4; // Broche connectée à la résistance de 10kΩ
const int analogPin = A0; // Broche analogique pour lire la tension
// Définir l'action pour déterminer quel cas afficher (1 à 16)
int action = 1; // Définir l'action à 1 pour appeler le case 1
void setup() {
// Initialisation de l'écran OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adresse I2C 0x3C par défaut
Serial.println(F("Échec de l'allumage de l'écran OLED"));
for (;;); // Boucle infinie si l'écran ne démarre pas
}
// Configurer la broche du bouton comme entrée avec pull-up interne
pinMode(buttonPin, INPUT_PULLUP);
// Initialisation du port série (pour débogage)
Serial.begin(9600);
// Effacer l'affichage et afficher le message initial
display.clearDisplay();
displayMessage();
}
float lireResistance(int pinOutput, int pinAnalog, int knownResistance) {
pinMode(pinOutput, OUTPUT); // Configurer la broche de sortie comme OUTPUT
digitalWrite(pinOutput, HIGH); // Régler la broche de sortie sur HIGH
pinMode(pinAnalog, INPUT); // Configurer la broche d'entrée comme INPUT
delay(100); // Attendre que la tension se stabilise
// Lire la tension sur la broche analogique
int lecture = analogRead(pinAnalog);
float tension = (lecture * 5.0) / 1023.0; // Convertir en tension (5V référence)
// Calcul de la résistance mesurée
float resistance = knownResistance * (5.0 / tension - 1);
// Réinitialiser les broches
digitalWrite(pinOutput, LOW); // Réinitialiser la broche de sortie sur LOW
return resistance;
}
void loop() {
// Lire l'état du bouton
int buttonState = digitalRead(buttonPin);
// Si le bouton est pressé (état LOW), lancer le test de l'action
if (buttonState == LOW) {
// Effacer l'affichage avant de dessiner
display.clearDisplay();
display.display();
delay(1000);
// Mesure de la résistance de 1 kΩ sur Pin 3
float resistance1 = lireResistance(pin2, analogPin, 1000); // 1 kΩ résistance connue
// Mesure de la résistance de 10 kΩ sur Pin 4
float resistance2 = lireResistance(pin1, analogPin, 10000); // 10 kΩ résistance connue
// Afficher les résultats
Serial.print("Résistance mesurée pour 1 kΩ : ");
Serial.print(resistance1);
Serial.println(" ohms");
Serial.print("Résistance mesurée pour 10 kΩ : ");
Serial.print(resistance2);
Serial.println(" ohms");
if (resistance1 == 300 && resistance2 == 400) {
// Afficher le texte à gauche
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
if (action >= 12 && action <= 16) {
display.print("INV"); // Afficher "INVERSION" pour les cas 12 à 16
drawThickCircleWithCross(); // Dessiner un rond avec une croix à l'intérieur
}
if (action == 1) {
display.print("PASS"); // Afficher "INVERSION" pour les cas 12 à 16
drawThickCircleWithCheck(); // Dessiner un rond avec une coche à l'intérieur
}
else {
display.print("COUPE"); // Afficher "COUPURE" pour les autres cas
drawThickCircleWithCross(); // Dessiner un rond avec une croix à l'intérieur
}
// Afficher "1 2 3" en haut et en bas à droite et dessiner les lignes correspondantes
displayTextWithLines();
// Mettre à jour l'affichage
display.display();
// Attendre que le bouton soit relâché pour éviter des lectures multiples
while (digitalRead(buttonPin) == LOW);
}
}
}
// Fonction pour afficher le texte "1 2 3" en haut et en bas à droite et dessiner des lignes
void displayTextWithLines() {
// Afficher "1 2 3" en haut à droite
display.setTextSize(2); // Augmenter la taille du texte
display.setCursor(SCREEN_WIDTH - 60, 0); // Ajuster le curseur pour le texte plus grand
display.print("1 2 3");
// Afficher "M 1 2" en bas à droite
display.setCursor(SCREEN_WIDTH - 60, SCREEN_HEIGHT - 16); // Ajuster pour le texte plus grand
display.print("1 2 3");
// Dessiner les lignes en fonction de l'action sélectionnée
switch (action) {
case 1:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 0, 0, 0); // Lignes droites pour "1", "2", "3"
break;
case 2:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 47, 0, -47); // "M" à "3", "2" à "2", "3" à "1"
break;
case 3:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 23, -23, 0); // "1" à "2", "2" à "1", "3" à "3"
break;
case 4:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 0, 24, -24); // "1" à "1", "2" à "3", "3" à "2"
break;
case 5:
drawThickLines(SCREEN_HEIGHT / 2, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 0, 0, 0); // "1" à moitié, "2" à "2", "3" à "3"
break;
case 6:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 20, 0, 0, 0); // "1" à "1", "2" à moitié, "3" à "3"
break;
case 7:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT / 2, 0, 0, 0); // "1" à "1", "2" à "2", "3" à moitié
break;
case 8:
drawThickLines(SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2, 0, 0, 0); // "1" à moitié, "2" à moitié, "3" à moitié
break;
case 9:
drawThickLines(SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT - 20, 0, 0, 0); // "1" à moitié, "2" à moitié, "3" à "3"
break;
case 10:
drawThickLines(SCREEN_HEIGHT / 2, SCREEN_HEIGHT - 20, SCREEN_HEIGHT / 2, 0, 0, 0); // "1" à moitié, "2" à "2", "3" à moitié
break;
case 11:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2, 0, 0, 0); // "1" à "1", "2" à moitié, "3" à moitié
break;
case 12:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 23, 24, -47); // "1" à "2", "2" à "3", "3" à "1"
break;
case 13:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 47, -23, -24); // "1" à "3", "2" à "1", "3" à "2"
break;
case 14:
drawThickLines(SCREEN_HEIGHT / 2, SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, 0, 24, -24); // "1" à moitié, "2" à "3", "3" à "2"
break;
case 15:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT - 20, SCREEN_HEIGHT / 2, 23, -23, 0); // "1" à "2", "2" à "1", "3" à moitié
break;
case 16:
drawThickLines(SCREEN_HEIGHT - 20, SCREEN_HEIGHT / 2, SCREEN_HEIGHT - 20, 47, 0, -47); // "1" à "3", "2" à moitié, "3" à "1"
break;
default:
Serial.println(F("Action non reconnue."));
break;
}
}
// Fonction générique pour dessiner des lignes épaisses en fonction des coordonnées de fin
void drawThickLines(int y1_end, int y2_end, int y3_end, int x1_end, int x2_end, int x3_end) {
// Dessiner des lignes épaisses pour "M"
for (int i = -1; i <= 1; i++) {
display.drawLine(SCREEN_WIDTH - 55 + i, 16, SCREEN_WIDTH - 55 + i + x1_end, y1_end, SSD1306_WHITE);
}
// Dessiner des lignes épaisses pour "1"
for (int i = -1; i <= 1; i++) {
display.drawLine(SCREEN_WIDTH - 32 + i, 16, SCREEN_WIDTH - 32 + i + x2_end, y2_end, SSD1306_WHITE);
}
// Dessiner des lignes épaisses pour "2"
for (int i = -1; i <= 1; i++) {
display.drawLine(SCREEN_WIDTH - 8 + i, 16, SCREEN_WIDTH - 8 + i + x3_end, y3_end, SSD1306_WHITE);
}
}
// Fonction pour dessiner un rond épais avec une croix à l'intérieur
void drawThickCircleWithCross() {
int x = 30; // Position du centre du cercle
int y = 45;
int radius = 15;
// Dessiner le cercle épais
for (int i = -1; i <= 1; i++) {
display.drawCircle(x, y, radius + i, SSD1306_WHITE);
}
// Dessiner la croix épaisse
for (int i = -1; i <= 1; i++) {
display.drawLine(x - radius / 2 + i, y - radius / 2, x + radius / 2 + i, y + radius / 2, SSD1306_WHITE);
display.drawLine(x - radius / 2 + i, y + radius / 2, x + radius / 2 + i, y - radius / 2, SSD1306_WHITE);
}
}
// Fonction pour dessiner un rond épais avec une coche à l'intérieur
void drawThickCircleWithCheck() {
int x = 30; // Position du centre du cercle
int y = 45;
int radius = 15;
// Dessiner le cercle épais
for (int i = -1; i <= 1; i++) {
display.drawCircle(x, y, radius + i, SSD1306_WHITE);
}
// Dessiner la coche épaisse
for (int i = -1; i <= 1; i++) {
display.drawLine(x - radius / 3 + i, y, x, y + radius / 3, SSD1306_WHITE);
display.drawLine(x + i, y + radius / 3, x + radius / 2, y - radius / 3, SSD1306_WHITE);
}
}
// Fonction pour afficher le message "TESTER XLR" et "PRESS TO BEGIN"
void displayMessage() {
display.setTextSize(2); // Taille du texte pour "TESTER XLR"
display.setTextColor(SSD1306_WHITE);
// Calculer la position pour centrer "TESTER XLR"
int16_t x1, y1;
uint16_t width, height;
display.getTextBounds("TESTER XLR", 0, 0, &x1, &y1, &width, &height);
display.setCursor((SCREEN_WIDTH - width) / 2, 10); // Centrer horizontalement et verticalement
display.print("TESTER XLR");
display.setTextSize(1); // Taille du texte pour "PRESS TO BEGIN"
display.getTextBounds("PRESS TO BEGIN", 0, 0, &x1, &y1, &width, &height);
display.setCursor((SCREEN_WIDTH - width) / 2, 40); // Centrer horizontalement
display.print("PRESS TO BEGIN");
// Mettre à jour l'affichage
display.display();
}