I am using digital pins (2,3,4,5) to play midi notes on and off with 3pdt switches with pull down 10k resistor between input pin and ground.
However when I press button 2 randomly at different time interval it sometimes shows pin 3 high in the monitor. (after reading forums here I have realised that it might be due to big switches that I am using where the physical contact plates might be reason for this). I have even changed wires and isolated them from each other to rule out the noise from the wires. I have checked all the connections and soldered the Arduino board pins with best possible skills that I have and tracks are all clear.
Same case occurs between other buttons as but its not always. However after reading about Debounce I have made changes to the code as following, now I got the error only once after that I havent seen it. Can someone have look at the code and advice me if I have assigned Debounce function to multiple switches correctly and if not, what should it be as I am new with coding Since I am using 3pdt switch I have added switch mode..
#include <MIDI.h>
int buttonPin[] = {2, 3, 4, 5};
byte mode[] = {0, 0 , 0 ,0};
boolean currentState = LOW;
boolean lastState = LOW;
int buttonPinM[] = {6, 7, 8, 9};
byte mode1[] = {0, 0, 0, 0};
boolean currentState1 = LOW;
boolean lastState1 = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup (){
MIDI.begin(MIDI_CHANNEL_OMNI);
int i;
for (i = 0; i < 4; i++) {
pinMode(buttonPin[i], INPUT);
}
int j;
for (j = 0; j < 4; j++) {
pinMode(buttonPinM[j], INPUT);
}
{
int i;
for (i = 0; i < 4; i++);
}
{
int j;
for (j = 0; j < 4; j++);
}
}
// This will track which pin/mode we're using
int j = 0;
int i = 0;
void loop () {
{
currentState = digitalRead(buttonPin[i]);
if ( (millis() - lastDebounceTime) > debounceDelay) {
switch (mode[i]) {
case 0:
if (currentState == HIGH) {
MIDI.sendNoteOff (i+55, 0, 1);
mode[i] = 1;
lastDebounceTime = millis();
}
break;
case 1:
if (currentState == HIGH)
mode[i] = 2;
break;
case 2:
if (currentState == LOW) {
MIDI.sendNoteOn (i+55, 127, 1);
mode[i] = 3;
lastDebounceTime = millis();
}
break;
case 3:
if (currentState == LOW)
mode[i] = 0;
break;
}
}
lastState = currentState;
if (++i > 3)
i = 0;
}
{
currentState1 = digitalRead(buttonPinM[j]);
if ( (millis() - lastDebounceTime) > debounceDelay) {
switch (mode1[j]) {
case 0:
if (currentState1 == HIGH ) {
MIDI.sendNoteOn (j+61, 127, 1);
mode1[j] = 1;
lastDebounceTime = millis();
}
break;
case 1:
if (currentState1 == HIGH)
mode1[j] = 2;
break;
case 2:
if (currentState1 == LOW) {
MIDI.sendNoteOff (j+61, 0, 1);
mode1[j] = 3;
lastDebounceTime = millis();
}
break;
case 3:
if (currentState1 == LOW)
mode1[j] = 0;
break;
}
}
lastState1 = currentState1;
if (++j > 3)
j = 0;
}
}
Ctrl+K
to have your browser do this for you.