I don't see any transistion to other states though, it always acts as if the button is not pushed
You should be able to attach a current limiting resistor and led between The Arduino pin 4 and ground on the pro micro if there is not an LED built in. My point is that you should have the same setup on both circuits as console does not equal pin toggling.
I exclusive or equals the on off variable with one. This is a shortcut to toggle.
I have a flash on my leds no matter what point in the code it is at with my snippet.
I never get the latched condition. I did notice in the serial it is also running the flash AFTER the latch was recognized. I think that is my problem, I need to somehow escape the whole function after that latch routine is recognised so that the flash part doesn't shut it off later in the code
You do not need a latch. The output is already decoupled from the input. You need to read the input, determine if the input has been low long enough, and then toggle your flag. Your flag since it only has two states can be zero and non-zero. This can easily be achieved by ^= 1.
I see the error in my own code. I will fix it and repost and explain.
I do need the LED enable to latch on or off based on the long duration. I do have this being recognized. The flash that runs after is what shuts everything off. so I think a simple return at the end of my long press check should jump back out to the loop and avoid the flash which is an on, delay, off
I will explain first and then I will fix it. The problem with my code is that I have a timer that is constantly looking to turn the LED off after 50 milliseconds from when the button is pressed initially. This means that up to 70 days after it's still trying to turn it off. I need to have the two inputs set separate flags.
ok, the functionality is such:
brief button press, 50ms on/off to led
long press, LED on or off indefinately until next long press event to toggle state.
and return didn't break off properly, now the flash is repeatedly triggered.
I have made corrections as noted below.
EDIT: See correction below.
this is driving me crazy. I always get the brief 50ms flash but can never get the steady on/off. I'll have to figure something else out.
I even tried this using the EasyButton library,
it gives the appropriate serial messages at the various points as it should given button holds, switches between the on and off. but the digital write doesn't seem to execute for anything other than the short press on, delay 50 and off part. it doesn't set high based on the condition for ON state being shown on L25-27. I do get the serial message it should be on from L26
`/*
Name: PressedForDuration.ino
Created: 9/5/2018 10:49:52 AM
Author: Evert Arias
Description: Example to demostrate how to use the library to detect a pressed for a given duration on a button.
*/
#include <EasyButton.h>
// Arduino pin where the button is connected to.
#define BUTTON_PIN 3
#define ledPin 4
//#define BAUDRATE 115200
bool ledState = false;
// Instance of the button.
EasyButton button(BUTTON_PIN);
// Callback function to be called when the button is pressed.
void onPressedForDuration()
{
// Serial.println("Button pressed 1");
ledState = !ledState;
if (ledState == 1){
// Serial.println("LED ON");
digitalWrite(ledPin, HIGH);
}
else{
// Serial.println("LED OFF");
digitalWrite(ledPin, LOW);
}
}
void onPressedForDuration2()
{
// Serial.println("Button pressed 2");
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
}
void setup()
{
// Initialize Serial for debuging purposes.
// Serial.begin(BAUDRATE);
pinMode(BUTTON_PIN, INPUT);
pinMode(ledPin, OUTPUT);
// Serial.println();
// Serial.println(">>> EasyButton pressedFor example <<<");
digitalWrite(ledPin, LOW);
// Initialize the button.
button.begin();
// Add the callback function to be called when the button is pressed for at least the given time.
button.onPressedFor(1000, onPressedForDuration);
button.onPressed(onPressedForDuration2);
}
void loop()
{
// Continuously read the status of the button.
button.read();
}`
Again, serial testing is on a Micro, only for debugging. Target ATTiny85 is where things fall apart. and the Micro is driving a regular LED on an output properly.
So the Micro test it follows the serial output, when LED should be on, it is. When it should be off, it goes off.
When I comment out the serial and change pins for the hard wired PCB with the ATTiny85, it doesn't do anything for the long press.
the short press is reconized and does flash the LED array, although it seems to be quite a bit longer duration than what I see with the Micro led probably on order of 10-15% longer. Same for the duration required to get it to trigger a response.
so it has got to be something with the ATTiny85 configuration/timers but what I have no idea
So getting closer, instead of 1 second hold for the 1000ms duration, I need to hold the button nearly 10 seconds! so yes, a timer issue
Ok got it now, set to 1MHz internal osc with timer1 set to 64MHz and timer is behaving. QUick press gets a fairly close 50ms (56ms actual) flash. Button held for about 1sec sets the array on, holding another second turns it off.
I had to add a flag for when the pushbutton is first pressed to that it doesn't cycle between on and off states 50000 times as you hold down the button past 1 second. I have tested this on Wokwi. <- is the link to my project.
/* Pushbutton Example with 50ms pulse on with soft on/off (long-press)
by @Perehama
*/
class IntervalTimer {
public:
IntervalTimer(unsigned long i);
void synchronizeTimer();
bool intervalComplete();
void setInterval(unsigned long i);
unsigned long elapsedTime();
private:
unsigned long _interval;
unsigned long _timestamp;
};
class PushButton {
public:
PushButton(int pinNumber, unsigned long debounceTime);
void inputPullup() const;
bool isPushed();
unsigned long durationHeld();
void setDebounce(unsigned long i);
private:
IntervalTimer _timer;
int _preState = HIGH;
unsigned char _debounceFlag = 0;
const int _ButtonPin;
const static int _RisingEdge = HIGH - LOW;
const static int _FallingEdge = LOW - HIGH;
};
IntervalTimer::IntervalTimer(const unsigned long i) : _interval(i) {}
void IntervalTimer::synchronizeTimer() {
_timestamp = millis();
}
bool IntervalTimer::intervalComplete() {
if (millis() - _timestamp >= _interval) {
_timestamp += _interval;
return true;
}
return false;
}
void IntervalTimer::setInterval(unsigned long i) {
_interval = i;
}
unsigned long IntervalTimer::elapsedTime() {
unsigned long x = millis() - _timestamp;
return x;
}
PushButton::PushButton(int pinNumber, unsigned long debounceTime) : _timer(debounceTime), _ButtonPin(pinNumber) { }
void PushButton::inputPullup() const {
pinMode(_ButtonPin, INPUT_PULLUP);
}
bool PushButton::isPushed() {
bool b = false;
if (_debounceFlag == 1) {
if (_timer.intervalComplete()) _debounceFlag = 0;
}
else {
int sample = digitalRead(_ButtonPin);
int state = sample - _preState;
if (state == _FallingEdge) {
_timer.synchronizeTimer();
b = true;
_debounceFlag = 1;
}
else if (state == _RisingEdge) {
_timer.synchronizeTimer();
_debounceFlag = 1;
}
_preState = sample;
}
return b;
}
unsigned long PushButton::durationHeld() {
unsigned long u = 0;
if (_preState == LOW) {
u = _timer.elapsedTime();
if (u == 0) u = 1;
}
return u;
}
void PushButton::setDebounce(unsigned long i) {
_timer.setInterval(i);
}
static const int buttonPin = 3, // switch pin
LED_Pin = 4; // LED output Enable Pin
const unsigned long MinButtonLongPressDuration = 1000UL, // Time we wait before we see the press as a long press
intervalButton = 50UL;
unsigned char longOnOffFlag = 0, pulseOnFlag = 0, longPressStartFlag;// added seperate flags for on/off and pulse on
PushButton Button(buttonPin, 50UL);
IntervalTimer OneShotTimer(intervalButton);
void setup() {
Button.inputPullup();
digitalWrite(LED_Pin, HIGH);// take the pin HIGH first to avoid a LED flash at start.
pinMode(LED_Pin, OUTPUT);
}
void loop() {
if (OneShotTimer.intervalComplete()) pulseOnFlag = 0;// only pulse flag is turned off 50 ms later
if (Button.isPushed()) {
OneShotTimer.synchronizeTimer();// timer is synced here so 50 ms later, timer will shut off
longPressStartFlag = pulseOnFlag = 1;// pulse state is turned on here.
}
if (longPressStartFlag == 1 && Button.durationHeld() >= MinButtonLongPressDuration) {
longOnOffFlag ^= 1;// on-off is toggled here
longPressStartFlag = 0;
}
if (longOnOffFlag == 0 && pulseOnFlag == 0) digitalWrite(LED_Pin, HIGH);
else digitalWrite(LED_Pin, LOW);// requires both flags off to turn off LED. Works for both now.
}
I have included a button class in the example, a library of sorts, that takes care of debouncing the button. I hope you study the code as there is a lot of concepts in practice here.
The values you originally started with in your example were maintained. Edit the following lines in my sketch. They are about halfway down the page, around line 98 if you have line numbers on.
static const int buttonPin = 3, // switch pin
LED_Pin = 4; // LED output Enable Pin
const unsigned long MinButtonLongPressDuration = 1000UL, // change this to nearly 10 seconds.
intervalButton = 50UL;// this is the pulse duration.
The fault was in the attiny oscillator and timer settings, causing the chip to run extremely slow, i found the right configuration settings combination to get it running at correct speed then my code worked fine. Thanks for the help though