coil res measure but not accurate
This commit is contained in:
parent
603ba091d5
commit
b453cfec30
2
.vscode/arduino.json
vendored
2
.vscode/arduino.json
vendored
@ -1,5 +1,5 @@
|
||||
{
|
||||
"port": "/dev/ttyACM27",
|
||||
"port": "/dev/ttyACM0",
|
||||
"board": "arduino:samd:arduino_zero_native",
|
||||
"sketch": "BoxmodFirmware.ino",
|
||||
"output": "./build/"
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Fonts/FreeSans9pt7b.h>
|
||||
#include <algorithm>
|
||||
|
||||
#define SSD1306_NO_SPLASH
|
||||
#define SCREEN_WIDTH 96
|
||||
@ -12,9 +13,12 @@
|
||||
|
||||
#define LED_PIN 18
|
||||
|
||||
#define batVcheckActivatePin 42
|
||||
#define batVcheckMesurePin 14
|
||||
#define batVcheckActivatePin 42
|
||||
|
||||
#define coilVcheckMesurePin1 15
|
||||
// #define coilVcheckMesurePin2 16
|
||||
#define coilRcheckActivatePin 16
|
||||
|
||||
|
||||
int bat_prct = 100;
|
||||
@ -33,13 +37,20 @@ void setup() {
|
||||
// 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
|
||||
@ -97,19 +108,78 @@ void checkBatteryVoltage(){
|
||||
digitalWrite(batVcheckActivatePin, LOW);
|
||||
int batv_mesure = analogRead(batVcheckMesurePin);
|
||||
digitalWrite(batVcheckActivatePin, HIGH);
|
||||
SerialUSB.print("batv_mesure: ");
|
||||
SerialUSB.print(batv_mesure);
|
||||
|
||||
// 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");
|
||||
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(15000);
|
||||
#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");
|
||||
@ -135,6 +205,8 @@ void loop() {
|
||||
|
||||
if (fireBtn.getStateOnce() == 0){
|
||||
checkBatteryVoltage();
|
||||
delay(1000);
|
||||
checkCoilResistance();
|
||||
}
|
||||
|
||||
|
||||
|
@ -17,9 +17,9 @@ void Btn::checkState(){
|
||||
}
|
||||
if ((millis() - _last_debounce_time) > _debounce_delay) {
|
||||
if (reading != _state) {
|
||||
SerialUSB.print(_pin);
|
||||
SerialUSB.print(": ");
|
||||
SerialUSB.println(_state);
|
||||
// SerialUSB.print(_pin);
|
||||
// SerialUSB.print(": ");
|
||||
// SerialUSB.println(_state);
|
||||
_state = reading;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user