Homemade Piano keybord project

Hello everybody.

I am working on a homemade piano keybord using ArduinoUno to control GarageBand on a MacBook.

-The piano keybord is on bread board and it is connected in parallel to ArduinoUno's digital inputs. It works fine.
-ArduinoUno is connected to the MacBook through a USB cable to control GarageBand.

-Now, the problem is the program that I have linked to you in this post.
-The program is working fine on PIN_2/NOTE_C3!!
-Making the point, I do not know how to develop it, in order to play the other notes, from C3 to C4 then.
I would like to use a switch statement with case1, case 2 etc....

Can anybody give some hints? I would appreciate it.
Thanks in advance!

PlaySimpDig_7_MIDI.ino (1.31 KB)

Hint: now might be a good time to learn about arrays.

/* ----   Midi PianoKeybord*/


#include "pitches.h"  // Header file with pitch definitions
#include <MIDI.h>

const int buzz = 10; // initialize buzz
int PIN_2 = 2; // initialize pin2
int PIN_3 = 3; //       "    pin3
int PIN_4 = 4; //       "    pin4
int PIN_5 = 5; //       "    pin5
int PIN_6 = 6; //       "    pin6
int PIN_7 = 7; //       "    pin7
int PIN_8 = 8; //       "    pin8
int PIN_9 = 9; //       "    pin9
int NOTE;

int garage = 0;




void setup() 
{
  pinMode(buzz, OUTPUT); // buzz as OUTPUT
  pinMode(PIN_2, INPUT); // PIN_2 as INPUT 
  pinMode(PIN_3, INPUT); // PIN_3 as INPUT 
  pinMode(PIN_4, INPUT); // PIN_4 as INPUT 
  pinMode(PIN_5, INPUT); // PIN_5 as INPUT 
  pinMode(PIN_6, INPUT); // PIN_6 as INPUT 
  pinMode(PIN_7, INPUT); // PIN_7 as INPUT 
  pinMode(PIN_8, INPUT); // PIN_8 as INPUT 
  pinMode(PIN_9, INPUT); // PIN_9 as INPUT 
  


  Serial.begin(115200);
}

void loop()
{   
    while(digitalRead(PIN_2)) //READ pin_2// while as well
    {
    noteOn(0x90, NOTE_C3, 0x45); // Note into channel 1, value of the note, velocity of the note
    }
    noteOn(0x90, NOTE_C3, 0x00); // Note into channel 1, value of the note, velocity of the note
    delay(50);
}



//Play a MIDI note
void noteOn(int cmd, int pitch, int velocity) 
{
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

Ok! Thanks Dude!

I think it's worth noting that the following should function the same as your code. You are continuously checking pin 2 and playing a note when it is pressed. When it goes up you stop the note and delay.

void loop()
{   
  if(digitalRead(PIN_2)) //READ pin_2// while as well
  {
    noteOn(0x90, NOTE_C3, 0x45); // Note into channel 1, value of the note, velocity of the note
  } 
  else {
    noteOn(0x90, NOTE_C3, 0x00); // Note into channel 1, value of the note, velocity of the note
    delay(50);
  }
}

The difference is that it uses the built-in repeating call to loop() instead of hanging out in a tight loop while the key is down. As you add more keys you will want to check them continuously.

If your pins and notes are all in a row you could accomplish what you want by using math (not sure about keeping that delay), adding an offset to the pin and pitch.

void loop()
{
  // check each key
  for( int i = 0; i < 8; i++)
  {  
    if(digitalRead(PIN_2+i)) //READ pin
    {
      noteOn(0x90, NOTE_C3+i, 0x45); // Note into channel 1, value of the note, velocity of the note
    } 
    else {
      noteOn(0x90, NOTE_C3+_i, 0x00); // Note into channel 1, value of the note, velocity of the note
    }

  }
}

Converting this code to use arrays would make it much more flexible because you could map the pins to pitches any way you wanted.

Thanks Dude! I'll let you know if it works !