From 1a9bc880ab0d43af5c5956a1db6d9ed9d758d111 Mon Sep 17 00:00:00 2001 From: "Ricardo@Work_lap" Date: Tue, 12 Jun 2018 13:58:55 +0000 Subject: [PATCH] Working on firmware, completly changed code and kept the old one inside ./firmware. --- firmware/v01/v01.ino | 192 ++++++++++++++++++++++++------------------ firmware/v01_orig.ino | 119 ++++++++++++++++++++++++++ 2 files changed, 227 insertions(+), 84 deletions(-) create mode 100644 firmware/v01_orig.ino diff --git a/firmware/v01/v01.ino b/firmware/v01/v01.ino index 6f2632b..1fd7d1a 100644 --- a/firmware/v01/v01.ino +++ b/firmware/v01/v01.ino @@ -1,21 +1,29 @@ -// include the library code: #include -#define delay_1 800 - - -#define b_nav 16 -#define b_ok 18 -#define b_back 19 -#define l_charge 17 -#define a_charge A6 -#define a_batt A7 - -// initialize the library with the numbers of the interface pins LiquidCrystal lcd(7, 6, 2, 3, 4, 5); -//Analog volt read pin -//const int voltPin = A7; + + +//Input & Button Logic +const int numOfInputs = 4; +const int inputPins[numOfInputs] = {16,18,19,11}; +int inputState[numOfInputs]; +int lastInputState[numOfInputs] = {LOW,LOW,LOW,LOW}; +bool inputFlags[numOfInputs] = {LOW,LOW,LOW,LOW}; +long lastDebounceTime[numOfInputs] = {0,0,0,0}; +long debounceDelay = 5; +int v_batt = 7; // Voltage line from the battery +int volt_1 = 0; +int volt_2 = 1; + +// Function declarations ----------------------------------------------------------------- +void setInputFlags (void); +void resolveInputFlags (void); +void printScreen (void); +void parameterChange (int key); +void inputAction (int input); +float volt_pin (int pin); // Read and display voltage +//----------------------------------------------------------------------------------------- // Variables for voltage divider float denominator; @@ -23,97 +31,113 @@ int resistor1 = 22000; // Values measured in circuit int resistor2 = 2200; // for better accuracy // ---------------------------------------------------- - -// Function declarations ----------------------------------------------------------------- -void volt_batt (void); // Read and display voltage -//----------------------------------------------------------------------------------------- - - +//LCD Menu Logic +const int numOfScreens = 5; +int currentScreen = 0; +String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"}, + {"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}}; +float parameters[numOfScreens]; +String parameters_sd[2] = {"No","Yes"}; void setup() { - - pinMode(b_nav, INPUT); - pinMode(b_ok, INPUT); - pinMode(b_back, INPUT); - pinMode(l_charge, OUTPUT); + for(int i = 0; i < numOfInputs; i++) { + pinMode(inputPins[i], INPUT); +// digitalWrite(inputPins[i], HIGH); // pull-up 20k + } + //Serial.begin(9600); lcd.begin(16, 2); - // Print a message to the LCD. - lcd.print("PowerTester v01"); - //Convert resistor values to division value - // Equation is previously mentions voltage divider equation - // R2 / (R1 + R2) - // In this case returns 0.20 or 1/5 + denominator = (float)resistor2 / (resistor1 + resistor2); +} - digitalWrite(l_charge, LOW); - -} // Void Setup Close void loop() { + setInputFlags(); + resolveInputFlags(); + parameters[3] = volt_pin(v_batt); + parameters[0] = volt_pin(volt_1); + parameters[1] = volt_pin(volt_2); +// printScreen(); +// delay(1000); +} -// lcd.setCursor(0, 1); // bottom left -// lcd.print(""); - - if (!digitalRead(b_nav)) { - volt_batt(); +void setInputFlags() { + for(int i = 0; i < numOfInputs; i++) { + int reading = digitalRead(inputPins[i]); + if (reading != lastInputState[i]) { + lastDebounceTime[i] = millis(); } - - else if (!digitalRead(b_ok)) { - digitalWrite(l_charge, !digitalRead(l_charge)); - lcd.setCursor(0, 1); // bottom left - lcd.print("Charge is toggled"); - delay(500); + if ((millis() - lastDebounceTime[i]) > debounceDelay) { + if (reading != inputState[i]) { + inputState[i] = reading; + if (inputState[i] == HIGH) { + inputFlags[i] = HIGH; + } + } } - - else if (!digitalRead(b_back)) { - float voltage_c; - float cur_c; - //Obtain RAW voltage data - voltage_c = analogRead(a_charge); - - //Convert to actual voltage (Calibrated by hand) - voltage_c = (voltage_c / 1024) * 3.3; - - cur_c = voltage_c / 0.5; - - //Output to LCD - lcd.setCursor(0, 1); // bottom left - lcd.print("cur C = "); - lcd.setCursor(11,1); - lcd.print(cur_c); - - //Delay to make LCD readable - delay(500); + lastInputState[i] = reading; } +} - else { - volt_batt(); +void resolveInputFlags() { + for(int i = 0; i < numOfInputs; i++) { + if(inputFlags[i] == HIGH) { + inputAction(i); + inputFlags[i] = LOW; + printScreen(); + } } +} + +void inputAction(int input) { + if(input == 0) { + if (currentScreen == 0) { + currentScreen = numOfScreens-1; + }else{ + currentScreen--; + } + }else if(input == 1) { + if (currentScreen == numOfScreens-1) { + currentScreen = 0; + }else{ + currentScreen++; + } + }else if(input == 2) { + parameterChange(0); + }else if(input == 3) { + parameterChange(1); + } +} + +void parameterChange(int key) { + if(key == 0) { + parameters[currentScreen]++; + }else if(key == 1) { + parameters[currentScreen]--; + } +} + +void printScreen() { + lcd.clear(); + lcd.print(screens[currentScreen][0]); + lcd.setCursor(0,1); + lcd.print(parameters[currentScreen]); + lcd.print(" "); + lcd.print(screens[currentScreen][1]); +} -} // void loop close - - -void volt_batt(void) { +float volt_pin(int pin) { float voltage; //Obtain RAW voltage data - voltage = analogRead(a_batt); + voltage = analogRead(pin); - //Convert to actual voltage (Calibrated by hand) + //Convert to actual voltage voltage = (voltage / 1024) * 3.3; - //Convert to voltage before divider - // Divide by divider = multiply - // Divide by 1/5 = multiply by 5 + //Convert to voltage after divider voltage = voltage / denominator; - //Output to LCD - lcd.setCursor(0, 1); // bottom left - lcd.print("Batt V = "); - lcd.setCursor(10,1); - lcd.print(voltage); - - //Delay to make LCD readable - delay(500); + return voltage; } diff --git a/firmware/v01_orig.ino b/firmware/v01_orig.ino new file mode 100644 index 0000000..6f2632b --- /dev/null +++ b/firmware/v01_orig.ino @@ -0,0 +1,119 @@ +// include the library code: +#include +#define delay_1 800 + + +#define b_nav 16 +#define b_ok 18 +#define b_back 19 +#define l_charge 17 +#define a_charge A6 +#define a_batt A7 + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(7, 6, 2, 3, 4, 5); + + +//Analog volt read pin +//const int voltPin = A7; + +// Variables for voltage divider +float denominator; +int resistor1 = 22000; // Values measured in circuit +int resistor2 = 2200; // for better accuracy +// ---------------------------------------------------- + + +// Function declarations ----------------------------------------------------------------- +void volt_batt (void); // Read and display voltage +//----------------------------------------------------------------------------------------- + + + +void setup() { + + pinMode(b_nav, INPUT); + pinMode(b_ok, INPUT); + pinMode(b_back, INPUT); + pinMode(l_charge, OUTPUT); + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("PowerTester v01"); + //Convert resistor values to division value + // Equation is previously mentions voltage divider equation + // R2 / (R1 + R2) + // In this case returns 0.20 or 1/5 + denominator = (float)resistor2 / (resistor1 + resistor2); + + digitalWrite(l_charge, LOW); + +} // Void Setup Close + +void loop() { + +// lcd.setCursor(0, 1); // bottom left +// lcd.print(""); + + if (!digitalRead(b_nav)) { + volt_batt(); + } + + else if (!digitalRead(b_ok)) { + digitalWrite(l_charge, !digitalRead(l_charge)); + lcd.setCursor(0, 1); // bottom left + lcd.print("Charge is toggled"); + delay(500); + } + + else if (!digitalRead(b_back)) { + float voltage_c; + float cur_c; + //Obtain RAW voltage data + voltage_c = analogRead(a_charge); + + //Convert to actual voltage (Calibrated by hand) + voltage_c = (voltage_c / 1024) * 3.3; + + cur_c = voltage_c / 0.5; + + //Output to LCD + lcd.setCursor(0, 1); // bottom left + lcd.print("cur C = "); + lcd.setCursor(11,1); + lcd.print(cur_c); + + //Delay to make LCD readable + delay(500); + } + + else { + volt_batt(); + } + + +} // void loop close + + +void volt_batt(void) { + + float voltage; + //Obtain RAW voltage data + voltage = analogRead(a_batt); + + //Convert to actual voltage (Calibrated by hand) + voltage = (voltage / 1024) * 3.3; + + //Convert to voltage before divider + // Divide by divider = multiply + // Divide by 1/5 = multiply by 5 + voltage = voltage / denominator; + + //Output to LCD + lcd.setCursor(0, 1); // bottom left + lcd.print("Batt V = "); + lcd.setCursor(10,1); + lcd.print(voltage); + + //Delay to make LCD readable + delay(500); +}