Attiny85 Blink based on voltage not working

Hi everyone, I'm attempting to make an LED begin blinking when the voltage from a LiPo hits a certain amount using an Attiny85. I've set it up through the following code - rather than having a hard and fast 'at this voltage we start blinking' rule, since over many monitoring's of the battery discharging it was clear that the only way to have a semi reliable metric would be for consecutive readings to be under 3.2V.

#include <avr/io.h>

int voltageCounter;
int analogData;
bool on;
int analogIn = A3;
int digitalOut = PB1;

void setup() {
pinMode(analogIn, INPUT);
pinMode(digitalOut, OUTPUT);
voltageCounter = 0;
on = false;
digitalWrite(digitalOut, HIGH);
}

void loop() {
analogData = analogRead(analogIn); //store data in analogData variable
if (analogData < 3.2){
if(voltageCounter = 20){
if(on){
digitalWrite(digitalOut, LOW);
on = false;
}
else{
digitalWrite(digitalOut, HIGH);
on = true;
}
}
else{
voltageCounter++;
}
}
else{
voltageCounter = 0;
}
delay(1000); //delay 1 second
}

Connected - (VCC to 5V, LiPo output to A3, GND to GND, PB1 to positive LED leg, and negative LED leg to GND).

The LED comes on, and I've tested all the connections, but the LED does not blink at any point - I'm guessing my issue is most likely with my access of the ATtiny85s pins in the code, but I'm not sure how else to go about it.

This sets voltageCounter to 20.
Use == for a comparison to 20.

1 Like

Okay, I'm an idiot - I'm new to hardware tinkering serves me right spending time soldering it in without double checking my code.

Does the rest of the program look to be okay? I only have one extra chip and I felt like I was playing a bit of guesswork when I referenced the pins, I believe I used two different sites.

Also, I thought that when if statements are accidentally done that way, that they always evaluate true after performing the operation - if that's the case then shouldn't it have been blinking the entire time?

Looks to me that current code has voltage counter = 20 all the time so on = false all the time and digitalOut is Low all the time.
Low on both sides of the LED would keep it off.
Do you have a current limit resistor with the LED to keep from damaging the LED or the IO pin? Something in the 270 to 1K range would do.

How did you add the Attiny85 to the IDE to have A3 and PB1 be recognized?

Dont forget to scale analog input
10bit .....0-1023 = 0-5V

LFRED

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.