Hi, I'm quite new to Arduino programming and I'm struggling with the flow / loops etc.
I've built a PWM charge controller using a ATTiny44.
It charges a battery to a set voltage (13.5v) and the PWM is adjusted to keep it there, tapering off towards the set point.
It works quite well, but there is some functions I would like to add and I'm struggling with the logic.
What I want it to do:
Start charging battery and take it to 14.4v. (only once a day)
Once it reaches 14.4v it needs to stop charging and maintain a float charge at 13.5v
Once the solar panel voltage drops below 5v (night time) it needs to turn on a load (led lamps) and m onitor the battery voltage.
If the battery voltage reaches 11v (LDV) or the panel voltage goes above 5v (day time) it needs to disconnect the load and start the charge cycle from step 1. (charge to 14.4v)
This is the sketch I'm using for charging. (PWM)
I got this from a project I found online.
I understand the workings of this sketch, but I'm not sure how to implement the required changes, and more specifically only running the 14.4v boost charge once per daily cycle.
const int setPoint = 13.5 * 21.5 / (21.5+82) * 1024 / 5 ;
const int setDayNight = 5 * 21.5 / (21.5+82) * 1024 / 5 ;
const int setLDV = 11 * 21.5 / (21.5+82) * 1024 / 5 ;
int measurement = 0;
int pulseWidth = 0;
int difference = 0;
int stepSize = 0;
int vinSol = 0;
int calculation = 0;
int led = 9;
if (measurement < setPoint)
{
pulseWidth += stepSize;
if (pulseWidth > 255) pulseWidth = 255;
// digitalWrite(led, LOW); // pwm to LED
}
if (measurement > setPoint)
{
pulseWidth -= stepSize;
if (pulseWidth < 0) pulseWidth = 0;
//digitalWrite(led, HIGH); // pwm to LED
}
analogWrite(5, pulseWidth);
delay(10);
}
vinSol is the solar panel voltage.
I get it with analogRead(A1);
To switch on the load : if (vinSol < setDayNight && measurement > setLDV)
{digitalWrite(2, HIGH);
}
else {
digitalWrite(2, LOW);
}