I have created the following rather simple code to toggle an LED driver.
The serial commands are commented out on the ATTiny85 portion as there is not a serial output.
I can trigger all three options with serial feedback that is correct on the Micro, but on the Tiny85 I only get the flash routine, the long press never seems to trigger in order to latch the LED enable line on or off. This DOES work on the micro via the serial feedback.
static const int buttonPin = 3; // switch pin
static const int LED_Pin = 4; // LED output Enable Pin
int buttonStatePrevious = LOW; // previousstate of the switch
unsigned long minButtonLongPressDuration = 1000; // Time we wait before we see the press as a long press
unsigned long buttonLongPressMillis; // Time in ms when we the button was pressed
bool buttonStateLongPress = false; // True if it is a long press
bool LEDState = false;
const int intervalButton = 50; // Time between two readings of the button state
unsigned long previousButtonMillis; // Timestamp of the latest reading
unsigned long buttonPressDuration; // Time the button is pressed in ms
//// GENERAL ////
unsigned long currentMillis; // Variabele to store the number of milleseconds since the Arduino has started
void setup() {
//Serial.begin(9600); // Initialise the serial monitor
pinMode(buttonPin, INPUT); // set buttonPin as input
pinMode (LED_Pin, OUTPUT); //set LED pin as output
// Serial.println("Press button");
}
// Function for reading the button state
void readButtonState() {
// If the difference in time between the previous reading is larger than intervalButton
if(currentMillis - previousButtonMillis > intervalButton) {
// Read the digital value of the button (LOW/HIGH)
int buttonState = digitalRead(buttonPin);
// If the button has been pushed AND
// If the button wasn't pressed before AND
// IF there was not already a measurement running to determine how long the button has been pressed
if (buttonState == LOW && buttonStatePrevious == HIGH && !buttonStateLongPress) {
buttonLongPressMillis = currentMillis;
buttonStatePrevious = LOW;
// Serial.println("Button pressed");
}
// Calculate how long the button has been pressed
buttonPressDuration = currentMillis - buttonLongPressMillis;
// If the button is pressed AND
// If there is no measurement running to determine how long the button is pressed AND
// If the time the button has been pressed is larger or equal to the time needed for a long press
if (buttonState == LOW && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
buttonStateLongPress = true;
// Serial.println("Button long pressed");
//led on code
// Serial.println(LEDState);
if (LEDState == HIGH){
// Serial.println("LED ON");
digitalWrite(LED_Pin, HIGH);
} else {
// Serial.println("LED OFF");
digitalWrite(LED_Pin, LOW);
}
LEDState = !LEDState;
// Serial.println(LEDState);
}
// If the button is released AND
// If the button was pressed before
if (buttonState == HIGH && buttonStatePrevious == LOW) {
buttonStatePrevious = HIGH;
buttonStateLongPress = false;
digitalWrite (LED_Pin, HIGH);
delay(100);
digitalWrite (LED_Pin, LOW);
// Serial.println("flash");
}
// store the current timestamp in previousButtonMillis
previousButtonMillis = currentMillis;
}
}
void loop() {
currentMillis = millis(); // store the current time
readButtonState(); // read the button state
}
I have the following options selected for the ATTiny85 in my IDE via the ATTiny Core board profile,

My IDE tools for the ATTiny85 are as follows
ATTiny Core
ATTiny85 chip selected
Clock Source 8MHz internal
Timer 1 Clock: CPU Frequency
LTO Disabled
Millis() Enabled
Save EEPROM: EEPROM retained
BOD; Disabled
Programmer: USBTinyISP
I don't see that behaviour when the sketch is programmed into an Uno or into an ATtiny85. I see a flash when the button is released, and (occasionally) the LED staying on for the duration of a long press. But the latter is not consistent on either.
The intent of the sketch is a multi function response from a single button with external pullup. A brief press should result in a 50ms activation of the led enable.
A long press of the button should toggle the led enable on or off until anoter long press is encountered
If the serial lines are uncommented, it responds properly on my micro. But it doesn't work on the tiny, only the flash bit. That's what I'm asking. I know it doesn't work, why does it not work m
One button isn't going to be able to tell the difference between a long press and a momentary press, so it will trip the briefly (50ms) state while waiting for the long press.
I am saying that I don't see it working anywhere. Not on an Uno, not on an ATtiny85. And it makes no difference on the Uno whether the Serial lines are commented or not. It doesn't work.
I can handle that, it's for a flashlight. I need both a flash and latch on/off..
As I said, by the serial feedback responses, I am getting to the appropriate locations in the code. Since the tiny doesn't have serial, I comment those serial print lines, and only get the flash.
Try my second code.... and behavior... it should teach you a lot, if you are learning to code... You declare some stuff as const when it's already global and assign a 0 value (LOW is also 0) when again it's a global. This shows some broken fluency with the language. Also, you declare your 50 ms constant as an int, but likely use it as a unsigned long. I don't know what "works/doesn't" looks like in either case, so you will have to explain exactly the behaviour of the non-working code. "working" is only relevant to explain how the functionality breaks from one chip to the next. If it doesn't compile, for example, that's not the same as not working (or it is, depending on your definition).
Ok, I think I figured this out, the flash routine is running after the latch part. so I need to jump out of the function at the end of the latch so the flash doesn't run