From 75fe7048fefda803dd6bdd6e14ed827edc3c7ae7 Mon Sep 17 00:00:00 2001 From: "Ricardo@Work_lap" Date: Wed, 24 Oct 2018 11:53:38 +0000 Subject: [PATCH] Finish commenting v01 firmware. --- firmware/v01/v01.ino | 121 +++++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 51 deletions(-) diff --git a/firmware/v01/v01.ino b/firmware/v01/v01.ino index 8572c6d..7c2908b 100644 --- a/firmware/v01/v01.ino +++ b/firmware/v01/v01.ino @@ -66,7 +66,7 @@ bool sd_inserted = no; // Holds the state if sd card is in or not int sd_status = sd_not_in; // Holds information about the sd card int currentScreen = 0; // Current display menu -char ble_data = 'Z'; +char ble_data = 'Z'; // Holds a character received by bluetooth // String with the menus to be displayed String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"}, @@ -81,7 +81,7 @@ float parameters[4]; // Parameters of the several voltages and curre float denominator; // Denominator to calculate the voltage divider int resistor1 = 22000; // Resistor one of voltage divider int resistor2 = 2200; // Resistor two of voltage divider -static char outstr[15]; +static char outstr[15]; // String output to bluetooth converted from float // ----------------------------------------------------------------------------------------------- @@ -173,7 +173,6 @@ ISR(TIMER1_COMPA_vect) load_status = off; } } - } //------------------------------------------- @@ -190,82 +189,90 @@ void loop() Serial.write(ble_data); // For debug only - if (sd_inserted == no) // If sd_rw value is turned on + if (sd_inserted == no) // If SD card is not inserted { - sd_status = sd_not_in; + sd_status = sd_not_in; // change card status to sd card not inserted } - else if (sd_rw == on) + else if (sd_rw == on) // otherwise sd_rw is ON { myFile = SD.open(file_name + ".TXT", FILE_WRITE); // Open file name and add ".TXT", - // for writing - if (myFile) // if succeds + // for writing + if (myFile) // if succeds { - myFile.print("Batt voltage, "); + myFile.print("Batt voltage, "); // Write string to card myFile.println(volt_pin(v_batt)); // Write to card battery voltage - myFile.print("Voltage one, "); + myFile.print("Voltage one, "); // Write string to card myFile.println(volt_pin(volt_1)); // Write to card voltage one - myFile.print("Voltage two, "); + myFile.print("Voltage two, "); // Write string to card myFile.println(volt_pin(volt_2)); // Write to card voltage two - myFile.print("Load current, "); + myFile.print("Load current, "); // Write string to card myFile.println(amp_pin(i_load)); // Write to card load current - myFile.println(""); // Write to card an empty line - myFile.close(); // Close file - sd_status = writing; + myFile.println(""); // Write to card an empty line + myFile.close(); // Close file + sd_status = writing; // change card status to, writing } else { - sd_status = file_fail; + sd_status = file_fail; // otherwise change card status to, failed } } else { - sd_status = sd_in; + sd_status = sd_in; // otherwise change card status to, card inserted } printScreen(); // Refresh screen and print current menu and values delay(delay_loop); // Repeat the main loop every delay_loop seconds } + +// Function that checks in what menu we are and wich button was pressed, +// in case button Right was pressed menu will rotate to the right +// in case button Left was pressed menu will rotate to the left +// in case button Action was pressed and we are on the fourth SD card menu, the card +// writing can be toggled between No and Yes +// in case button Action was pressed and we are on the fifth Load menu, the load +// can be toggled between On and Off void menuState() { - if (!rRIGHT) + if (!rRIGHT) // if button Right is pressed { - if (currentScreen == (numOfScreens - 1)) + if (currentScreen == (numOfScreens - 1)) // check if its in the last screen { - currentScreen = 0; + currentScreen = 0; // then change to first screen } else { - currentScreen++; + currentScreen++; // otherwise increment screen } while (!rRIGHT); // Check if button is still pressed do nothing printScreen(); // Refresh screen and print current menu and values } - if (!rLEFT) + if (!rLEFT) // if button Right is pressed { - if (currentScreen == 0) + if (currentScreen == 0) // check if its the first screen { - currentScreen = (numOfScreens - 1); + currentScreen = (numOfScreens - 1); // then change screen to last screen } else { - currentScreen--; + currentScreen--; // otherwise decrement screen } while (!rLEFT); // Check if button is still pressed do nothing printScreen(); // Refresh screen and print current menu and values } - if (!rACT) + if (!rACT) // if button Action is pressed { - if (currentScreen == 4) + if (currentScreen == 4) // check if is fourth screen { - sd_rw = !sd_rw; + sd_rw = !sd_rw; // toggle read/write on SD card } - if (currentScreen == 5) + if (currentScreen == 5) // check if is fith screen { - load_status = !load_status; + load_status = !load_status; // toggle ON/OFF load } while (!rACT); // Check if button is still pressed do nothing printScreen(); // Refresh screen and print current menu and values @@ -273,58 +280,70 @@ void menuState() } + +// Function that checks the currentscreen scanned in function menuState() +// and prints on the LCD the corresponding strings for the currentScreen void printScreen() { - lcd.clear(); - lcd.print(screens[currentScreen][0]); - lcd.setCursor(0,1); + lcd.clear(); // Clear the LCD + lcd.print(screens[currentScreen][0]); // Print the current screen first line + lcd.setCursor(0,1); // move the LCD cursor to the second line - if (currentScreen == 4) + if (currentScreen == 4) // check if current screen is number 4 { - lcd.print(parameters_sd[sd_rw]); + lcd.print(parameters_sd[sd_rw]); // print content of parameters_sd string to second line } - else if (currentScreen == 5) + else if (currentScreen == 5) // check if current screen is number 5 { - lcd.print(parameters_load[load_status]); + lcd.print(parameters_load[load_status]); // print content of parameters_load string to second line } - else if (currentScreen == 6) + else if (currentScreen == 6) // check if current screen is number 6 { - lcd.print(sd_card[sd_status]); + lcd.print(sd_card[sd_status]); // print content of sd_card string to second line } else - { - lcd.print(parameters[currentScreen]); - lcd.print(" "); - lcd.print(screens[currentScreen][1]); + { // otherwise + lcd.print(parameters[currentScreen]); // print content of parameters string to second line + lcd.print(" "); // print one blank space + lcd.print(screens[currentScreen][1]); // and print content of screens second string } } + +// Function that takes an analogue pin number, reads and converts to real voltage value +// and returns it as a float float volt_pin(int pin) { float voltage; - voltage = analogRead(pin); // Obtain RAW voltage data + voltage = analogRead(pin); // Obtain RAW voltage data voltage = (voltage / 1024) * 3.3; // Convert to actual voltage voltage = voltage / denominator; // Convert to voltage after divider - return voltage; + return voltage; // return the final real voltage } + +// Function that takes an analogue pin number, reads and converts to real amperage value +// and returns it as a float float amp_pin (int pin) { - int readAmpsADC = 0; - float amps = 0.0; + int readAmpsADC = 0; // variable that holds the value read on analogue pin + float amps = 0.0; // variable that holds final amperage value - readAmpsADC = analogRead(pin); - amps = fabs(fmap(readAmpsADC, 0.0, 1023.0, 0.01, 3.3)); + readAmpsADC = analogRead(pin); // read pin and store value on variable readAmpsADC + amps = fabs(fmap(readAmpsADC, 0.0, 1023.0, 0.01, 3.3)); // function to convert from the analogue value to real amperage value - return amps; + return amps; // return the final real amperage } + +// Function that takes in, analogue pin value, minimum ADC value, maximum ADC value, minimum voltage, maximum voltage +// and returns the raw value float fmap(float x, float in_min, float in_max, float out_min, float out_max) { - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; // return the raw amperage value }