I have a sketch that has 3 LED lit Buttons and 2 IR Transmitter/Receivers all set to trigger MIDI notes upon interaction. For some reason unknown to me, both GND indicator LED's on each of the IR Receivers keep flashing at random in unison with one another and sending unwanted MIDI code on their own, without interaction.
Does anyone know why this might be, and how to fix the issue?
Thanks in advance.
#include <MIDI.h>
#include <IRremote.h>
#define ATMEGA328 1
#ifdef ATMEGA328
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
#endif
// BUTTON LIGHTS & IR
const int LED1 = 9; // BUTTON 1 INTERNAL LIGHT
const int LED2 = 10; // BUTTON 2 INTERNAL LIGHT
const int LED3 = 11; // BUTTON 3 INTERNAL LIGHT
const int LED4 = 5; // IR TRANSMITTER 1
const int LED5 = 7; // IR TRANSMITTER 2
const int LED6 = 6; // IR RECEIVER 1
const int LED7 = 8; // IR RECEIVER 2
// PHYSICAL BUTTONS
const int N_BUTTONS = 3; //* total number of buttons
const int BUTTON_ARDUINO_PIN[N_BUTTONS] = { 2, 3, 4 }; //* pins of each button connected straight to the Arduino
int buttonCState[N_BUTTONS] = {}; // stores the button current value
int buttonPState[N_BUTTONS] = {}; // stores the button previous value
unsigned long lastDebounceTime[N_BUTTONS] = { 0 }; // the last time the output pin was toggled
unsigned long debounceDelay = 50; //* the debounce time; increase if the output flickers
// MIDI
byte midiCh = 1; //* MIDI channel to be used
const byte noteIR1 = 24; // MIDI note for IR RECEIVER 1 (C4)
const byte noteIR2 = 28; // MIDI note for IR RECEIVER 2 (D4)
const byte noteButtons[N_BUTTONS] = { 31, 33, 35 }; // MIDI notes for physical buttons (E4, F4, G4)
IRrecv irrecv1(6); // Pin 6 is connected to IR RECEIVER 1
IRrecv irrecv2(8); // Pin 8 is connected to IR RECEIVER 2
decode_results results1;
decode_results results2;
int IRState1 = HIGH; // HIGH when no object detected by IR RECEIVER 1, LOW when object detected
int IRState2 = HIGH; // HIGH when no object detected by IR RECEIVER 2, LOW when object detected
unsigned long lastIRStartTime1 = 0; // Time when IR note started (RECEIVER 1)
unsigned long lastIRStartTime2 = 0; // Time when IR note started (RECEIVER 2)
const unsigned long IRDuration = 3000; // Duration of the IR note in milliseconds (3 seconds)
void setup() {
//PIN MODES FOR LEDS & IR
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, INPUT_PULLUP); // IR RECEIVER 1 input with pull-up resistor
pinMode(LED7, INPUT_PULLUP); // IR RECEIVER 2 input with pull-up resistor
//LED STATE ON/OFF
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
// Baud Rate
Serial.begin(115200); //*
#ifdef DEBUG
Serial.println("Debug mode");
Serial.println();
#endif
// Buttons
// Initialize buttons with pull-up resistors
for (int i = 0; i < N_BUTTONS; i++) {
pinMode(BUTTON_ARDUINO_PIN[i], INPUT_PULLUP);
}
// IR Receiver setup
irrecv1.enableIRIn(); // Start IR RECEIVER 1
irrecv2.enableIRIn(); // Start IR RECEIVER 2
}
void loop() {
// Read IR input state for RECEIVER 1
int currentIRState1 = digitalRead(6); // Assuming IR RECEIVER 1 is connected to Pin 6
// Object detected by RECEIVER 1
if (currentIRState1 == LOW && IRState1 == HIGH) {
// Object detected, send MIDI note on for IR RECEIVER 1
MIDI.sendNoteOn(noteIR1, 127, midiCh);
lastIRStartTime1 = millis();
IRState1 = LOW;
}
// Check if the IR note duration has passed (RECEIVER 1)
if (IRState1 == LOW && millis() - lastIRStartTime1 >= IRDuration) {
// IR note duration elapsed, send MIDI note off
MIDI.sendNoteOff(noteIR1, 0, midiCh);
IRState1 = HIGH;
}
// Read IR input state for RECEIVER 2
int currentIRState2 = digitalRead(8); // Assuming IR RECEIVER 2 is connected to Pin 8
// Object detected by RECEIVER 2
if (currentIRState2 == LOW && IRState2 == HIGH) {
// Object detected, send MIDI note on for IR RECEIVER 2
MIDI.sendNoteOn(noteIR2, 127, midiCh);
lastIRStartTime2 = millis();
IRState2 = LOW;
}
// Check if the IR note duration has passed (RECEIVER 2)
if (IRState2 == LOW && millis() - lastIRStartTime2 >= IRDuration) {
// IR note duration elapsed, send MIDI note off
MIDI.sendNoteOff(noteIR2, 0, midiCh);
IRState2 = HIGH;
}
// Handle physical buttons
for (int i = 0; i < N_BUTTONS; i++) {
buttonCState[i] = digitalRead(BUTTON_ARDUINO_PIN[i]);
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (buttonPState[i] != buttonCState[i]) {
lastDebounceTime[i] = millis();
if (buttonCState[i] == LOW) {
// Sends the MIDI note ON accordingly to the chosen board for physical buttons
MIDI.sendNoteOn(noteButtons[i], 127, midiCh);
} else {
// Sends the MIDI note OFF accordingly to the chosen board for physical buttons
MIDI.sendNoteOff(noteButtons[i], 0, midiCh);
}
buttonPState[i] = buttonCState[i];
}
}
}
}