Fixed P5 label on schematic. Finished menu, buttons, voltage mesarument, load on/off.

This commit is contained in:
2018-06-13 15:43:22 +00:00
parent 1a9bc880ab
commit a102828864
2 changed files with 132 additions and 101 deletions

View File

@@ -1,5 +1,4 @@
EESchema Schematic File Version 2 EESchema Schematic File Version 2
LIBS:PowT_B-rescue
LIBS:power LIBS:power
LIBS:device LIBS:device
LIBS:transistors LIBS:transistors
@@ -994,7 +993,7 @@ L Conn_01x02 P6
U 1 1 589324D7 U 1 1 589324D7
P 9775 8025 P 9775 8025
F 0 "P6" H 9775 8175 50 0000 C CNN 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 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 F 3 "" H 9775 8025 50 0000 C CNN
1 9775 8025 1 9775 8025

View File

@@ -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.h> #include <LiquidCrystal.h>
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 // Macros to read the buttons --------------------------------
const int numOfInputs = 4; #define rLEFT digitalRead(LEFT) // Read button Left
const int inputPins[numOfInputs] = {16,18,19,11}; #define rRIGHT digitalRead(RIGHT) // Read button Right
int inputState[numOfInputs]; #define rACT digitalRead(ACT) // Read button Action
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 // Variables ------------------------------------------------------------------------------
float denominator; bool sd_status = 0;
int resistor1 = 22000; // Values measured in circuit bool load_status = 0;
int resistor2 = 2200; // for better accuracy
// ----------------------------------------------------
//LCD Menu Logic const int numOfScreens = 6;
const int numOfScreens = 5;
int currentScreen = 0; int currentScreen = 0;
String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"}, String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"},
{"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}}; {"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}, {"Load", ""}};
float parameters[numOfScreens];
String parameters_sd[2] = {"No","Yes"}; 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() { void setup() {
for(int i = 0; i < numOfInputs; i++) {
pinMode(inputPins[i], INPUT); pinMode(LEFT, INPUT); // Button 1 is an input
// digitalWrite(inputPins[i], HIGH); // pull-up 20k pinMode(RIGHT, INPUT); // Button 2 is an input
} pinMode(ACT, INPUT); // Button 3 is an input
//Serial.begin(9600); pinMode(load_line, OUTPUT); // Output that controls the load
lcd.begin(16, 2); lcd.begin(16, 2);
// Voltage divider --> Vout = Vin * R2 / R1 + R2
denominator = (float)resistor2 / (resistor1 + resistor2); 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() { void loop() {
setInputFlags();
resolveInputFlags();
parameters[3] = volt_pin(v_batt); parameters[3] = volt_pin(v_batt);
parameters[0] = volt_pin(volt_1); parameters[0] = volt_pin(volt_1);
parameters[1] = volt_pin(volt_2); parameters[1] = volt_pin(volt_2);
// printScreen();
// delay(1000); printScreen();
delay(4000);
} }
void setInputFlags() { void menuState() {
for(int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]); if (!rRIGHT) {
if (reading != lastInputState[i]) { if (currentScreen == 5) {
lastDebounceTime[i] = millis(); currentScreen = 0;
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
} }
} else {
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{
currentScreen++; 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() { void printScreen() {
lcd.clear(); lcd.clear();
lcd.print(screens[currentScreen][0]); lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1); lcd.setCursor(0,1);
lcd.print(parameters[currentScreen]);
lcd.print(" "); if (currentScreen == 4) {
lcd.print(screens[currentScreen][1]); 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 volt_pin(int pin) {
float voltage; float voltage;
//Obtain RAW voltage data voltage = analogRead(pin); // Obtain RAW voltage data
voltage = analogRead(pin); voltage = (voltage / 1024) * 3.3; // Convert to actual voltage
voltage = voltage / denominator; // Convert to voltage after divider
//Convert to actual voltage
voltage = (voltage / 1024) * 3.3;
//Convert to voltage after divider
voltage = voltage / denominator;
return voltage; return voltage;
} }