Search code examples
loopsif-statementarduinoarduino-unoarduino-ide

Multiple IFs in Arduino


I'm building a launchpad with an Arduino Leonardo. This project consists on buttons connected to the Arduino Leonardo which then sends MIDI (acronym for Musical Instrument Digital Interface) signals to a PC whenever any of these buttons is pushed. The PC, subsequently, plays music sounds whenever it receives MIDI signals from the arduino.

It's critical to be able to push various buttons simultaneously so multiple sounds can be played at the same time. I've managed to almost finish the project. The only thing I haven't accomplished yet is, precisely, coding something that allows the Arduino to detect the button's signals simultaneously.

Here is my code:

#include <frequencyToNote.h>
#include <MIDIUSB.h>
#include <MIDIUSB_Defs.h>
#include <pitchToFrequency.h>
#include <pitchToNote.h>

const int buttonA = 0;
const int buttonB = 1;
const int buttonC = 2;
const int buttonD = 3;
const int buttonE = 4;
const int buttonF = 5;
const int buttonG = 6;
const int buttonH = 7;
const int buttonI = 8;
const int buttonJ = 9;
const int buttonK = 10;
const int buttonL = 11;
const int buttonM = 12;
const int buttonN = 13;
const int buttonO = A0;
const int buttonP = A1;

#define CHANNEL 1  // MIDI channel number

boolean buttonAState;
boolean buttonBState;
boolean buttonCState;
boolean buttonDState;
boolean buttonEState;
boolean buttonFState;
boolean buttonGState;
boolean buttonHState;
boolean buttonIState;
boolean buttonJState;
boolean buttonKState;
boolean buttonLState;
boolean buttonMState;
boolean buttonNState;
boolean buttonOState;
boolean buttonPState;

void setup() {
  pinMode(buttonA, INPUT_PULLUP);
  pinMode(buttonB, INPUT_PULLUP);
  pinMode(buttonC, INPUT_PULLUP);
  pinMode(buttonD, INPUT_PULLUP);
  pinMode(buttonE, INPUT_PULLUP);
  pinMode(buttonF, INPUT_PULLUP);
  pinMode(buttonG, INPUT_PULLUP);
  pinMode(buttonH, INPUT_PULLUP);
  pinMode(buttonI, INPUT_PULLUP);
  pinMode(buttonJ, INPUT_PULLUP);
  pinMode(buttonK, INPUT_PULLUP);
  pinMode(buttonL, INPUT_PULLUP);
  pinMode(buttonM, INPUT_PULLUP);
  pinMode(buttonN, INPUT_PULLUP);
  pinMode(buttonO, INPUT_PULLUP);
  pinMode(buttonP, INPUT_PULLUP);
}

int noterest(){
  delay(70);
}

void loop() {
  buttonAState = digitalRead(buttonA);
  buttonBState = digitalRead(buttonB);
  buttonCState = digitalRead(buttonC);
  buttonDState = digitalRead(buttonD);
  buttonEState = digitalRead(buttonE);
  buttonFState = digitalRead(buttonF);
  buttonGState = digitalRead(buttonG);
  buttonHState = digitalRead(buttonH);
  buttonIState = digitalRead(buttonI);
  buttonJState = digitalRead(buttonJ);
  buttonKState = digitalRead(buttonK);
  buttonLState = digitalRead(buttonL);
  buttonMState = digitalRead(buttonM);
  buttonNState = digitalRead(buttonN);
  buttonOState = digitalRead(buttonO);
  buttonPState = digitalRead(buttonP);

  if (buttonAState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 36, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 36, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonBState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 37, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 37, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonCState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 38, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 38, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonDState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 39, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 39, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonEState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 40, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 40, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonFState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 41, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 41, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonGState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 42, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 42, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonHState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 43, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 43, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonIState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 44, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 44, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonJState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 45, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 45, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonKState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 46, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 46, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonLState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 47, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 47, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonMState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 48, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 48, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonNState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 49, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 49, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonOState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 50, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 50, 100};
    MidiUSB.sendMIDI(noteOff);
  }

  if (buttonPState == HIGH)
  {
   midiEventPacket_t noteOn = {0x09, 0x90 | 1, 51, 100};
   MidiUSB.sendMIDI(noteOn);
   noterest(); 
  } else {
    midiEventPacket_t noteOff = {0x08, 0x80 | 1, 51, 100};
    MidiUSB.sendMIDI(noteOff);
  }

}

As you can see, I'm assuming the Arduino reads the IF statements on the loop linearly. Is this assumption correct? How can I achieve my goal of reading the buttons simultaneously? Is there any other way I can improve my code to make it more efficient?

Thank you in advance!


Solution

  • First use arrays to store your vars:

     uint32_t buttonStates = 0;
     const int button[16] = {0,1,2,3,4,5,...};  // its only the first pins
    

    Initialize with a for loop

     for (uint8_t i=0;i<16;i++) {
       pinMode(buttons[i], INPUT_PULLUP);
     }
    

    catch the presses in binary encoding (buttonStates loop)

     for (uint8_t i=0;i<16;i++) {
       if (digitalRead(buttons[i]) == 1) buttonStates += pow(2,i);
     }
    

    so if you press two buttons you get the combined value, instead of if we use switch case

     switch(buttonStates){
        case 1: //do whatever you do
          break;
     ......
      default: //do what if novalue is generated
         break;
     }
    

    but as your midi packets are the same

       midiEventPacket_t noteOn = {0x09, 0x90 | 1, 37, 100};
    

    you couldintroduce a var tone

     midiEventPacket_t noteOn = {0x09, 0x90 | 1, tone, 100};
    

    so your buttonstate loop can be replaced with

     for (uint8_t i=0;i<16;i++) {
       if (digitalRead(buttons[i]) == HIGH) {
           tone = 36 + i;
         midiEventPacket_t noteOn = {0x09, 0x90 | 1, tone, 100};
         MidiUSB.sendMIDI(noteOn);  
        }
        else {
         tone = 36 + i;
         midiEventPacket_t noteOff = {0x08, 0x80 | 1, tone, 100};
         MidiUSB.sendMIDI(noteOff);
       }
     }
    

    tone is defined in this case uint tone = 0;

    so basiclly we could reduce your program to 40-50 lines of compact code