I am attempting to make an automatic battery charger and have made a simple prototype using a potentiometer connected to an analog pin in place of the battery to act like the battery changing voltage during use, with an led connected to a pwm pin to show when charging would be taking place. My code so far, where highVoltage is the battery voltage at which charging should stop, and lowVoltage is the battery voltage at which charging should start, is as follows:
void loop {
if (voltage >= highVoltage) {
analogWrite(led, 0);
}
if (voltage <= lowVoltage)
analogWrite(led, 255);
}
I'd like to be able to change the charging voltage through pwm at a point between highVoltage and lowVoltage so that the charging voltage is reduced when the battery level reaches about 80%.
I've tried nesting if statements as follows:
void loop {
if (voltage >= highVoltage) {
analogWrite(led, 0);
}
if (voltage <= lowVoltage) {
if (voltage <= 80%) {
analogWrite(led, 255);
}
if (voltage < highVoltage) {
analogWrite(led, 150);
}
}
}
The output when the battery voltage is above 80% does not change though. Can anyone suggest how I can get this working?
The text editor for this forum doesn't let you tab. Here is the code copied from my sketch, all properly indented, and still doing exactly the same thing. Unfortunately the solution is no more clear. Sorry for my laziness.
I've added specific voltages to try and make this clearer, and changed the last if statement. It seems to behave in the same way though ie the led turns on at 1.5v or less and stays on until the voltage is 4.8 or more. The led brightness does not appear to change between 4 and 4.8 volts which would indicate that the voltage supplied is not being reduced by pwm. Please could anyone let me know how this can be achieved?
void loop {
if (voltage >= 4.8 ) {
analogWrite(led, 0);
}
if (voltage <= 1.5) {
if (voltage <= 4) {
analogWrite(led, 255);
}
if(voltage > 4 && voltage < 4.8 ) {
analogWrite(led, 100);
}
}
}
Thank you for helping me through my first post. If I understand your questions correctly, answers are as follows:
led is connected to pin 9 for pwm.
I'm using an arduino uno.
I'm reading voltage going through the potentiometer on A0.
To elaborate further, when the reading on the potentiometer goes below 1.5v I want the led turned on at 100% pwm until the potentiometer reached 4v, and then for the led to be turned on at 100/255% between 4v and 4.8v. Above 4.8v the led should switch off.
if (voltage <= 1.5) {
if (voltage <= 4) {
analogWrite(led, 255);
}
if(voltage > 4 && voltage < 4.8 ) {
analogWrite(led, 100);
}
}
If the voltage is less than, or equal 1.5, it has to be below 4, doesn't it? There is no way that a value below 1.5 can be between 4 and 4.8.
You need one if statement and several else if statements, with no nesting. Deal with one end of the range first, then progressively deal with the rest of the range.
I see what you are saying but my aim is that once the voltage reaches 4.8 the led will stay off until the voltage reaches 1.5. Consider my first code block:
void loop {
if( voltage >= 4.8) {
analogWrite(led, 0); //if voltage goes above 4.8 volts the led turns off and stays off until voltage = 1.5v
}
if( voltage <= 1.5) {
analogWrite(led, 255) //if voltage goes below 1.5 volts the led turns on and stays on until voltage = 4.8v
}
}
So in the 2nd if statement, the led will remain on even if the the voltage goes above 1.5 volts. This is how I want it to work, apart from the requirement the the pwm voltage to the led should drop when the potentiometer voltage is between 4 and 4.8 volts. Once the voltage gets to 4.8 volts the led should turn off until it gets to 1.5 volts again. As I understand, what you have suggested would keep the led on at all voltages below 4.8 volts, albeit a different pwm levels, as per the following code:
With respect to the first code, you are only dealing with the cases where voltage is above 4.8 and where voltage is below 1.5. For voltages in between, nothing happens. As the voltage drops from 5.0 to 4.5, there is nothing that will make the LED turn on. As the voltage drops from 1.75 to 1.25, the LED will come on.
As the voltage rises from 1.25 to 1.75, nothing will happen. The LED was on, so it will stay on.
As the voltage rises from 4.5 to 5.0, the LED will turn off somewhere in between.
To allow the voltage to drop to 1.5V before switching on the charger again, you need to keep track of the "state" of the battery, with a variable named like WasCharged to remember that it was charged and the charger doesn't need to come on yet.
Thanks MorganS - That solved my problem exactly. I'm not sure what the code you posted was for though? Any further suggestions of how I could shorten my code and how to allow me to assign a string to batteryState variable would be welcome. I've seriously just tried to google 'arduino string variables' but none of what I found was very clear.
My code is as follows:
int led = 9;
int batteryState = 1;
void setup() {
// put your setup code here, to run once:
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// initialize digital pin 13 as an output.
pinMode(led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
if (voltage >= 4.8) {
analogWrite(led, 0);
batteryState = 1;
}
else if (voltage >= 4 && batteryState == 1) {
analogWrite(led, 0);
}
else if (voltage >= 4 && batteryState == 0) {
analogWrite(led, 100);
}
else if (voltage >= 1.5 && batteryState == 1) {
analogWrite(led, 0);
}
else if (voltage >= 1.5 && batteryState == 0) {
analogWrite(led, 255);
}
else if (voltage < 1.5) {
analogWrite(led, 255);
batteryState = 0;
}
}
richardhope:
I'd like to be able to change the charging voltage through pwm at a point between highVoltage and lowVoltage so that the charging voltage is reduced when the battery level reaches about 80%.
My line of code was attempting to answer this question.
Your use of batteryState looks good although how do you remember if "1" means charged? I would modify your code as follows:
int led = 9;
const int AlreadyCharged = 1;
const int Charging = 0;
int batteryState = Charging;
void setup() {
// put your setup code here, to run once:
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// initialize digital pin 13 as an output.
pinMode(led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
if (voltage >= 4.8) {
analogWrite(led, 0);
batteryState = AlreadyCharged ;
}
else if (voltage >= 4 && batteryState == AlreadyCharged ) {
analogWrite(led, 0);
}
else if (voltage >= 4 && batteryState == Charging ) {
analogWrite(led, 100);
}
else if (voltage >= 1.5 && batteryState == AlreadyCharged ) {
analogWrite(led, 0);
}
else if (voltage >= 1.5 && batteryState == Charging ) {
analogWrite(led, 255);
}
else if (voltage < 1.5) {
analogWrite(led, 255);
batteryState = Charging ;
}
}