I am trying to implement the programming code for the Musical Bench that is described here:
I am using an Arduino Uno with the Sparkfun Musical Instrument shield in accordance with the instructions but I am unable to get the SingingSnack.ino sketch to work and I am not strong in programming languages so I cannot resolve the error messages when I try to verify the code.
Here is my program:
// Libraries #include
include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
// needed for MIDI communications with the MI shield
// These are the most tweak-able macros, the ones that really define how it "plays"
#define SIXTEENTH_NOTE_MS 100
// Approx. loop rate in millis, all note durations will be integer multiples of this. Tweak to speed up or slow down the arpeggio.
#define SENSOR_HYSTERESIS 10
// ignore incremental changes smaller than this value... needed to kill noise, but also affects how busy and dense the arpeggio is
#define LOWEST_NOTE 48
// 48 = C3 in MIDI; the piano keyboard ranges from A0 = 21 through C8 = 108, the lowest octaves can be pretty muddy sounding
#define HIGHEST_NOTE 96
// 96 = C7. A smaller range will result in a less busy feel, and may allow more repeated strikes of the same note. You probably won't hear half of this range with human touch, but short the touch pads with metal and you will
// these should need less tweaking, but go ahead and see what they do
#define MY_INSTRUMENT 0
// max 127. GM1 Melodic bank instrument 0 is the Acoustic Grand Piano. You could tweak this, but beware: since we are not using any note off method, if you pick one with long or infinite sustain, you'll get cacaphony in a hurry
#define NOISE_FLOOR 1000
// out of 1023, higher than this is considered open circuit.
#define VELOCITY 100
// note strike "force", max 127
#define VOLUME 110
// max 127, 110 works well for most headphones and fine for most amplifiers as well
// From here on down, tweaking is probably not a great idea...
// pin assignments
#define RX_PIN 2
// Soft serial Rx; M.I shield TxD
#define TX_PIN 3
// shield RxD
#define RESET_PIN 4
// low active shield reset
#define LED_PIN 13
// Arduino native LED
#define SENSOR_PIN A0
// also known as P14
// Standard MIDI, ADDRESS == CH1 which is all we propose to use
#define MIDI_BAUD 31250
#define PROG_CHANGE 0xC0
// program change message
#define CONT_CHANGE 0xB0
// controller change message
#define NOTE_ON 0x90
// sorry about the Hex, but MIDI is all about the nybbles!
#define ALL_OFF 0x7B
// Using "all notes off" rather than "note off" for single voice MIDI is a common tactic; MIDI devices are notorious for orphaning notes
#define BANK_SELECT 0x00
#define CHANNEL_VOL 0x07
// GM1 instrument bank mapping implemented on VS1053b chip
#define MELODIC_BANK 0x79
// there are other options, just not any that are better.
// Global variables and objects SoftwareSerial
MIDIserial(RX_PIN, TX_PIN);
// channel for MIDI comms with the MI shield
unsigned int touchSensor;
// holds the most recent accepted sensor value.
unsigned int sensorBuffer;
// holds a provisional sensor value while we look at it
unsigned int note;
// holds the calculated MIDI note value
void setup()
{
Serial.begin(9600);
// USB is for debugging and tuning info, make sure your serial monitor is set to match this baud rate
// reset pin set-up and hwr reset of the VS1053 on the MI shield. It's probably going to make a fart-y sound if you have an external amplifier!
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, LOW);
delay(100);
digitalWrite(RESET_PIN, HIGH);
delay(1000);
//Start soft serial channel for MIDI, and send one-time only messages
MIDIserial.begin(MIDI_BAUD);
talkMIDI(CONT_CHANGE , CHANNEL_VOL, VOLUME);
// set channel volume
talkMIDI(CONT_CHANGE, BANK_SELECT, MELODIC_BANK);
// select instrument bank
talkMIDI(PROG_CHANGE, MY_INSTRUMENT, 0);
// set specific instrument voice within bank.
}
void loop()
{
sensorBuffer = analogRead(SENSOR_PIN);
// get new sensor value to examine
if (abs(sensorBuffer-touchSensor) >= SENSOR_HYSTERESIS)
{
// only accept a new sensor value if it differs from the last accepted value by the minimum amount touchSensor = sensorBuffer;
// accept the new sensor value, but don't play a note yet...
if (touchSensor <= NOISE_FLOOR){
// only play a note if the accepted sensor value is below the noise floor
// we've passed both the anti-noise tests; time to calculate and play a new note
note = map(touchSensor, 0, 1023, HIGHEST_NOTE, LOWEST_NOTE);
// high sensor values (high resistence, light touch) map to low notes and V.V.
talkMIDI(CONT_CHANGE , ALL_OFF, 0);
// turn off previous note(s); comment this out if you like the notes to ring over each other
talkMIDI(NOTE_ON , note, VELOCITY);
// play new note!
Serial.print(touchSensor);
// report out values in case anyone is monitoring
Serial.print(' ');
Serial.println(note);
}}
delay (SIXTEENTH_NOTE_MS);
// tempo: do nothing for sixteenth note duration, the rest of the loop will execute so fast we'll never notice it }
//Sends all the MIDI messages we need for this sketch. Use with caution outside of this sketch: most of the MIDI protocol is not supported!
void talkMIDI(byte cmd, byte data1, byte data2)
{
digitalWrite(LED_PIN, HIGH);
// use Arduino LED to signal MIDI activity
MIDIserial.write(cmd);
MIDIserial.write(data1);
if(cmd != PROG_CHANGE) MIDIserial.write(data2);
//Send 2nd data byte if needed; all of our supported commands other than PROG_CHANGE have 2 data bytes
digitalWrite(LED_PIN, LOW);
}
Here are the error messages I am getting:
Arduino: 1.7.8 (Windows 8.1), Board: "Arduino Uno"
Using library MIDI in folder: C:\Program Files (x86)\Arduino\libraries\MIDI (legacy)
C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10708 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Program Files (x86)\Arduino\libraries\MIDI C:\Users\Joe\AppData\Local\Temp\build992557699153488874.tmp\SingingSnack.cpp -o C:\Users\Joe\AppData\Local\Temp\build992557699153488874.tmp\SingingSnack.cpp.o
SingingSnack.ino:4:1: error: 'include' does not name a type
SingingSnack.ino:56:11: error: expected constructor, destructor, or type conversion before '(' token
SingingSnack.ino: In function 'void setup()':
SingingSnack.ino:75:1: error: 'MIDIserial' was not declared in this scope
SingingSnack.ino: In function 'void loop()':
SingingSnack.ino:109:1: error: a function-definition is not allowed here before '{' token
SingingSnack.ino:117:1: error: expected '}' at end of input
Error compiling.