Hi,
Im a beginner on the arduino and programming !
I have been having fun learning the basics and now would like to attempt
to make a battery swapper !
I would like to start by swapping two batterries ( make the primary the charging and make the charging
the primary .
But im starting slowly , firstly Im learning how to monitor a 12v battery via voltge divider ,
and read the value and send it to serial monitor .
So i am experimenting ....
I have put a circuit together and monitor the voltage of a 12volt battery .
for testing purposes i added a trim pot to manipulate the voltage of the 12v battery myself.
I set it to switch relay off if sensorValue is below or equal to 400 .
this works , but i have a problem !
when the sensorValue reaches 400(or batterylevel) it fluctuates as it will be under load .
so sensorValue will be at 400 399 401 for a while and the relay connects and disconnects !
how do i turn it off once and only allow the relay to turn back on once i tell it to ?
Iv gone into dumb mode thinking about it ,
I hope iv explained my problem , if not let me know .
any help with direction is appreciated .
I know i can do this by time and not by sensing voltage , i would like to see if its possible
by reading the value of the voltage and switching relays on or off when voltages get to
a set level .
Hais.
// These constants won't change. They're used to give names
// to the pins used:
const int pot = A0; // Analog input pin that the potentiometer is attached to
const int redled = 9; // Digital output pin that the LED is attached to
const int yellowled = 2; // LED yellow on digital pin 2
long previousmillis = 0;
int redstate = LOW;
int yellowstate = LOW;
int sensorValue = 0; // value read from the pot
int outputValue = 0;
int val = 0;
int volts =0;
int relay = 11;
int relaystate = LOW;
long interval = 300;
// value output to the PWM (analog out)
void setup() {
pinMode (redled, OUTPUT);
pinMode (relay, OUTPUT);
pinMode (yellowled, OUTPUT);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(pot);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
unsigned long currentmillis = millis();
if (currentmillis - previousmillis > interval) {
previousmillis = currentmillis;
if (redstate == LOW)
redstate = HIGH;
else
redstate = LOW;
digitalWrite (redled , redstate);
}
if ( sensorValue >= 400) // if volatge is over x volatge
{
digitalWrite (relay, HIGH);
digitalWrite (yellowled, HIGH);
}
else {
digitalWrite (relay , LOW);
digitalWrite (yellowled, LOW);
}
// volts = (sensorValue / (sensorValue/11);
Serial.print ("Volts To be termined \t ");
Serial.print("SensorValue ");
Serial.print (sensorValue, DEC);
Serial.print("\t outputvalue ");
Serial.println (outputValue, DEC);
delay (300);
}
Moderator: Edited to put code in box. AWOL