#include "TimerOne.h" http://www.arduino.cc/playground/Code/Timer1
#include <LiquidCrystal_I2C.h> // using the LCD I2C Library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
#include <Wire.h>
//----------------------------------------------------------------------------------------------------------
//////// Arduino pins Connections//////////////////////////////////////////////////////////////////////////////////
// A0 - Voltage divider (solar)
// A1 - ACS 712 Out
// A2 - Voltage divider (battery)
// A3 - Temperature
// A4 - LCD SDA
// A5 - LCD SCL
// D5 - LCD back control button
// D8 - 2104 MOSFET driver SD
// D9 - 2104 MOSFET driver IN
// D10 - Relay Control
// D11 - Green LED
// D12 - Yellow LED
// D13 - Red LED
///////// Definitions /////////////////////////////////////////////////////////////////////////////////////////////////
#define SOL_VOLTS_CHAN 0 // defining the adc channel to read solar volts
#define SOL_AMPS_CHAN 1 // Defining the adc channel to read solar amps
#define BAT_VOLTS_CHAN 2 // defining the adc channel to read battery volts
#define AVG_NUM 10 // number of iterations of the adc routine to average the adc readings
// ACS 712 Current Sensor is used. Current Measured = (5/(1023 *0.025))*ADC - (2.5/0.025)
// ACS 758 Current Sensor is used. Current Measured = (5/(1023 *0.04))*ADC - (2.5/0.04)
#define SOL_AMPS_SCALE 0.1955034213098729227761485826002 // the scaling value for raw adc reading to get solar amps // 5/(1024*0.066)
#define SOL_VOLTS_SCALE 0.1955034213098729227761485826002 // the scaling value for raw adc reading to get solar volts // (5/1024)*((R1*R2)/(R1+R2))
#define BAT_VOLTS_SCALE 0.0977517106549364613880742913001 // the scaling value for raw adc reading to get battery volts
//#define BAT_AMPS_SCALE 0.364311 // the scaling value for raw adc reading to get battery amps // 5/(1023*0.04)
#define PWM_PIN 9 // the output pin for the pwm (only pin 9 avaliable for timer 1 at 25kHz)
#define PWM_ENABLE_PIN 8 // pin used to control shutoff function of the IR2104 MOSFET driver (hight the mosfet driver is on)
#define PWM_FULL 1023 // the actual value used by the Timer1 routines for 100% pwm duty cycle
#define PWM_MAX 100 // the value for pwm duty cyle 0-100%
#define PWM_MIN 18 // the value for pwm duty cyle 0-100% (below this value the current running in the system is = 0)
#define PWM_START 2 // the value for pwm duty cyle 0-100%
#define PWM_INC 1 //the value the increment to the pwm value for the ppt algorithm
#define TRUE 1
#define FALSE 0
#define ON TRUE
#define OFF FALSE
#define TURN_ON_MOSFETS digitalWrite(PWM_ENABLE_PIN, HIGH) // enable MOSFET driver
#define TURN_OFF_MOSFETS digitalWrite(PWM_ENABLE_PIN, LOW) // disable MOSFET driver
#define ONE_SECOND 25000 //count for number of interrupt in 1 second on interrupt period of 40us
#define ABSORPTION_LIMIT 3600000 // 1 hour in milliseconds
#define NIGHT_TIME 3600000 // 1 hour in milliseconds
#define CHARGER_RESTART_TIME 600000 // 10 mins in milliseconds
#define MIN_SOL_VOLTS 10.00
#define LOW_SOL_VOLTS 15.00
#define LOW_SOL_WATTS 2.00 //value of solar watts // this is 2 .00watts
#define MIN_SOL_WATTS 1.00 //value of solar watts // this is 1.00 watts
#define MIN_BAT_VOLTS 8.00 //value of battey voltage // this is 8.00 volts
#define MAX_BAT_VOLTS 14.80 //value of battery voltage// this is 14.40 volts
#define BULK_CH_SP 14.20 //value of bulk charge setpoint//this is 14.10 volts
#define FLOAT_CHARGE_SP 13.80
#define Charge_Restart_SP 13.20
#define OFF_NUM 9 // number of iterations of off charger state
//#define MAX_BAT_AMPS 60
float MAX_BAT_AMPS = 80.00;
//------------------------------------------------------------------------------------------------------
//Defining led pins for indication
#define LED_GREEN 11
#define LED_YELLOW 12
#define LED_RED 13
//-----------------------------------------------------------------------------------------------------
// Defining relay control pin
#define RELAY_PIN 10 // pin-10 is used to control the relay
const float RELAY_PIN_ON = 5;
const float RELAY_PIN_OFF = 3;
//-----------------------------------------------------------------------------------------------------
// Defining lcd back light pin
#define BACK_LIGHT_PIN 5 // pin-5 is used to control the lcd back light
//-------------------------------------------------------------------------------------------------------
// global variables
int count = 0;
int pwm = 0; // pwm duty cycle 0-100%
float bulk_charge_sp=0;
float float_charge_sp=0;
float system_volt=0;
float Charge_Restart_V=0;
float min_sol_volts=0;
float low_sol_volts=0;
int eff = 0;
float sol_amps; // solar amps
float sol_volts; // solar volts
float bat_amps; // battery amps
float bat_volts; // battery volts
float batcurr;
float sol_watts; // solar watts
float old_sol_watts = 0; // solar watts from previous time throughppt routine
float bat_watts; // battery watts
float msec=0;
float last_msec=0;
float elasped_msec=0;
float elasped_time=0;
float ampSecs = 0;
float ampHours=0;
float watts=0;
float wattSecs = 0;
float wattHours=0;
unsigned int seconds = 0; // seconds from timer routine
unsigned long absorption_time; // to keep track of today's time in absorption state
unsigned long charger_millis;
unsigned long restart_time;
unsigned int prev_seconds = 0; // seconds value from previous pass
unsigned int interrupt_counter = 0; // counter for 40us interrrupt
boolean led_on = TRUE;
int delta = PWM_INC; // variable used to modify pwm duty cycle for the ppt algorithm
float I_OUTPUT_MAX = 40; //maximum current
enum charger_mode {off, on, bulk, absorption, bat_float,} charger_state; // enumerated variable that holds state for charger state machine
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27,20,4); // Set the LCD I2C address
long time = 0; // variable to store time the back light control button was pressed in millis
int relay_pin = 10;
int back_light_pin_State = 0;
int relay_status=0;
int tem;
const int screennTime = 5000;
int screennTimer = 0;
byte screenPage = 1;
//byte IDLE_FLAG = 0;
byte solar[8] = //icon for solar
{
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
0b00000
};
byte battery[8]=
{
0b01110,
0b11011,
0b10001,
0b10001,
0b11111,
0b11111,
0b11111,
0b11111,
};
byte degree[8] =
{
0b00011,
0b00011,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
//------------------------------------------------------------------------------------------------------
// This routine is automatically called at powerup/reset
//------------------------------------------------------------------------------------------------------
void setup() // run once, when the sketch starts
{
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
digitalWrite(LED_RED,LOW);
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_YELLOW,LOW);
pinMode(PWM_ENABLE_PIN, OUTPUT); // sets the digital pin as output
pinMode(7, OUTPUT);
digitalWrite(7 ,LOW);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
Timer1.initialize(40); // initialize timer1, and set a 40uS period
Timer1.pwm(PWM_PIN, 0); // setup pwm on pin 9, 0% duty cycle
TURN_OFF_MOSFETS; // turn off MOSFET driver chip
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
//Serial.begin(9600); // open the serial port at 38400 bps:
pwm = PWM_START; // starting value for pwm
pinMode(BACK_LIGHT_PIN, INPUT);
pinMode(RELAY_PIN,OUTPUT);
digitalWrite(RELAY_PIN,LOW); // default relay state is OFF
digitalWrite(BACK_LIGHT_PIN,LOW); // default LCd back light is OFF
charger_state = off; // start the charger from off
charger_millis = millis() ; // initialise the local clock
lcd.init(); // initialize the lcd for 20 chars 4 lines, turn on backlight
lcd.backlight();
lcd.print(" Initialising MPPT ");
delay(1000);
lcd.clear();
lcd.createChar(1,solar);
lcd.createChar(2,battery);
lcd.createChar(3,degree);
lcd.noBacklight();
}
//------------------------------------------------------------------------------------------------------
// Main loop
//------------------------------------------------------------------------------------------------------
void loop()
{
read_data(); // read data from inputs
setpoint(); // decide the charge set point according to system voltage
system_voltage(); // detect the system voltage according to battery voltage
run_charger(); // run the charger state machine
run_power (); // read the power
// print_data(); // print data
relay_control(); // control the relay
ledIndication(); // led indication
amps_control();
temperature();
displayEff(); // display efficiency
buzzer_on();
backLight_timer();
// led_blink();
if ((millis() - screennTimer) >= screennTime) {
screenPage ++;
delay(5000);
if (screenPage > 2) {
screenPage = 1;
}
lcd.clear();
screennTimer = millis();
}
lcd_display(screenPage); // lcd display
}
//------------------------------------------------------------------------------------------------------
// This routine reads and averages the analog inputs for this system, solar volts, solar amps,
// battery volts and battery amps.
//------------------------------------------------------------------------------------------------------
int read_adc(int channel){
int sum = 0;
int temp;
int i;
for (i=0; i<AVG_NUM; i++) { // loop through reading raw adc values AVG_NUM number of times
temp = analogRead(channel); // read the input pin
sum += temp; // store sum for averaging
delayMicroseconds(50); // pauses for 5000 microseconds
}
return(sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it
}
//------------------------------------------------------------------------------------------------------
// This routine reads all the analog input values for the system. Then it multiplies them by the scale
// factor to get actual value in volts or amps.
//------------------------------------------------------------------------------------------------------
void read_data(void) {
sol_amps = (read_adc(SOL_AMPS_CHAN) * SOL_AMPS_SCALE -100); //input of solar amps
sol_volts = read_adc(SOL_VOLTS_CHAN) * SOL_VOLTS_SCALE; //input of solar volts
//bat_amps = (read_adc(BAT_AMPS_CHAN) * BAT_AMPS_SCALE -185.54 ); //input of solar amps
bat_volts = read_adc(BAT_VOLTS_CHAN) * BAT_VOLTS_SCALE; //input of battery volts
sol_watts = sol_amps * sol_volts ; //calculations of solar watts
bat_watts = batcurr * bat_volts ;
batcurr = ((sol_watts/bat_volts)* 0.90);
if (sol_amps <=0)
(sol_amps =0);
if (bat_amps <=0)
(bat_amps =0);
if (batcurr <=0)
(batcurr =0);
if (sol_watts <=0)
(sol_watts =0);
}
//------------------------------------------------------------------------------------------------------
// This is interrupt service routine for Timer1 that occurs every 40uS.
//
//------------------------------------------------------------------------------------------------------
void callback()
{
if (interrupt_counter++ > ONE_SECOND) { //increment interrupt_counter until one second has passed
interrupt_counter = 0;
seconds++; //then increment seconds counter
}
}
//------------------------------------------------------------------------------------------------------
// This routine uses the Timer1.pwm function to set the pwm duty cycle.
//------------------------------------------------------------------------------------------------------
void set_pwm_duty(void) {
if (pwm > PWM_MAX) { // check limits of PWM duty cyle and set to PWM_MAX
pwm = PWM_MAX;
}
else if (pwm < PWM_MIN) { // if pwm is less than PWM_MIN then set it to PWM_MIN
pwm = PWM_MIN;
}
if (pwm < PWM_MAX) {
Timer1.pwm(PWM_PIN,(PWM_FULL * (long)pwm / 100), 40); // use Timer1 routine to set pwm duty cycle at 40uS period
//Timer1.pwm(PWM_PIN,(PWM_FULL * (long)pwm / 100));
}
else if (pwm == PWM_MAX) { // if pwm set to 100% it will be on full but we have
Timer1.pwm(PWM_PIN,(PWM_FULL - 1), 40); // keep switching so set duty cycle at 99.9%
//Timer1.pwm(PWM_PIN,(PWM_FULL - 1));
}
}
//------------------------------------------------------------------------------------------------------
// This routine is the charger state machine. It has four states on, off, bulk and float.
// It's called once each time through the main loop to see what state the charger should be in.
// The battery charger can be in one of the following four states:
//
// On State - this is charger state for MIN_SOL_WATTS < solar watts < LOW_SOL_WATTS. In this state isthe solar
// watts input is too low for the bulk charging state but not low enough to go into the off state.
// In this state we just set the pwm = 99.9% to get the most of low amount of power available.
// Bulk State - this is charger state for solar watts > MIN_SOL_WATTS. This is where we do the bulk of the battery
// charging and where we run the Peak Power Tracking alogorithm. In this state we try and run the maximum amount
// of current that the solar panels are generating into the battery.
// Float State - As the battery charges it's voltage rises. When it gets to the MAX_BAT_VOLTS we are done with the
// bulk battery charging and enter the battery float state. In this state we try and keep the battery voltage
// at MAX_BAT_VOLTS by adjusting the pwm value. If we get to pwm = 100% it means we can't keep the battery
// voltage at MAX_BAT_VOLTS which probably means the battery is being drawn down by some load so we need to back
// into the bulk charging mode.
// Off State - This is state that the charger enters when solar watts < MIN_SOL_WATTS. The charger goes into this
// state when there is no more power being generated by the solar panels. The MOSFETs are turned
// off in this state so that power from the battery doesn't leak back into the solar panel.
//------------------------------------------------------------------------------------------------------
void run_charger(void) {
static int off_count = OFF_NUM;
switch (charger_state) {
case on:
if (sol_volts < MIN_SOL_VOLTS) { //if watts input from the solar panel is less than
charger_state = off; //the minimum solar watts then
off_count = OFF_NUM; //go to the charger off state
TURN_OFF_MOSFETS;
}
else if (sol_volts < LOW_SOL_VOLTS) { //else if the solar input watts is less than low solar watts
charger_state = on; //it means there is not much power being generated by the solar panel
TURN_ON_MOSFETS; //so go to charger on state
}
else if (sol_volts < LOW_SOL_VOLTS) { //else if the solar input watts is less than low solar watts
pwm = PWM_MIN; //it means there is not much power being generated by the solar panel
set_pwm_duty(); //so we just set the pwm = 60% so we can get as much of this power as possible
} //and stay in the charger on state
else {
pwm = ((bat_volts * 10) / (sol_volts / 10)) + 5; //else if we are making more power than low solar watts figure out what the pwm
charger_state = bulk; //value should be and change the charger to bulk state
}
break;
case bulk:
if (sol_volts < MIN_SOL_VOLTS) { //if watts input from the solar panel is less than
charger_state = off; //the minimum solar watts then it is getting dark so
off_count = OFF_NUM; //go to the charger off state
TURN_OFF_MOSFETS;
}
else if (bat_volts > bulk_charge_sp) { //else if the battery voltage has gotten above the float
charger_state = bat_float; //battery float voltage go to the charger battery float state
}
else { // this is where we do the Peak Power Tracking ro Maximum Power Point algorithm
if (old_sol_watts >= sol_watts) { // if previous watts are greater change the value of
delta = -delta; // delta to make pwm increase or decrease to maximize watts
}
pwm += delta; // add delta to change PWM duty cycle for PPT algorythm (compound addition)
old_sol_watts = sol_watts; // load old_watts with current watts value for next time
set_pwm_duty(); // set pwm duty cycle to pwm value
}
break;
case bat_float:
if (sol_volts < MIN_SOL_VOLTS) { //if watts input from the solar panel is less than
charger_state = off; //the minimum solar watts then it is getting dark so
off_count = OFF_NUM; //go to the charger off state
set_pwm_duty();
TURN_OFF_MOSFETS;
}
else if (bat_volts > bulk_charge_sp) { //since we're in the battery float state if the battery voltage
pwm -= 1; //is above the float voltage back off the pwm to lower it
set_pwm_duty();
charger_state = bat_float;
}
else if (bat_volts < Charge_Restart_V) { //else if the battery voltage is less than the float voltage
pwm += 1; //increment the pwm to get it back up to the float voltage
set_pwm_duty();
charger_state = bulk; //float voltage so jump to charger bulk state to charge the battery
}
break;
case off: //when we jump into the charger off state, off_count is set with OFF_NUM
if (off_count > 0) { //this means that we run through the off state OFF_NUM of times with out doing
off_count--; //anything, this is to allow the battery voltage to settle down to see if the
} //battery has been disconnected
else if (sol_volts < MIN_SOL_VOLTS) { //else if the solar input watts is less than low solar watts
pwm = PWM_START; //it means there is not much power being generated by the solar panel
set_pwm_duty();
}
else if ((bat_volts > MIN_BAT_VOLTS) && (tem < 65) && (bat_volts < bulk_charge_sp) && (sol_volts > (bat_volts + 5))) {
pwm = PWM_START; //if battery volts aren't quite so high but we have solar volts
set_pwm_duty(); //greater than battery volts showing it is day light then
charger_state = on; //change charger state to on so we start charging
TURN_ON_MOSFETS;
}
break;
default:
TURN_OFF_MOSFETS;
break;
}
}
//------------------------------------------------------------------------------------------------------------
//--------------------------------------------------BATTERY AMPS CONTROL----------------------------------------
void amps_control(void){
const float I_PROP_RESPONSE = 10;
float dutyResponse = 0;
int iDutyResponse = 0;
if (batcurr > MAX_BAT_AMPS) {
float err = batcurr - MAX_BAT_AMPS;
dutyResponse = err * I_PROP_RESPONSE;
}
if (batcurr > MAX_BAT_AMPS) { //since we're in the battery float state if the battery voltage
pwm -= 1; //is above the float voltage back off the pwm to lower it
set_pwm_duty();
}
return true;
}
//----------------------------------------------------------------------------------------------------------
//--------------------------------------POWER---------------------------------------------------------------
void run_power(void){
if ((charger_state == bulk) || (charger_state == on) || (charger_state==bat_float))
msec = millis();
elasped_msec = msec - last_msec; //Calculate how long has past since last call of this function
elasped_time = elasped_msec / 1000.0; // 1sec=1000 msec
watts = batcurr * bat_volts; //Watts now
ampSecs = (batcurr * elasped_time); //AmpSecs since last measurement
wattSecs = ampSecs * bat_volts; //WattSecs since last measurement
ampHours = ampHours + ampSecs/3600; // 1 hour=3600sec //Total ampHours since program started
wattHours = wattHours + wattSecs/3600000; // 1 hour=3600sec //Total wattHours since program started
last_msec = msec; //Store 'now' for next time
}
//----------------------------------------------------------------------------------------------------------------------
//////////////////////////////////SYSTEM VOLTAGE AUTO DETECT ///////////////////////////////////////////////////////////
//----------------------------------------------------------------------------------------------------------------------
void system_voltage(void)
{
if ((bat_volts > MIN_BAT_VOLTS ) && (bat_volts < MAX_BAT_VOLTS))
{
system_volt = 12;
}
else if ((bat_volts > MIN_BAT_VOLTS*2 ) && (bat_volts < MAX_BAT_VOLTS*2))
{
system_volt = 24;
}
else if ((bat_volts > MIN_BAT_VOLTS*4 ) && (bat_volts < MAX_BAT_VOLTS*4))
{
system_volt = 48;
}
else if ((bat_volts > MIN_BAT_VOLTS*8 ) && (bat_volts < MAX_BAT_VOLTS*8))
{
system_volt = 96;
}
else if ((system_volt < MIN_BAT_VOLTS ) || (system_volt > MAX_BAT_VOLTS))
{
system_volt = 0;
}
}
//--------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------TEMPERATURE-----------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
void temperature (void)
{
tem=analogRead(A3);
tem=map(tem, 0, 1023, 0, 100);
//12V
if((bat_volts <= 10.4) && (system_volt == 12))
{
digitalWrite(7,HIGH);
}
if((bat_volts >= 11.5) && (system_volt == 12))
{
digitalWrite(7,LOW);
}
//24V
if((bat_volts <= 21.0) && (system_volt == 24))
{
digitalWrite(7,HIGH);
}
if((bat_volts >= 23.0) && (system_volt == 24))
{
digitalWrite(7,LOW);
}
//48V
if((bat_volts <= 42.0) && (system_volt == 48))
{
digitalWrite(7,HIGH);
}
if((bat_volts >= 45.0) && (system_volt == 48))
{
digitalWrite(7,LOW);
}
//96v
if((bat_volts <= 88.0) && (system_volt == 96))
{
digitalWrite(7,HIGH);
}
if((bat_volts >= 90.0) && (system_volt == 96))
{
digitalWrite(7,LOW);
}
if(tem>59){
//digitalWrite(7, HIGH); //fan on
}
if(tem<=45){
//digitalWrite(7, LOW); //fan off
}
if(tem>100){
tem=100;}
if (tem<0){
tem=0;}
}
//----------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------buzzer----------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------
void buzzer_on (void){
if (tem>=65){
digitalWrite(4, HIGH);
}
else if(tem<=62){
digitalWrite(4, LOW);
}
}
//---------------------------------------------------------------------------------------------------------------------------
////////////////////////////////////CHARGE SET POINT ///////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------------------------------------------------------
void setpoint(void)
{
if(system_volt ==12)
{
bulk_charge_sp = (BULK_CH_SP);
float_charge_sp=(FLOAT_CHARGE_SP);
Charge_Restart_V=(Charge_Restart_SP);
min_sol_volts=(MIN_SOL_VOLTS);
low_sol_volts=(LOW_SOL_VOLTS);
}
else if(system_volt == 24)
{
bulk_charge_sp = (BULK_CH_SP*2);
float_charge_sp=(FLOAT_CHARGE_SP*2);
Charge_Restart_V=(Charge_Restart_SP*2);
min_sol_volts=(MIN_SOL_VOLTS*2);
low_sol_volts=(LOW_SOL_VOLTS*2);
}
else if(system_volt == 48)
{
bulk_charge_sp = (BULK_CH_SP*4);
float_charge_sp=(FLOAT_CHARGE_SP*4);
Charge_Restart_V=(Charge_Restart_SP*4);
min_sol_volts=(MIN_SOL_VOLTS*4);
low_sol_volts=(LOW_SOL_VOLTS*4 );
}
else if(system_volt == 96)
{
bulk_charge_sp = (BULK_CH_SP*8);
float_charge_sp=(FLOAT_CHARGE_SP*8);
Charge_Restart_V=(Charge_Restart_SP*8);
min_sol_volts=(MIN_SOL_VOLTS*8);
low_sol_volts=(LOW_SOL_VOLTS*8);
}
}
//----------------------------------------------------------------------------------------------------------------------
/////////////////////////////////////////////RELAY CONTROL/////////////////////////////////////////////////////
//----------------------------------------------------------------------------------------------------------------------
void relay_control(void)
{
float pIn = sol_volts * sol_amps;
if (batcurr > RELAY_PIN_ON) // relay will on
{
// digitalWrite(RELAY_PIN, HIGH); // relay is ON
}
else if (batcurr < RELAY_PIN_OFF){
//digitalWrite(RELAY_PIN, LOW); //relay is OFF
}
}
//------------------------------------------------------------------------------------------------------
//------------------------------------------------EFFICIENCY--------------------------------------------
//------------------------------------------------------------------------------------------------------
void displayEff(void) {
float pIn = sol_amps * sol_volts ;
float pOut = batcurr * bat_volts ;
if ((charger_state == bulk) || (charger_state == on) || (charger_state==bat_float))
{
//conversion efficiency
eff = (int)(pOut / pIn * 100);
}
if (eff<0){
eff=0;
}
if (eff>100){
eff=100;
}
}
//------------------------------------------------------------------------------------------------------
// This routine to indicate charger state using Green, Yellow, and red LEDs
//------------------------------------------------------------------------------------------------------
void ledIndication(void) {
if (charger_state == off){
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_YELLOW,LOW);
digitalWrite(LED_RED,HIGH);
}
else if ((charger_state == bulk) || (charger_state == on)) {
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_YELLOW,HIGH);
digitalWrite(LED_RED,LOW);
}
else if (charger_state == bat_float) {
digitalWrite(LED_GREEN,HIGH);
digitalWrite(LED_YELLOW,LOW);
digitalWrite(LED_RED,LOW);
}
}
//------------------------------------------------------------------------------------------------------
//-------------------------- LCD DISPLAY --------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void lcd_display(byte page)
{
switch (page) {
case 1:
lcd.setCursor(0, 0);
lcd.print(F("NATHYTECH SOLARS "));
lcd.setCursor(0, 1);
lcd.print(F("SYSTEM:"));
lcd.setCursor(7, 1);
if (system_volt ==12)
lcd.print("12 VOLTS" );
else if (system_volt ==24)
lcd.print("24VOLTS" );
else if (system_volt ==48)
lcd.print("48VOLTS" );
else if (system_volt ==96)
lcd.print("96VOLTS" );
else if (system_volt ==0)
lcd.print("BATTERY ERROR" );
break;
case 2:
lcd.setCursor(0, 0);
lcd.write(1);
lcd.print(sol_volts, 1);
//lcd.print(F("V"));
lcd.setCursor(0, 1);
lcd.print(sol_amps, 1);
lcd.print(F("A"));
lcd.setCursor(6, 1);
lcd.print(sol_watts, 1);
lcd.print(F("W "));
lcd.setCursor(7, 0);
lcd.write(2);
lcd.print(bat_volts, 1);
// lcd.print(F("V"));
lcd.setCursor(12, 1);
lcd.print(batcurr, 1);
lcd.print(F("A"));
lcd.setCursor(13, 0);
if (charger_state == on)
lcd.print(F("ON"));
else if (charger_state == off)
lcd.print(F("OFF"));
else if (charger_state == bulk)
lcd.print(F("BULK"));
else if (charger_state == bat_float)
lcd.print(F("FLOAT"));
break;
}
delay(500);
}
void backLight_timer(void){
back_light_pin_State = digitalRead(BACK_LIGHT_PIN);
if (back_light_pin_State == HIGH)
{
time = millis(); // If any of the buttons are pressed, save the time in millis to "time"
}
if ((millis() - time) <= 300000)
lcd.backlight();
else
lcd.noBacklight();
}
//The End