I am working on a device to monitor the voltage of a battery and do an action when it gets to a certain point, and another action when it gets to another point after that. It detects what type of battery it is (2 cell, or 3 cell) when the battery is plugged in. It is programmed on an ATTiny85 but written in Arduino. The program works very well except for one problem, when the device that the battery is hooked up to is being used the voltage dips and the computer sees that and performs the second action, skipping the first because of a false read in the dip. Is there any way to modify this code to force the computer to have to read the voltage level for a certain number of seconds before it confirms it and continues with the appropriate action? I am reading the voltage through a resistor voltage divider and it works great. I just need the computer to read the voltage for a certain number of seconds before it does any action. Hope you can see what the device does through the code better than I could explain it. Thanks in advance!
int buzzer = 1, voltpin = 2, val = 0, mosfet = 0, cut = 0, buzz = 0;
boolean buzzed = false, cutoff = false;
void setup() {
pinMode(mosfet, OUTPUT);
pinMode(buzzer, OUTPUT);
delay(3000);
val = analogRead(voltpin);
if (val >= 560) {
buzz = 580;
cut = 566;
}
else if ( val >= 364) {
buzz = 387;
cut = 363;
}
}
void loop() {
val = analogRead(voltpin);
if (cutoff == false) {
if (val <= buzz && buzzed == false) {
tone(buzzer, 4500);
delay(250);
noTone(buzzer);
delay(250);
tone(buzzer, 4500);
delay(250);
noTone(buzzer);
delay(250);
tone(buzzer, 4500);
delay(1000);
noTone(buzzer);
buzzed = true;
}
if (val <= cut && cutoff == false) {
digitalWrite(mosfet, LOW);
cutoff = true;
}
if (cutoff == false) {
digitalWrite(mosfet, HIGH);
}
}
delay(1000);
}