the sketch is work in progress. I had written the whole program for pic and now i am converting little by little to arduino nano. the code so far
#include<LiquidCrystal.h>
#include <avr/interrupt.h>
#include <avr/io.h>
//#include <cstdio.h>
LiquidCrystal lcd (7,8,9,10,11,12); // Define LCD display pins RS, E, D4, D5, D6, D7
const int SpeedoPin = 2;
const int MenuPin = 6;
const int PlusPin = 5;
const int MinusPin = 4;
const int BuzzPin = 3;
volatile unsigned long millis_count = 0;
unsigned int a = 0, volts = 0, vadc, save_maH, batt_cap, amps;
float mAsec;
unsigned char flag_200ms = 0; // set HIGH once 0.2 second (200ms)
unsigned char charging_flag; // 1=charging, 0=discharging
void SpeedoIn();
void power_calc();
void display_va( char col, char row, unsigned long value, char type);
void display_percent_watt(char row, char col, unsigned int value, char type);
void setup() {
// put your setup code here, to run once:
// Disable Global Interrupts
//cli();
noInterrupts();
// Set Timer1 to Normal Mode
TCCR1A = 0; // Set Entire TCCR1A register to 0
TCCR1B = 0; //(1 << WGM12); // Set Entire TCCR1B register to 0
TCCR1B |= B00000001; // Prescaler = 1
TCNT1 = 49535; // Timer Preloading
TIMSK1 |= B00000001;
// Enable Global Interrupts
//sei();
interrupts();
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BuzzPin, OUTPUT);
pinMode(SpeedoPin, INPUT_PULLUP);
pinMode(MenuPin, INPUT_PULLUP);
pinMode(PlusPin, INPUT_PULLUP);
pinMode(MinusPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2),SpeedoIn,CHANGE);
Serial.begin(9600);
lcd.begin(20,4);
//lcd.setCursor(5, 0);
//lcd.print("V");
//lcd.setCursor(13, 0);
//lcd.print("A");
//lcd.setCursor(19, 0);
//lcd.print("W");
lcd.setCursor(6, 1);
lcd.print("mA");
lcd.setCursor(0, 2);
lcd.print("E");
lcd.setCursor(9, 2);
lcd.print("F");
}
void loop() {
// put your main code here, to run repeatedly:
power_calc();
delay(200);
}
ISR(TIMER1_OVF_vect){
TCNT1 = 49535;
// Increment the millisecond counter
millis_count++;
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
ISR(TIMER1_COMPA_vect){
millis_count++;
}
void power_calc(){
unsigned long int wt = 000;
//float amps;// = 00.00;
unsigned int amps;
float volts = 00.00;
char va[] = "00.00 ";
unsigned char sign;
vadc = analogRead(A0); // Read AN0/RA0 for voltage
volts = vadc * 4.89;
volts = volts / 100;
//volts = (vadc /1023) * 5;
a = analogRead(A3); // Read AN2/RA2 for current
if (a >= 511){
amps = (float)(((a - 511) * 4.89) / 6.6); // ACS712-30A=6.6,20A=10
sign = "-"; // Battery Discharging
charging_flag = 0; // show that battery is discharging
}
else if (a < 510){
amps = (float)(((511 - a) * 4.89) / 6.6);
sign = "+"; // Battery Charging
charging_flag = 1; // show that battery is charging
}
//amps += current;
mAsec = (float)(amps / 180); // mA/0.2Sec
//if(DspUpdInt > 4){ // Display Voltage,Current,Watts every 1 Second
//////////////////////
//***LCD Line 1 ***//
/// Voltage Display ///
//volts = volts /5 ; // Voltage to display
//display_va(0, 0, volts, "V");
//lcd.setCursor(0, 0);
//lcd.print(sign);
lcd.setCursor(0, 0);
lcd.print(String(volts) + "V");
/// Display Current ///
amps = (amps * 10);
//lcd.setCursor(7, 0);
//lcd.print("+" + (String(amps)) + "A");
//display_va(9, 0, amps, "A");
/// Display Wattage ///
va[0] = (amps/1000);
va[1] = ((amps/100)%10);
//va[2] = ".";
va[3] = ((amps/10)%10);
va[4] = ((amps/1)%10);
va[5] = "A";
lcd.setCursor(9, 0);
lcd.print(va);
wt = (volts * amps) * 10;
//lcd.setCursor(15, 0);
//lcd.print("+");
lcd.setCursor(15, 0);
//lcd.print("+" + (String(wt)) + "W");
lcd.print(amps);
//display_percent_watt(0, 17, wt, 'W');
}
//}
void display_va( char col, char row, unsigned long value, char type){
char va[] = "00.00 ";
//va[6] = '\0';
va[0] = (value/1000);
va[1] = ((value/100)%10);
//va[2] = ".";
va[3] = ((value/10)%10);
va[4] = ((value/1)%10);
va[5] = type;
lcd.setCursor(col, row);
//for(int x = 0; x < 7; x++){
//lcd.setCursor(x, pos_row);
//va = "123";
lcd.print(va);
//}
}
void display_percent_watt(char row, char col, unsigned int value, char type){
unsigned char percent[4];// = "000 ";
percent[4] = '\0';
if (value < 100){
percent[0] = ' ';
}
else{
percent[0] = (value/100);
}
percent[1] = ((value/10)%10);
percent[2] = ((value/1)%10);
percent[3] = type;
lcd.setCursor(col, row);
for(int x = 0; x < 7; x++){
lcd.setCursor(x, row);
lcd.print(percent[x]);
}
}
void SpeedoIn(){
}