From f4bccade0a073094860337b75ea9a7d4bd872497 Mon Sep 17 00:00:00 2001 From: "Ricardo@Work_lap" Date: Fri, 6 Jul 2018 16:05:34 +0000 Subject: [PATCH] :q --- firmware/v01/v01.ino | 80 ++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/firmware/v01/v01.ino b/firmware/v01/v01.ino index 29aa5b5..61720ff 100644 --- a/firmware/v01/v01.ino +++ b/firmware/v01/v01.ino @@ -15,6 +15,7 @@ Tue Jun 12 14:56:12 GMT 2018 // Configuration of pins, insert here arduino pin numbers only(not fisical)---------------------------- +#define i_load 6 // Analog pin for the load current #define v_batt 7 // Analog pin for the battery voltage #define volt_1 0 // Analog pin for voltage 1 #define volt_2 1 // Analog pin for voltage 2 @@ -27,11 +28,22 @@ LiquidCrystal lcd(7, 6, 2, 3, 4, 5); // LCD screen pins, LiquidCrystal(rs, enabl // Other configurations ------------------------------------------------------------------------------------ -#define battery_volt 3 // Parameter position where battery voltage value is hold #define voltage_1 0 // Parameter position where voltage one value is hold #define voltage_2 1 // Parameter position where voltage two value is hold +#define curr 2 // Parameter position where load current value is hold +#define battery_volt 3 // Parameter position where battery voltage value is hold #define numOfScreens 7 // Number of screen menus, select here and rearrange String screens -File myFile; +#define delay_loop 4000 // Value in miliseconds for the main loop routine +#define sd_not_in 0 // SD card is not inserted +#define sd_in 1 // SD card is inserted +#define file_fail 2 // Can't write to file +#define writing 3 // Write on the sd card +#define curr_limit 2 // Maximum current allowed +#define off 0 // off is always zero +#define on 1 // on is always one +#define no 0 // no is always zero +#define yes 1 // yes is alyays one +File myFile; // Initiate the object File to write in the SD card //---------------------------------------------------------------------------------------------------------- @@ -39,23 +51,13 @@ File myFile; #define rLEFT digitalRead(LEFT) // Read button Left #define rRIGHT digitalRead(RIGHT) // Read button Right #define rACT digitalRead(ACT) // Read button Action -#define sd_not_in 0 // SD card is not inserted -#define sd_in 1 // SD card is inserted -#define file_fail 2 // Can't write to file -#define writing 3 // Write on the sd card -#define load_off 0 // Turn off the load -#define load_on 1 // Turn on the load -#define off 0 // off is always zero -#define on 1 // on is always one -#define no 0 // no is always zero -#define yes 1 // yes is alyays one //---------------------------------------------------------------- // Variables -------------------------------------------------------------------------------------------------------------- bool sd_rw = off; // Holds the state of read/write of the card -bool load_status = load_off; // Holds the state of on/off of the load -bool sd_inserted = no; // Holds the stats if sd card is in or not +bool load_status = off; // Holds the state of on/off of the load +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 @@ -67,9 +69,10 @@ String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"}, String parameters_sd[2] = {"No","Yes"}; // String to display the sd card logging String parameters_load[2] = {"Off","On"}; // String to display the load state String sd_card[4] = {"Card missing","Card inserted","File Write fail","Writing..."}; // String to display the sd card state +String file_name = "measures"; // Initial file name only 8 characters allowed float parameters[4]; // Parameters of the several voltages and current -float denominator; +float denominator; // Denominator to calculate the voltage divider int resistor1 = 22000; // Resistor one of voltage divider int resistor2 = 2200; // Resistor two of voltage divider // ------------------------------------------------------------------------------------------------------------------------ @@ -77,9 +80,11 @@ int resistor2 = 2200; // Resistor two of voltage divider // Function declarations -------------------------------------------------- -void menuState(void); // Read buttons and update menu -void printScreen (void); // Print menus and variables on screen -float volt_pin (int pin); // Read and display voltage +void menuState(void); // Read buttons and update menu +void printScreen (void); // Print menus and variables on screen +float volt_pin (int pin); // Read and display voltage +float amp_pin (int pin); // Read and display current +float fmap(float x, float in_min, float in_max, float out_min, float out_max); // Calculate current opamp //------------------------------------------------------------------------- @@ -91,7 +96,7 @@ void setup() pinMode(ACT, INPUT); // Button act is an input pinMode(load_line, OUTPUT); // load_line is an output - lcd.begin(16, 2); + lcd.begin(16, 2); // Start LCD display with 16 characters and 2 lines // Voltage divider --> Vout = Vin * R2 / R1 + R2 denominator = (float)resistor2 / (resistor1 + resistor2); @@ -125,8 +130,12 @@ void setup() // Timer compare interrupt service routine -- ISR(TIMER1_COMPA_vect) { - menuState(); // Read buttons act accordingly and update menus - digitalWrite(load_line, load_status); // Turn load on or off depending on the load_status + menuState(); // Read buttons act accordingly and update menus + if (amp_pin(i_load) > curr_limit) // If load current more then current limit + { + load_status = off; // Turn off load + } + digitalWrite(load_line, load_status); // Turn load on or off depending on the load_status } //------------------------------------------- @@ -136,7 +145,7 @@ void loop() parameters[battery_volt] = volt_pin(v_batt); // Update Battery voltage value parameters[voltage_1] = volt_pin(volt_1); // Update voltage one value parameters[voltage_2] = volt_pin(volt_2); // Update voltage two value - + parameters[curr] = amp_pin(i_load); // Update current load if (sd_inserted == no) // If sd_rw value is turned on { @@ -144,8 +153,8 @@ void loop() } else if (sd_rw == on) { - myFile = SD.open("measure.txt", FILE_WRITE); // Open file measure.txt for writing - if (myFile) // if succeds + myFile = SD.open(file_name + ".TXT", FILE_WRITE); // Open file name and add ".TXT", for writing + if (myFile) // if succeds { myFile.print("Batt voltage, "); myFile.println(volt_pin(v_batt)); // Write to card battery voltage @@ -153,6 +162,8 @@ void loop() myFile.println(volt_pin(volt_1)); // Write to card voltage one myFile.print("Voltage two, "); myFile.println(volt_pin(volt_2)); // Write to card voltage two + myFile.print("Load current, "); + 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; @@ -168,7 +179,7 @@ void loop() } printScreen(); // Refresh screen and print current menu and values - delay(5000); // Repeat the main loop every 5 seconds + delay(delay_loop); // Repeat the main loop every delay_loop seconds } void menuState() @@ -254,3 +265,22 @@ float volt_pin(int pin) return voltage; } + + +float amp_pin (int pin) +{ + int readAmpsADC = 0; + float amps = 0.0; + + readAmpsADC = analogRead(pin); + amps = fabs(fmap(readAmpsADC, 0.0, 1023.0, 0.01, 3.3)); + + return amps; +} + + +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; +} +