Hi guys, I have been trying for days now to solve a little problem I have with the code of my DIY guitar pedals's switching system. It uses an ATtiny85 running at 16Mhz internal, and the one wire bootloader that actually just lets me program the chip using two wires. TX/RX on pin 2 of the chip, and ground. easy to program these little ones like this
Anyway, this is how the switching works:
Short press changes the state of a relay and mute pin combination, so, it turns the effect ON or OFF. Latching switch.
Long press, turns the effect ON on press or keeps the ON if it was already ON, until I release the footswitch, and if it was held for over 1 second. Momentary switching.
Middle press(between half a second and 1 second), puts the pedal in remote mode. It will then be able to receive commands from am external footswitch connected to arduino pin 3 of the ATtiny.
There is also code for this pin to switch the effect while the pedal is not in "Remote Mode", and this is intended. It doesn't do the latching switching. Just ON if the footswitch is ON and OFF if it is OFF. No latching.
All this is working as intended, even if the code is a mess
The problem is as you can see on the video attached below, when I press the switch sometimes the relay (Yellow LED) behaves like it wasn't debounced, and it is. I suppose 20ms should be enough?
And also the pedal enters "Remote Mode" (Red LED) when switching the switch several times and slowly. I think it might be detecting 2 consecutive presses as middle press? I can't understand why
Anyway, thanks a lot for your help. I am still a beginner in arduino coding, and at the moment I put together pieces of code that I find online. We gotta start somewhere, right?
Here is the video and code. Thanks again
João
#define PRESSED LOW
#define NOT_PRESSED HIGH
const byte relay = 0;
const byte mute = 1;
const byte remote = 2;
unsigned long shortPress = 15;
unsigned long longPress = 1000;
unsigned long midpress = 500;
bool relay_State = false;
bool remote_State = false;
//bool ledState = false;
typedef struct Buttons {
const byte pin = 4;
const int debounce = 20;
unsigned long counter=0;
bool prevState = NOT_PRESSED;
bool currentState;
} Button;
typedef struct Buttons1 {
const byte pin = 3;
const int debounce = 20;
unsigned long counter=0;
bool prevState = NOT_PRESSED;
bool currentState;
} Button1;
// create a Button variable type
Button button;
Button1 button1;
void setup() {
pinMode(relay, OUTPUT);
pinMode(mute, OUTPUT);
pinMode(remote, OUTPUT);
pinMode(button.pin, INPUT_PULLUP);
pinMode(button1.pin, INPUT_PULLUP);
digitalWrite(relay, LOW);
digitalWrite(mute, LOW);
digitalWrite(remote, LOW);
}
void loop() {
if (remote_State == true) {
RemoteControl();
}
// check the button
button.currentState = digitalRead(button.pin);
// has it changed?
if (button.currentState != button.prevState) {
delay(button.debounce);
// update status in case of bounce
button.currentState = digitalRead(button.pin);
if (button.currentState == PRESSED) {
// a new press event occured
// record when button went down
button.counter = millis();
digitalWrite(mute, HIGH);
delay(35);
digitalWrite(relay, HIGH);
delay(35);
digitalWrite(mute, LOW);
}
if (button.currentState == NOT_PRESSED) {
// but no longer pressed, how long was it down?
unsigned long currentMillis = millis();
if ((currentMillis - button.counter >= shortPress) && (currentMillis - button.counter < midpress)) {
// Short press detected.
digitalWrite(mute, HIGH); //Turn Opto ON
delay(30);
relay_State = !relay_State;
digitalWrite(relay, relay_State);
delay(30);
digitalWrite(mute,LOW);
currentMillis == 0;
}
if ((currentMillis - button.counter >= longPress)) {
// the long press was detected
digitalWrite(mute, HIGH); //Turn Opto ON
delay(30);
digitalWrite(relay, LOW);
delay(30);
digitalWrite(mute, LOW);
relay_State = false;
currentMillis == 0;
}
if ((currentMillis - button.counter >= midpress) && (currentMillis - button.counter < longPress)) { // Mid press detected.
remote_State = !remote_State;
digitalWrite(remote, remote_State);
digitalWrite(mute, HIGH); //Turn Opto ON
delay(30);
digitalWrite(relay, LOW);
delay(30);
digitalWrite(mute, LOW);
relay_State = false;
currentMillis == 0;
}
}
// used to detect when state changes
button.prevState = button.currentState;
}
// check the button
button1.currentState = digitalRead(button1.pin);
// has it changed?
// if (button1.currentState != button1.prevState) {
if ((button1.currentState != button1.prevState) && (remote_State == false)) {
delay(button1.debounce);
// update status in case of bounce
button1.currentState = digitalRead(button1.pin);
if (button1.currentState == PRESSED) {
if (relay_State == false) {
digitalWrite(mute, HIGH);
delay(35);
}
digitalWrite(relay, HIGH);
delay(35);
digitalWrite(mute, LOW);
relay_State = true;
}
if (button1.currentState == NOT_PRESSED) {
// but no longer pressed, how long was it down?
if (relay_State == true) {
digitalWrite(mute, HIGH); //Turn Opto ON
delay(35);
relay_State = false;
digitalWrite(relay, relay_State);
delay(35);
digitalWrite(mute,LOW);
}
}
// used to detect when state changes
button1.prevState = button1.currentState;
}
}
void RemoteControl() {
// check the button
button1.currentState = digitalRead(button1.pin);
// has it changed?
if (button1.currentState != button1.prevState) {
delay(button1.debounce);
// update status in case of bounce
button1.currentState = digitalRead(button1.pin);
if (button1.currentState == PRESSED) {
// a new press event occured
// record when button went down
button1.counter = millis();
digitalWrite(mute, HIGH);
delay(35);
digitalWrite(relay, HIGH);
delay(35);
digitalWrite(mute, LOW);
}
if (button1.currentState == NOT_PRESSED) {
// but no longer pressed, how long was it down?
unsigned long currentMillis = millis();
if (currentMillis - button1.counter <= longPress) {
// short press detected.
digitalWrite(mute, HIGH); //Turn Opto ON
delay(30);
relay_State = !relay_State;
digitalWrite(relay, relay_State);
delay(30);
digitalWrite(mute,LOW);
}
if ((currentMillis - button1.counter > longPress)) {
// the long press was detected
digitalWrite(mute, HIGH); //Turn Opto ON
delay(30);
digitalWrite(relay, LOW);
relay_State = false;
delay(30);
digitalWrite(mute, LOW);
}
}
// used to detect when state changes
button1.prevState = button1.currentState;
}
}