Hi Guys, Thought I'd Share this bit of code for probably the smallest microcontroller relay switching you could use.
I've also got it available to view and test with an online emulator here --> https://wokwi.com/projects/361050179761795073
Not bad for 50 lines of code :)
Thanks all
byte inPins[] = {0, 1, 2};
byte outPins[] = {3, 4, 5};
enum { ON = HIGH, OFF = LOW };
#define N_BUT sizeof(inPins)
byte ToggleState [N_BUT];
int chkButtons() {
for (unsigned n = 0; n < sizeof(inPins); n++) {
byte BUT = digitalRead(inPins[n]);
if (ToggleState[n] != BUT) {
ToggleState[n] = BUT;
if (OFF == BUT){
return n;
}
}
while (digitalRead(inPins[n]) == LOW) {delay(10);}
}
return -1;
}
void setIO(){
for (unsigned c = 0; c< sizeof(inPins); c++) {
pinMode (inPins[c], INPUT_PULLUP);
ToggleState[c] = digitalRead(inPins[c]);
}
for (unsigned d = 0; d< sizeof(outPins); d++) {
pinMode (outPins[d], OUTPUT);
}
}
void setFunctions(){
int n = chkButtons();
if (0 <= n) {
digitalWrite(outPins[n], !digitalRead(outPins[n]));
}
delay(10);
}
void setOFF(){
digitalWrite(outPins[0], HIGH);
digitalWrite(outPins[1], HIGH);
digitalWrite(outPins[2], HIGH);
}
void setup() {
setIO();
setOFF();
}
void loop() {
setFunctions();
}
Just curious... Why didn't you do traditional debouncing? Is this a scheme you came up with and what were you solving for? Or did you see this elsewhere and use it?
What's "traditional" debouncing? I've seen it done so many ways, in both hardware and firmware.
Quote from: mark2 on April 19, 2023, 10:59:02 AM
Just curious... Why didn't you do traditional debouncing? Is this a scheme you came up with and what were you solving for? Or did you see this elsewhere and use it?
not sure what you mean by traditional debouncing, but yes this is my code I wrote in notepad++
also this is using arrays to map the switching to relays.
I have another that I've been working on that is a 3 channel with 5 toggles, but I want to add each CC message as an array, along with midi store function that I haven't implemented yet
https://wokwi.com/projects/354353450063283201