Hey everyone! So I have been expiriemtning with an attiny 85 to make a simple 1 cell battery voltage monitor. I want it to simply beep and show an LED when the voltage gets below a specified value. The only problem is, whenever I try to run my code, it doesn’t do anything even when the voltage is below the threshold value. I think it might be becuase of the analogRead part. I am supplying the battery voltage to the attiny 85’s vcc pin, and also supplying the battery’s voltage to the analog pin I am reading from, is this ok or could this be causing the problem?
Here is the code
//A1 is battery input pin
int buzzerPin = 0; //pin the buzzer is on
int ledPin = 3; //pin the LED is on
float batteryVoltage; //initialize the variable for battery voltage
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(A1, INPUT);
}
void loop() {
batteryVoltage = (analogRead(A1)) * (5.0 / 1023.0); //read pin the batery is on, then convert to volts
delay(5);
if (batteryVoltage <= 3.7){ //make noise and show LED if the voltage is 3.7 volts or below
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(buzzerPin, LOW); // do not beep or light LED if the voltage is above 3.7
digitalWrite(ledPin, LOW);
}
}