/*
**************************************************************
* POWER & SWR meter on 2x16 char LCD display *
* By ON7EQ Aug 2011 *
**************************************************************
The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* GND to LCD VO pin (pin 3) (contrast)
* pin 13 = LCD backlight control (1= backlight ON). Use an emittor follower (like BC639) to control LCD backlight (this draws about 120mA)
* pin 4 = connect a piezo buzzer (other end to ground)
* pin A0 : SWR probe FWD voltage input (SWR bridge type 'MONIMATCH')
* pin A1 : SWR probe REFL voltage input
! Set the diode forward voltage in variable 'Diode'. For 1SS99 or other Schottky diodes, it is around 130mV @ 0,1mA
* pin A5 : Power Supply Voltage input
MODE SELECT : there are 2 possibilities to select mode :
--------------------------------------------------------
1° with 3 position toggle switch:
* pin Digital 2 : 'bip' Sound Switch : when grounded -> SWR 'bip' tone mode
* pin Digital 3 : PEP mode Switch : when grounded -> PEP mode
When switch in center position : normal power/swr mode, instant power & dBm readout
When using a center toggle switch, Pin 5 must be connected to +5v
If a 3-way toggle switch is present, it will be automatically detected and the variable 'PushButton' set accordingly.
2° with push button for cyling modes:
* pin Digital 5 : Mode push button : when grounded (pulse), next mode is selected
When using a push button, Pins 2 and 3 must be connected to +5v !
! the mode is stored in EEPROM & memorized for next boot
BAND SELECT :
-------------
It can be necessary to apply different parameters / calibration factors per band.
* pin Digital 6 : BAND push button : when grounded (pulse), next band is selected
! the band is stored in EEPROM & memorized for next boot
*/
// include the LCD library code:
//#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
// include math functions
#include "math.h"
// include EEPROM write
#include <EEPROM.h>
int a,b,c;
long volt,current,power,ah;
unsigned long msec = 0;
float time = 0.0;
int sample = 0;
float totalCharge = 0.0;
float averageAmps = 0.0;
float ampSeconds = 0.0;
long ampHours = 0;
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
LiquidCrystal_I2C lcd(0x27, 20, 4);
// *************************************************************
// ******************** S E T U P ************************
// *************************************************************
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop()
{
for(int i=0;i<200;i++){
c = analogRead(A1);
a = analogRead(A0);
volt=volt+a;
current=current+c;
delay(1);
}
current=(current/200-514);
if(current<1)current=0;
current=current*7;// change the number to different value to calibrate the current sensor
volt=volt/30;
power=(volt*current)/1000;
sample = sample + 1;
msec = millis();
time = (float) msec / 1000.0;
totalCharge = totalCharge + (current);
averageAmps = totalCharge / sample;
ampSeconds = averageAmps*time;
ampHours = ampSeconds/3600;
Serial.print(volt);
Serial.print(" ");
Serial.print(current);
Serial.print(" ");
Serial.print(power);
Serial.print(" ");
Serial.print(ampHours);
Serial.print(" ");
Serial.println(time);
lcd.setCursor(6,0);
lcd.print("V");
b=volt%10;
lcd.setCursor(5,0);
lcd.print(b);
b=(volt/10)%10;
lcd.setCursor(4,0);
lcd.print(b);
lcd.setCursor(3,0);
lcd.print(".");
b=(volt/100)%10;
lcd.setCursor(2,0);
lcd.print(b);
b=(volt/1000)%10;
lcd.setCursor(1,0);
if(volt>999)lcd.print(b);
else lcd.print(" ");
lcd.setCursor(14,0);
lcd.print("A");
b=current%10;
lcd.setCursor(13,0);
lcd.print(b);
b=(current/10)%10;
lcd.setCursor(12,0);
lcd.print(b);
lcd.setCursor(11,0);
lcd.print(".");
b=(current/100)%10;
lcd.setCursor(10,0);
lcd.print(b);
lcd.setCursor(6,1);
lcd.print("W");
b=power%10;
lcd.setCursor(5,1);
lcd.print(b);
b=(power/10)%10;
lcd.setCursor(3,1);
lcd.print(b);
lcd.setCursor(4,1);
lcd.print(".");
b=(power/100)%10;
lcd.setCursor(2,1);
if(power>99)lcd.print(b);
else lcd.print(" ");
b=(power/1000)%10;
lcd.setCursor(1,1);
if(power>999)lcd.print(b);
else lcd.print(" ");
//------------
lcd.setCursor(14,1);
lcd.print("AH");
b=ampHours%10;
lcd.setCursor(13,1);
lcd.print(b);
b=(ampHours/10)%10;
lcd.setCursor(12,1);
lcd.print(b);
lcd.setCursor(11,1);
lcd.print(".");
b=(ampHours/100)%10;
lcd.setCursor(10,1);
lcd.print(b);
b=(ampHours/1000)%10;
lcd.setCursor(9,1);
lcd.print(b);
delay(100);
}