diff --git a/PowT_B/PowT_B.sch b/PowT_B/PowT_B.sch index 80d2944..8e3831a 100644 --- a/PowT_B/PowT_B.sch +++ b/PowT_B/PowT_B.sch @@ -1,5 +1,4 @@ EESchema Schematic File Version 2 -LIBS:PowT_B-rescue LIBS:power LIBS:device LIBS:transistors @@ -994,7 +993,7 @@ L Conn_01x02 P6 U 1 1 589324D7 P 9775 8025 F 0 "P6" H 9775 8175 50 0000 C CNN -F 1 "ExT. Power" H 10050 8025 50 0000 C CNN +F 1 "Load" H 9950 8025 50 0000 C CNN F 2 "Connectors_Phoenix:PhoenixContact_MC-G_02x5.08mm_Angled" H 9775 8025 50 0001 C CNN F 3 "" H 9775 8025 50 0000 C CNN 1 9775 8025 diff --git a/firmware/v01/v01.ino b/firmware/v01/v01.ino index 1fd7d1a..5cb85ff 100644 --- a/firmware/v01/v01.ino +++ b/firmware/v01/v01.ino @@ -1,143 +1,175 @@ +/********************************************************************* + + Powertester + +Measures 2 voltages and current from an equipment(Load), and logs +the results on SD card. Control and read values from bluetooth. + +By Jony Silva +Tue Jun 12 14:56:12 GMT 2018 +*********************************************************************/ + + #include -LiquidCrystal lcd(7, 6, 2, 3, 4, 5); +// Configuration of pins ------------------------------------------------------------------------------ +#define v_batt 7 // Analog pin for the battery voltage, insert here arduino pin +#define volt_1 0 // Analog pin for voltage 1, insert here arduino pin +#define volt_2 1 // Analog pin for voltage 2, insert here arduino pin +#define load_line 17 // Line that turns load On or Off +#define LEFT 16 // Button 1 will be function Left, insert here arduino pin +#define RIGHT 18 // Button 2 will be function Right, insert here arduino pin +#define ACT 19 // Button 3 will be function Action, insert here arduino pin +LiquidCrystal lcd(7, 6, 2, 3, 4, 5); // LCD screen pins, LiquidCrystal(rs, enable, d4, d5, d6, d7) +//----------------------------------------------------------------------------------------------------- -//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; +// Macros to read the buttons -------------------------------- +#define rLEFT digitalRead(LEFT) // Read button Left +#define rRIGHT digitalRead(RIGHT) // Read button Right +#define rACT digitalRead(ACT) // Read button Action +//------------------------------------------------------------ -// 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; -int resistor1 = 22000; // Values measured in circuit -int resistor2 = 2200; // for better accuracy -// ---------------------------------------------------- +// Variables ------------------------------------------------------------------------------ +bool sd_status = 0; +bool load_status = 0; -//LCD Menu Logic -const int numOfScreens = 5; +const int numOfScreens = 6; int currentScreen = 0; String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"}, - {"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}}; -float parameters[numOfScreens]; + {"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}, {"Load", ""}}; String parameters_sd[2] = {"No","Yes"}; +String parameters_load[2] = {"Off","On"}; +float parameters[4]; + +float denominator; +int resistor1 = 22000; // Resistor one of voltage divider +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 setup() { - for(int i = 0; i < numOfInputs; i++) { - pinMode(inputPins[i], INPUT); -// digitalWrite(inputPins[i], HIGH); // pull-up 20k - } - //Serial.begin(9600); + + pinMode(LEFT, INPUT); // Button 1 is an input + pinMode(RIGHT, INPUT); // Button 2 is an input + pinMode(ACT, INPUT); // Button 3 is an input + pinMode(load_line, OUTPUT); // Output that controls the load + lcd.begin(16, 2); + // Voltage divider --> Vout = Vin * R2 / R1 + R2 denominator = (float)resistor2 / (resistor1 + resistor2); + + // initialize timer1 --------------------------------------- + noInterrupts(); // disable all interrupts + TCCR1A = 0; + TCCR1B = 0; + TCNT1 = 0; + + OCR1A = 9000; // Load value to compare + TCCR1B |= (1 << WGM12); // CTC mode + TCCR1B |= (1 << CS10); // 64 prescaler + TCCR1B |= (1 << CS11); // + TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt + interrupts(); // enable all interrupts + // ---------------------------------------------------------- + } +// Timer compare interrupt service routine -- +ISR(TIMER1_COMPA_vect) +{ + menuState(); + digitalWrite(load_line, load_status); +} +//------------------------------------------- + + 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); + + printScreen(); + delay(4000); } -void setInputFlags() { - for(int i = 0; i < numOfInputs; i++) { - int reading = digitalRead(inputPins[i]); - if (reading != lastInputState[i]) { - lastDebounceTime[i] = millis(); - } - if ((millis() - lastDebounceTime[i]) > debounceDelay) { - if (reading != inputState[i]) { - inputState[i] = reading; - if (inputState[i] == HIGH) { - inputFlags[i] = HIGH; - } +void menuState() { + + if (!rRIGHT) { + if (currentScreen == 5) { + currentScreen = 0; } - } - lastInputState[i] = reading; - } -} - -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{ + else { currentScreen++; - } - }else if(input == 2) { - parameterChange(0); - }else if(input == 3) { - parameterChange(1); } + while (!rRIGHT); // Check if button is still pressed do nothing + printScreen(); + } + + if (!rLEFT) { + if (currentScreen == 0) { + currentScreen = 5; + } + else { + currentScreen--; + } + while (!rLEFT); // Check if button is still pressed do nothing + printScreen(); + } + + if (!rACT) { + if (currentScreen == 4) { + sd_status = !sd_status; + } + if (currentScreen == 5) { + load_status = !load_status; + } + while (!rACT); // Check if button is still pressed do nothing + printScreen(); + } + } -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]); + + if (currentScreen == 4) { + lcd.print(parameters_sd[sd_status]); + } + else if (currentScreen == 5) { + lcd.print(parameters_load[load_status]); + } + else { + lcd.print(parameters[currentScreen]); + lcd.print(" "); + lcd.print(screens[currentScreen][1]); + } } float volt_pin(int pin) { float voltage; - //Obtain RAW voltage data - voltage = analogRead(pin); - - //Convert to actual voltage - voltage = (voltage / 1024) * 3.3; - - //Convert to voltage after divider - voltage = voltage / denominator; + 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; }