#include <LiquidCrystal.h> // Display library
//setup display
// VSS gnd, VCC +5v, cont pot, rw - gnd
// pins for lcd RS En D4 D5 D6 D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int row = 0; // setCursor row
int col = 0; // setCursor column
//discharging flag and pin
boolean discharging = LOW;
const int dischargingPin = 13;
//discharging timer variables
int hh = 0; // hours
int mm = 0; // mins
int ss = 0; // seconds
// Voltmeter varibables and pin
const int vadcPin = 0; //ADC0 for battery
const int iadcPin = 1; //ADC1 for current
const int sadcPin = 2; //ADC2 for stop
int adcPin = 2; //ADC pin for measuring
int vadc = 0; //
float vadcMeas = 0.0; // measured and calculated V
float vadcCalc = 0.0; // calculated V
float R1 = 100000.0; // adc divider R1 (100K) +5V---[_R1_]---ADC_PIN---[_R2_]---GND
float R2 = 10000.0; // adc divider R2 (10K)
// Button setup
const int buttonPin = 8;// button pin RB (10k) +5V---BUTTON---BT_PIN---[_RB_]---GND
int buttonState = 0; // reading button
void setup() { //setup
//set pins
pinMode(dischargingPin, OUTPUT); //pin for discharging control
pinMode(vadcPin, INPUT); //V-meter ADC pin
pinMode(iadcPin, INPUT); //I-meter ADC pin
pinMode(sadcPin, INPUT); //ADC pin for stop setup
pinMode(buttonPin, INPUT); //Button pin
cli(); //stop interrupts
//timer setup
//For arduino Mega
//timer4 will interrupt at 0.5Hz
TCCR4A = 0;// set entire TCCR1A register to 0
TCCR4B = 0;// same for TCCR1B
TCNT4 = 0;//initialize counter value to 0
// set compare match register for 0.5hz increments
OCR4A = 15624/2;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR4B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR4B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK4 |= (1 << OCIE4A);
//timer setup
//For arduino Mega
//timer4 will interrupt at 0.5Hz
// Display setup
// lcd1602
// setup number columns (16) and row (2)
lcd.begin(16, 2);
// Hello world!
// lcd.print("Hello World!");
// set cursor on second row (first row - 0 , second row - 1) and 8 columns from left
lcd.setCursor(8, 1);
// output "setup"
lcd.print("Setup");
// Display setup
// lcd1602
// sei(); //allow interrupts
} // end setup
ISR(TIMER4_COMPA_vect){ //timer1 interrupt 0.5Hz toggles pin 13 (LED)
//generates pulse wave of frequency 0.5Hz/2 = 0.25Hz (takes two cycles for full wave- toggle high then toggle low)
digitalWrite(dischargingPin,discharging); // blinking for check - must be changed when test ok!!!
discharging = !discharging; // blinking for check - must be changed when test ok!!!
ss++; // second counter
}
void tiktak() { // time count
if (ss>21) {
ss = 0;
mm++;
}
if (mm>11) {
mm = 0;
hh ++;
}
}
void vmeter(){
// meter
vadc = analogRead(adcPin);
vadcCalc = (vadc * 5.0) / 1023.0;
vadcMeas = vadcCalc / (R2/(R1+R2));
}
void button(){ // wait for button
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
while (buttonState == LOW) {
row = 0;
col = 0;
adcPin = sadcPin;
vmeter();
lcd.setCursor(row, col);
lcd.print(vadcCalc*3);
buttonState = digitalRead(buttonPin);
}
// read the state of the pushbutton value:
// buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
// if (buttonState == HIGH) {
// // turn LED on:
// digitalWrite(ledPin, HIGH);
// } else {
// // turn LED off:
// digitalWrite(ledPin, LOW);
// }
sei(); //allow interrupts and start tik-tak
}
void time(){ // time to display
row = 0;
col = 7;
lcd.setCursor(col, row);
// hours
if (hh<100){
lcd.print(" ");
}
if (hh<10){
lcd.print(" ");
}
lcd.print(hh);
lcd.print(":");
// minuts
if (mm<10){
lcd.print(" ");
}
lcd.print(mm);
lcd.print(":");
// second
if (ss<20){
lcd.print(" ");
}
lcd.print(ss/2);
}
void loop(){ // main
vmeter();
button();
while(hh<10){
tiktak();
time();
delay(100);
} // pause in milisec
} // end main