I have a simple first project that tests the voltage drop over a resistor in order to test the capacity of Lithium cells and I've installed a Reed relay to control the circuit. I'm sure I'm missing something, but I've been searching for hours on how to write for the shut off. I thought an if statement would work, but it's not. Any help or pointers for a newbie would be appreciated. I can't seem to find the proper approach.
Below is my code:
// My Power Cell Tester
// tests voltage drop across resistor, calculates current and records capacity
const float resistor = 2.0;
int LED = 13;
int Relay = 8;
float capacity=0, value1, value2, voltage1, voltage2, current, time=0;
void measure (void) {
value1= analogRead(0);
value2= analogRead(3);
voltage1=abs(value1/1024*5.0);
voltage2=abs(value2/1024*5.0);
current = abs((voltage1-voltage2)*1000/resistor);
capacity=abs(current*time/3600);
time++;
}
boolean x=false;
ISR(TIMER1_OVF_vect) {
TCNT1=0x0BDC;
x=!x;
measure();
}
void setup() {
pinMode(LED, OUTPUT);
pinMode(Relay, OUTPUT);
TIMSK1=0x01; // enabled global and timer overflow interrupt;
TCCR1A = 0x00; // normal operation page 148 (mode0);
TCNT1=0x0BDC; // set initial value to remove time error (16bit counter register)
TCCR1B = 0x04; // start timer/ set clock
Serial.begin(9600);
};
void loop ()
{
digitalWrite(LED, HIGH);
digitalWrite(Relay, HIGH);
Serial.print("Voltage1= ");
Serial.print(voltage1);
Serial.print(" V. ");
Serial.print("Voltage2= ");
Serial.print(voltage2);
Serial.print(" V. ");
Serial.print("Voltage drop= ");
Serial.print(voltage1-voltage2);
Serial.print(" V. ");
Serial.print("Current= ");
Serial.print(current);
Serial.print(" mA ");
Serial.print("Capacity= ");
Serial.print(capacity);
Serial.print(" mAh ");
Serial.print("Discharge time= ");
Serial.print(time);
Serial.print(" sec. ");
Serial.print("\n");
delay(1000);
{
if (voltage1 < 3.68);
digitalWrite (Relay, LOW);
}
};
The cut off value listed was arbitrary. I intend on using 3.0 V for the Lithium cells when I get this right.
Also, my current calculations are better since using abs numbers instead of floating, they were all over the place, but they still varies quite a bit. Is there an error there?