#include "src/Btn/Btn.h" #include #include #include #include #define SSD1306_NO_SPLASH #define SCREEN_WIDTH 96 #define SCREEN_HEIGHT 16 #define OLED_RESET -1 // Pas de reset matériel #define SCREEN_ADDRESS 0x3C // Adresse I2C (à confirmer avec un scanner I2C) #define LED_PIN 18 #define batVcheckActivatePin 42 #define batVcheckMesurePin 14 int bat_prct = 100; float coil_res = 1.1; float coil_pw = 17.8; Btn plusBtn(6, 50); Btn moinsBtn(7, 50); Btn fireBtn(17, 50); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { SerialUSB.begin(115200); // while (!SerialUSB); // Leonardo: wait for serial monitor SerialUSB.println("\nBoxmodFirmware"); pinMode(LED_PIN, OUTPUT); analogWrite(LED_PIN, 0); pinMode(batVcheckActivatePin, OUTPUT); digitalWrite(batVcheckActivatePin, HIGH); pinMode(batVcheckMesurePin, INPUT); if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { SerialUSB.println(F("Échec de l'initialisation de l'écran OLED")); // while (true); // Boucle infinie en cas d'échec } display.clearDisplay(); delay(500); display.setRotation(2); } void drawScreen() { display.clearDisplay(); display.setTextColor(SSD1306_WHITE); // battery display.setTextSize(1); display.setCursor(0, 0); display.print(String(bat_prct)+"%"); // display.drawRect(0, 0, 39, 69, SSD1306_WHITE); // Rectangle vide // display.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE); // Rectangle rempli // coil resistance display.setTextSize(1); display.setCursor(0, 8); // display.print(String(coil_res)+"Ω"); char coil_res_output[3]; // Adjust size accordingly sprintf(coil_res_output, "%.1f\xE9", coil_res); // Format float with 1 decimal place display.print(coil_res_output); // power display.setTextSize(2); display.setCursor(30, 0); // display.print(String(coil_pw)+"W"); char coil_pw_output[4]; // Adjust size accordingly sprintf(coil_pw_output, "%.1fw", coil_pw); // Format float with 1 decimal place display.print(coil_pw_output); display.display(); // Rafraîchir l'écran } void checkBatteryVoltage(){ // res bridge // Vout = Vin * (R2 / (R1 + R2)) // R2 = (Vout / Vin) * R1 // vout should be btw 1.5V & 2.1V // https://docs.arduino.cc/language-reference/en/functions/analog-io/analogReadResolution analogReadResolution(12); // btw 0 & 4095 // https://docs.arduino.cc/language-reference/en/functions/analog-io/analogReference/ analogReference(AR_INTERNAL2V23); digitalWrite(batVcheckActivatePin, LOW); int batv_mesure = analogRead(batVcheckMesurePin); digitalWrite(batVcheckActivatePin, HIGH); // SerialUSB.print("batv_mesure: "); // SerialUSB.print(batv_mesure); double batv = (batv_mesure * 2.23/4095.0)*2; // SerialUSB.print(", batv: "); // SerialUSB.print(batv); // SerialUSB.println("V"); // range of 3.4V to 4.1V bat_prct = (batv - 3.4) / (4.1 - 3.4) * 100; // Clamp the percentage to [0, 100] bat_prct = constrain(bat_prct, 0, 100); } void loop() { if(plusBtn.getStateOnce() == 0){ SerialUSB.println("Plus"); coil_pw+=0.1; if (coil_pw >= 20){ coil_pw = 20; } } if(moinsBtn.getStateOnce() == 0){ SerialUSB.println("Moins"); coil_pw-=0.1; if (coil_pw <= 5){ coil_pw = 5; } } if(fireBtn.getState() == 0){ analogWrite(LED_PIN, 255); }else{ analogWrite(LED_PIN, 0); } if (fireBtn.getStateOnce() == 0){ checkBatteryVoltage(); } drawScreen(); }