198 lines
5.4 KiB
C++
198 lines
5.4 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 <avr/sleep.h>
|
|
#include <LiquidCrystal.h>
|
|
|
|
|
|
// Configuration of pins, insert here arduino pin numbers only(not fisical)----------------------------
|
|
#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
|
|
#define load_line 17 // Line that turns load On or Off
|
|
#define LEFT 16 // Button 1 will be function Left
|
|
#define RIGHT 18 // Button 2 will be function Right
|
|
#define ACT 19 // Button 3 will be function Action
|
|
LiquidCrystal lcd(7, 6, 2, 3, 4, 5); // LCD screen pins, LiquidCrystal(rs, enable, d4, d5, d6, d7)
|
|
//-----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
// 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 numOfScreens 6 // Number of screen menus, select here and rearrange String screens
|
|
//----------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
// 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;
|
|
|
|
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 left is an input
|
|
pinMode(RIGHT, INPUT); // Button right is an input
|
|
pinMode(ACT, INPUT); // Button act is an input
|
|
pinMode(load_line, OUTPUT); // load_line is an output
|
|
|
|
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[battery_volt] = volt_pin(v_batt);
|
|
parameters[voltage_1] = volt_pin(volt_1);
|
|
parameters[voltage_2] = volt_pin(volt_2);
|
|
|
|
printScreen();
|
|
delay(4000);
|
|
}
|
|
|
|
void menuState()
|
|
{
|
|
|
|
if (!rRIGHT)
|
|
{
|
|
if (currentScreen == (numOfScreens - 1))
|
|
{
|
|
currentScreen = 0;
|
|
}
|
|
else
|
|
{
|
|
currentScreen++;
|
|
}
|
|
while (!rRIGHT); // Check if button is still pressed do nothing
|
|
printScreen();
|
|
}
|
|
|
|
if (!rLEFT)
|
|
{
|
|
if (currentScreen == 0)
|
|
{
|
|
currentScreen = (numOfScreens - 1);
|
|
}
|
|
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 if (currentScreen == 6)
|
|
{
|
|
lcd.print(screens[currentScreen][1]);
|
|
}
|
|
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;
|
|
}
|