120 lines
2.5 KiB
Arduino
120 lines
2.5 KiB
Arduino
|
// include the library code:
|
||
|
#include <LiquidCrystal.h>
|
||
|
#define delay_1 800
|
||
|
|
||
|
|
||
|
#define b_nav 16
|
||
|
#define b_ok 18
|
||
|
#define b_back 19
|
||
|
#define l_charge 17
|
||
|
#define a_charge A6
|
||
|
#define a_batt A7
|
||
|
|
||
|
// initialize the library with the numbers of the interface pins
|
||
|
LiquidCrystal lcd(7, 6, 2, 3, 4, 5);
|
||
|
|
||
|
|
||
|
//Analog volt read pin
|
||
|
//const int voltPin = A7;
|
||
|
|
||
|
// Variables for voltage divider
|
||
|
float denominator;
|
||
|
int resistor1 = 22000; // Values measured in circuit
|
||
|
int resistor2 = 2200; // for better accuracy
|
||
|
// ----------------------------------------------------
|
||
|
|
||
|
|
||
|
// Function declarations -----------------------------------------------------------------
|
||
|
void volt_batt (void); // Read and display voltage
|
||
|
//-----------------------------------------------------------------------------------------
|
||
|
|
||
|
|
||
|
|
||
|
void setup() {
|
||
|
|
||
|
pinMode(b_nav, INPUT);
|
||
|
pinMode(b_ok, INPUT);
|
||
|
pinMode(b_back, INPUT);
|
||
|
pinMode(l_charge, OUTPUT);
|
||
|
lcd.begin(16, 2);
|
||
|
// Print a message to the LCD.
|
||
|
lcd.print("PowerTester v01");
|
||
|
//Convert resistor values to division value
|
||
|
// Equation is previously mentions voltage divider equation
|
||
|
// R2 / (R1 + R2)
|
||
|
// In this case returns 0.20 or 1/5
|
||
|
denominator = (float)resistor2 / (resistor1 + resistor2);
|
||
|
|
||
|
digitalWrite(l_charge, LOW);
|
||
|
|
||
|
} // Void Setup Close
|
||
|
|
||
|
void loop() {
|
||
|
|
||
|
// lcd.setCursor(0, 1); // bottom left
|
||
|
// lcd.print("");
|
||
|
|
||
|
if (!digitalRead(b_nav)) {
|
||
|
volt_batt();
|
||
|
}
|
||
|
|
||
|
else if (!digitalRead(b_ok)) {
|
||
|
digitalWrite(l_charge, !digitalRead(l_charge));
|
||
|
lcd.setCursor(0, 1); // bottom left
|
||
|
lcd.print("Charge is toggled");
|
||
|
delay(500);
|
||
|
}
|
||
|
|
||
|
else if (!digitalRead(b_back)) {
|
||
|
float voltage_c;
|
||
|
float cur_c;
|
||
|
//Obtain RAW voltage data
|
||
|
voltage_c = analogRead(a_charge);
|
||
|
|
||
|
//Convert to actual voltage (Calibrated by hand)
|
||
|
voltage_c = (voltage_c / 1024) * 3.3;
|
||
|
|
||
|
cur_c = voltage_c / 0.5;
|
||
|
|
||
|
//Output to LCD
|
||
|
lcd.setCursor(0, 1); // bottom left
|
||
|
lcd.print("cur C = ");
|
||
|
lcd.setCursor(11,1);
|
||
|
lcd.print(cur_c);
|
||
|
|
||
|
//Delay to make LCD readable
|
||
|
delay(500);
|
||
|
}
|
||
|
|
||
|
else {
|
||
|
volt_batt();
|
||
|
}
|
||
|
|
||
|
|
||
|
} // void loop close
|
||
|
|
||
|
|
||
|
void volt_batt(void) {
|
||
|
|
||
|
float voltage;
|
||
|
//Obtain RAW voltage data
|
||
|
voltage = analogRead(a_batt);
|
||
|
|
||
|
//Convert to actual voltage (Calibrated by hand)
|
||
|
voltage = (voltage / 1024) * 3.3;
|
||
|
|
||
|
//Convert to voltage before divider
|
||
|
// Divide by divider = multiply
|
||
|
// Divide by 1/5 = multiply by 5
|
||
|
voltage = voltage / denominator;
|
||
|
|
||
|
//Output to LCD
|
||
|
lcd.setCursor(0, 1); // bottom left
|
||
|
lcd.print("Batt V = ");
|
||
|
lcd.setCursor(10,1);
|
||
|
lcd.print(voltage);
|
||
|
|
||
|
//Delay to make LCD readable
|
||
|
delay(500);
|
||
|
}
|