I've spent most of the day trying to work this out and can't find exactly the information i'm after and i can't figure it out myself so hoping someone can give me a hand.
I'm using two ATtiny85s currently which are part of a larger circuit. tiny #1 is a strange noise maker, tiny #2 is controlling a relay which switches in/out tiny #1 into a larger guitar effect. It's all working well but i started thinking that it would be cool if the footswitch connected to tiny #2 which bypasses/unbypasses could also change the 'mode' of tiny #1 which is making the noise. So that the footswitch would bypass/unbypass tiny #2 with a short press (as it does currently), but select between two modes in tiny #1 when there's a long press.
So i need to change the code for tiny #2 so that it toggles on button release rather than press, and it also needs to ignore long presses so that it won't bypass tiny #1 when you're trying to change the mode.
Then i need to have tiny #1 read the same footswitch as tiny #2 and change between two different pieces of code (noise makers) only when it detects a long press. So i guess tiny #1 only deals with short presses and tiny #2 only deals with long presses.
This is an attempt at the code for tiny #2 largely based on an arduinogetstarted tutorial.
#include <ezButton.h>
int buttonPin = 3;
int relayPin = 0;
const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
ezButton button(buttonPin); // create ezButton object that attach to buttonPin;
int relayState = LOW;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
void setup() {
button.setDebounceTime(50); // set debounce time to 50 milliseconds
pinMode (buttonPin, INPUT_PULLUP);
pinMode (relayPin, OUTPUT);
digitalWrite (relayPin, relayState);
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
pressedTime = millis();
isPressing = true;
isLongDetected = false;
}
if (button.isReleased()) {
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if ( pressDuration < SHORT_PRESS_TIME ) {
relayState = !relayState;
digitalWrite (relayPin, relayState);
}
if (isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if ( pressDuration > LONG_PRESS_TIME ) {
isLongDetected = true;
}
}
}
I'm not sure that's the best way to go about it but that's where i'm at.
I'm really stumped on how to use long presses only to toggle between modes on tiny #1. I thought about using the same approach here but i can't think how to select between two different bits of code.
Sorry for the info-dump! Any help would be greatly appreciated
Patrick