Files
PowerTester/firmware/v01/v01.ino

176 lines
4.8 KiB
C++

/*********************************************************************
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>
// 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)
//-----------------------------------------------------------------------------------------------------
// 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
//------------------------------------------------------------
// Variables ------------------------------------------------------------------------------
bool sd_status = 0;
bool load_status = 0;
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", ""}, {"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() {
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() {
parameters[3] = volt_pin(v_batt);
parameters[0] = volt_pin(volt_1);
parameters[1] = volt_pin(volt_2);
printScreen();
delay(4000);
}
void menuState() {
if (!rRIGHT) {
if (currentScreen == 5) {
currentScreen = 0;
}
else {
currentScreen++;
}
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 printScreen() {
lcd.clear();
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,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;
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;
}