(Solved)Works in simulation but not Attiny85?

So I wanted to make a cute led with a button. It works in simulation based on Arduino Uno as I designed.

The led turns on when the button is pushed for 1 second, and turns off when the button is pushed for 1 second again.

But on ATtiny85, the led turns on after 1 second however long I push the switch, and so it turns off when I push the button again, no matter how long.

Programmer: USBTinyISP
Board: ATtiny85
Processor: ATtiny85
Clock: Internal 1 Mhz


int LED1 = 1;
int button = 0;
unsigned long time = millis();

boolean LED1State = false;

long buttonTimer = 0;
long longPressTime = 1000;

boolean buttonActive = false;
boolean longPressActive = false;

void setup() {

pinMode(LED1, OUTPUT);
pinMode(button, INPUT);

}

void loop() {

if (digitalRead(button) == HIGH) {

if (buttonActive == false) {
buttonActive = true;
buttonTimer = millis();
}
if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
longPressActive = true;
LED1State = !LED1State;
digitalWrite(LED1, LED1State);
}
} else {
if (buttonActive == true) {
if (longPressActive == true) {
longPressActive = false;
} else {
}
buttonActive = false;
}
}
}
——————-
Excuse me for the messy post, I’m very new and unfamiliar with computer stuff. I will try to learn hard, any kind of help is appreciated. Thank you very much!

Do you have a pull-down resistor on your button?

I don’t know what that means.

I’m sorry, the reply was an error. I don’t have any pulldown resistor on my button;)

This is why we all are so negative about simulators here - they get behavior wrong, even in very basic cases like this.

Without a pulldown (if pressing button connects to vcc) or pullup (if it connects to gnd) resistor on the button pin (10k is a good default value), when button is not pressed, pin is connected to nothing and hence has no defined voltage - it will pick up noise from the environment and read random values.

The preferred way to do a button is to wire it to connect to gnd when pressed, and set pinMode to INPUT_PULLUP - this uses the internal pullup on the chip so no external parts are needed. You of course have to adapt the code so it treats LOW as pressed instead of HIGH, but this is trivial.

Oh my! That really worked! Now it turns on and off when I push the button for 1 second. Thank you so much! That was really helpful and made me so happy:):heart:

Glad to hear you got it sorted out.