#include "src/Btn/Btn.h" #include #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) // only for debuging #define LED_PIN 13 #define batVcheckMesurePin 14 #define batVcheckActivatePin 42 #define coilVcheckMesurePin1 15 // #define coilVcheckMesurePin2 16 #define coilRcheckActivatePin 16 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"); // debug led for fire btn pinMode(LED_PIN, OUTPUT); analogWrite(LED_PIN, 0); // battery voltage check pinMode(batVcheckActivatePin, OUTPUT); digitalWrite(batVcheckActivatePin, HIGH); pinMode(batVcheckMesurePin, INPUT); // coil restistance check pinMode(coilRcheckActivatePin, OUTPUT); digitalWrite(coilRcheckActivatePin, HIGH); pinMode(coilVcheckMesurePin1, 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 checkCoilResistance(){ analogReadResolution(12); // btw 0 & 4095 analogReference(AR_DEFAULT); // measuring the analog input of voltage digitalWrite(coilRcheckActivatePin, LOW); delay(1000); #define NUM_SAMPLES 30 int readings[NUM_SAMPLES]; for (size_t i = 0; i < NUM_SAMPLES; i++) { readings[i]= analogRead(coilVcheckMesurePin1); delay(10); } digitalWrite(coilRcheckActivatePin, HIGH); // coilV_measure = coilV_measure/range; std::sort(readings, readings + NUM_SAMPLES); int coilV_measure = readings[NUM_SAMPLES / 2]; SerialUSB.print("coilV_measure: "); SerialUSB.print(coilV_measure); const double vref = 3.30000; const double r_one = 1.070000; // converting adc to volt // 2.48/3.05 = 0.8131 const double adc_correction = 0.8131; double v_two = (coilV_measure * vref/4095.000000) * adc_correction; SerialUSB.print(", v_two: "); SerialUSB.print(v_two, 6); SerialUSB.print("V"); // calculate voltage accross know res double v_one = vref - v_two; SerialUSB.print(", v_one: "); SerialUSB.print(v_one); SerialUSB.print("V"); // calculate current double i = v_one / r_one; SerialUSB.print(", i: "); SerialUSB.print(i,6); SerialUSB.print("A"); // calculate Ohm double r_two = v_two/ i; SerialUSB.print(", r_two: "); SerialUSB.print(r_two); SerialUSB.println("Ohm"); // coil_res = (3.3 - v_two)-1); // coil_res = (3.3 - v_two) * 14.9; coil_res = r_two; // SerialUSB.print(", coil_res: "); // SerialUSB.print(coil_res); // SerialUSB.println("Ohm"); } 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(); delay(1000); checkCoilResistance(); } drawScreen(); }