URGENT: Sparkfun Musical Instrument Shield

Hi everyone,

I am creating a project using the musical instrument shield along with flex sensors, and the project idea is to design a glove that plays certain notes depending on the degree the finger is bent, imitating hitting notes on a piano/keyboard.

I have been trying to make each flex sensor correspond to a specific note, but the sparkfun musical instrument shield doesn't seem to be responding even though all of the flex sensors are reading values. Is anyone familiar with the program and would be able to help? That would be highly appreciated!

Below is the code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);  // RX, TX

byte note = 0;       //The MIDI note value to be played
byte resetMIDI = 4;  //Tied to VS1053 Reset line
byte ledPin = 13;    //MIDI traffic inidicator
int instrument = 12;

int flexpin1 = A12;
int flexpin2 = A11, flexpin3 = A12, flexpin4 = A13, flexpin5 = A14;

void setup() {
  Serial.begin(57600); //Setup soft serial for MIDI control
  mySerial.begin(31250);
  talkMIDI(0xB0, 0, 0x79);  //Bank select Melodic
  talkMIDI(0xC0, 12, 0);    //Set instrument number.
}

void loop() {
  int flexVal1 = analogRead(flexpin1);
  Serial.print("Sensor1: ");
  Serial.println(flexVal1);

  if (digitalRead(flexVal1) < 250) {
    noteOn(0, 27, 60);
    //delay(50);
  } else {
    noteOff(0, 27, 60);
    // delay(50);
  }
}

//Send a MIDI note-on message.  Like pressing a piano key
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) {
  talkMIDI( (0x90 | channel), note, attack_velocity);
}

//Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) {
  talkMIDI( (0x80 | channel), note, release_velocity);
}

//Plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) {
  digitalWrite(ledPin, HIGH);
  mySerial.write(cmd);
  mySerial.write(data1);
}

/* int flexVal2;
flexVal2 = analogRead(flexpin2);
Serial.print("Sensor2: ");
Serial.println(flexVal2);


int flexVal3;
flexVal3 = analogRead(flexpin3);
Serial.print("Sensor3: ");
Serial.println(flexVal3);


int flexVal4;
flexVal4 = analogRead(flexpin4);
Serial.print("Sensor4: ");
Serial.println(flexVal4);


int flexVal5;
flexVal5 = analogRead(flexpin5);
Serial.print("Sensor5: ");
Serial.println(flexVal5);
*/

you only read flexpin1 in the loop. the other sensors are never used

side notes:

  • you seem to be on a MEGA, don't use softwareSerial use Serial2 for example
  • your talkMIDI() function does not do anything with data2... seems weird.
1 Like

  int flexVal1 = analogRead(flexpin1);
  Serial.print("Sensor1: ");
  Serial.println(flexVal1);

  if (digitalRead(flexVal1) < 250) {
    noteOn(0, 27, 60);

The digital read line makes no sense , you have flexVal1 as an analog value ( 0-1024) , you can’t then digital read it as if it were an input . Furthermore a digital read hase a value of 0 or 1 so β€œ<250 β€œ is meaningless .
Did you mean:


  int flexVal1 = analogRead(flexpin1);
  Serial.print("Sensor1: ");
  Serial.println(flexVal1);

  if (flexVal1< 250)
 {
    noteOn(0, 27, 60);
 }

1 Like

Thank you so much for pointing out the problem! The program is now running :pray:

Thank you so much for your response! I eventually realized the problem with the digitalRead and replaced it with the flexVal1. Now the program is functioning properly!

As I said

Seems one of your parameter is ignored

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.