vbox-firmware/BoxmodFirmware.ino

216 lines
5.4 KiB
Arduino
Raw Normal View History

2025-02-09 00:31:27 +01:00
#include "src/Btn/Btn.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSans9pt7b.h>
2025-02-11 23:01:02 +01:00
#include <algorithm>
2025-02-09 00:31:27 +01:00
#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
2025-02-09 00:31:27 +01:00
2025-02-09 18:58:01 +01:00
#define batVcheckMesurePin 14
2025-02-11 23:01:02 +01:00
#define batVcheckActivatePin 42
2025-02-09 18:58:01 +01:00
2025-02-11 23:01:02 +01:00
#define coilVcheckMesurePin1 15
// #define coilVcheckMesurePin2 16
#define coilRcheckActivatePin 16
2025-02-09 18:58:01 +01:00
2025-02-09 00:31:27 +01:00
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");
2025-02-11 23:01:02 +01:00
// debug led for fire btn
2025-02-09 00:31:27 +01:00
pinMode(LED_PIN, OUTPUT);
analogWrite(LED_PIN, 0);
2025-02-11 23:01:02 +01:00
// battery voltage check
2025-02-09 18:58:01 +01:00
pinMode(batVcheckActivatePin, OUTPUT);
digitalWrite(batVcheckActivatePin, HIGH);
pinMode(batVcheckMesurePin, INPUT);
2025-02-11 23:01:02 +01:00
// coil restistance check
pinMode(coilRcheckActivatePin, OUTPUT);
digitalWrite(coilRcheckActivatePin, HIGH);
pinMode(coilVcheckMesurePin1, INPUT);
2025-02-09 00:31:27 +01:00
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
}
2025-02-09 18:58:01 +01:00
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);
2025-02-11 23:01:02 +01:00
SerialUSB.print("batv_mesure: ");
SerialUSB.print(batv_mesure);
2025-02-09 18:58:01 +01:00
double batv = (batv_mesure * 2.23/4095.0)*2;
2025-02-11 23:01:02 +01:00
SerialUSB.print(", batv: ");
SerialUSB.print(batv);
SerialUSB.println("V");
2025-02-09 18:58:01 +01:00
// 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);
}
2025-02-11 23:01:02 +01:00
void checkCoilResistance(){
analogReadResolution(12); // btw 0 & 4095
analogReference(AR_DEFAULT);
// measuring the analog input of voltage
digitalWrite(coilRcheckActivatePin, LOW);
delay(1000);
2025-02-11 23:01:02 +01:00
#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");
}
2025-02-09 00:31:27 +01:00
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);
}
2025-02-09 18:58:01 +01:00
if (fireBtn.getStateOnce() == 0){
checkBatteryVoltage();
2025-02-11 23:01:02 +01:00
delay(1000);
checkCoilResistance();
2025-02-09 18:58:01 +01:00
}
2025-02-09 00:31:27 +01:00
drawScreen();
}