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",
|
"board": "arduino:samd:arduino_zero_native",
|
||||||
"sketch": "BoxmodFirmware.ino",
|
"sketch": "BoxmodFirmware.ino",
|
||||||
"output": "./build/"
|
"output": "./build/"
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
#include <Adafruit_SSD1306.h>
|
#include <Adafruit_SSD1306.h>
|
||||||
#include <Fonts/FreeSans9pt7b.h>
|
#include <Fonts/FreeSans9pt7b.h>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#define SSD1306_NO_SPLASH
|
#define SSD1306_NO_SPLASH
|
||||||
#define SCREEN_WIDTH 96
|
#define SCREEN_WIDTH 96
|
||||||
@ -12,9 +13,12 @@
|
|||||||
|
|
||||||
#define LED_PIN 18
|
#define LED_PIN 18
|
||||||
|
|
||||||
#define batVcheckActivatePin 42
|
|
||||||
#define batVcheckMesurePin 14
|
#define batVcheckMesurePin 14
|
||||||
|
#define batVcheckActivatePin 42
|
||||||
|
|
||||||
|
#define coilVcheckMesurePin1 15
|
||||||
|
// #define coilVcheckMesurePin2 16
|
||||||
|
#define coilRcheckActivatePin 16
|
||||||
|
|
||||||
|
|
||||||
int bat_prct = 100;
|
int bat_prct = 100;
|
||||||
@ -33,13 +37,20 @@ void setup() {
|
|||||||
// while (!SerialUSB); // Leonardo: wait for serial monitor
|
// while (!SerialUSB); // Leonardo: wait for serial monitor
|
||||||
SerialUSB.println("\nBoxmodFirmware");
|
SerialUSB.println("\nBoxmodFirmware");
|
||||||
|
|
||||||
|
// debug led for fire btn
|
||||||
pinMode(LED_PIN, OUTPUT);
|
pinMode(LED_PIN, OUTPUT);
|
||||||
analogWrite(LED_PIN, 0);
|
analogWrite(LED_PIN, 0);
|
||||||
|
|
||||||
|
// battery voltage check
|
||||||
pinMode(batVcheckActivatePin, OUTPUT);
|
pinMode(batVcheckActivatePin, OUTPUT);
|
||||||
digitalWrite(batVcheckActivatePin, HIGH);
|
digitalWrite(batVcheckActivatePin, HIGH);
|
||||||
pinMode(batVcheckMesurePin, INPUT);
|
pinMode(batVcheckMesurePin, INPUT);
|
||||||
|
|
||||||
|
// coil restistance check
|
||||||
|
pinMode(coilRcheckActivatePin, OUTPUT);
|
||||||
|
digitalWrite(coilRcheckActivatePin, HIGH);
|
||||||
|
pinMode(coilVcheckMesurePin1, INPUT);
|
||||||
|
|
||||||
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
|
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
|
||||||
SerialUSB.println(F("Échec de l'initialisation de l'écran OLED"));
|
SerialUSB.println(F("Échec de l'initialisation de l'écran OLED"));
|
||||||
// while (true); // Boucle infinie en cas d'échec
|
// while (true); // Boucle infinie en cas d'échec
|
||||||
@ -97,19 +108,78 @@ void checkBatteryVoltage(){
|
|||||||
digitalWrite(batVcheckActivatePin, LOW);
|
digitalWrite(batVcheckActivatePin, LOW);
|
||||||
int batv_mesure = analogRead(batVcheckMesurePin);
|
int batv_mesure = analogRead(batVcheckMesurePin);
|
||||||
digitalWrite(batVcheckActivatePin, HIGH);
|
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;
|
double batv = (batv_mesure * 2.23/4095.0)*2;
|
||||||
// SerialUSB.print(", batv: ");
|
SerialUSB.print(", batv: ");
|
||||||
// SerialUSB.print(batv);
|
SerialUSB.print(batv);
|
||||||
// SerialUSB.println("V");
|
SerialUSB.println("V");
|
||||||
// range of 3.4V to 4.1V
|
// range of 3.4V to 4.1V
|
||||||
bat_prct = (batv - 3.4) / (4.1 - 3.4) * 100;
|
bat_prct = (batv - 3.4) / (4.1 - 3.4) * 100;
|
||||||
// Clamp the percentage to [0, 100]
|
// Clamp the percentage to [0, 100]
|
||||||
bat_prct = constrain(bat_prct, 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() {
|
void loop() {
|
||||||
if(plusBtn.getStateOnce() == 0){
|
if(plusBtn.getStateOnce() == 0){
|
||||||
SerialUSB.println("Plus");
|
SerialUSB.println("Plus");
|
||||||
@ -135,6 +205,8 @@ void loop() {
|
|||||||
|
|
||||||
if (fireBtn.getStateOnce() == 0){
|
if (fireBtn.getStateOnce() == 0){
|
||||||
checkBatteryVoltage();
|
checkBatteryVoltage();
|
||||||
|
delay(1000);
|
||||||
|
checkCoilResistance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,9 +17,9 @@ void Btn::checkState(){
|
|||||||
}
|
}
|
||||||
if ((millis() - _last_debounce_time) > _debounce_delay) {
|
if ((millis() - _last_debounce_time) > _debounce_delay) {
|
||||||
if (reading != _state) {
|
if (reading != _state) {
|
||||||
SerialUSB.print(_pin);
|
// SerialUSB.print(_pin);
|
||||||
SerialUSB.print(": ");
|
// SerialUSB.print(": ");
|
||||||
SerialUSB.println(_state);
|
// SerialUSB.println(_state);
|
||||||
_state = reading;
|
_state = reading;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user