Go Down

Topic: Talk MIDI command (Read 733 times) previous topic - next topic

dbecerra

I am trying to have a switch trigger notes that will be produced by a wave shield and then have a servo move about 20 degrees..... i have a code and have gotten my music code to work and the switch and servo to work... but I can't seem to get the switch to trigger the music... can anyone help?
this is my code thus far


#include <SoftwareSerial.h>



#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3);

#include <Servo.h>

Servo myservo;



byte note = 0;
byte resetMIDI = 4;
int SwitchPin = 0; //switchpin on pin 7
int pos = 70;
int instrument = 0;
int LedPin = 13;



void setup()
{
pinMode(SwitchPin, INPUT);
pinMode(LedPin, OUTPUT);
myservo.attach(9);

Serial.begin(57600);
mySerial.begin(31250);

pinMode(resetMIDI,OUTPUT);
digitalWrite(resetMIDI, LOW);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
talkMIDI(0xB0,0x07,120);
}


void loop()
{
SwitchPin=digitalRead(7); //read pin 7
myservo.write(pos); //puts servo to position 70 degrees
delay(5);
pos=constrain(pos,0,180);



if(SwitchPin==1)

{
  delay(500);
//Serial.printIn(instrument,"Demo Fancy Sounds");
talkMIDI(0xB0, 71, 0x79);
Serial.print("Instrument:");
//Serial.printIn(instrument, DEC);

talkMIDI(0xC0, 71, 0);

noteOn(0, 50, 60);
delay(200);
noteOff(0, 50, 60);
delay(100);

noteOn(0, 53, 60);
delay(200);
noteOff(0, 53, 60);
delay(100);

noteOn(0, 52, 60);
delay(400);
noteOff(0, 52, 60);
delay(100);

noteOn(0, 53, 60);
delay(400);
noteOff(0, 53, 60);
delay(100);

  noteOn(0, 55, 60);
delay(400);
noteOff(0, 55, 60);
delay(200);

  noteOn(0, 58, 60);
delay(300);
noteOff(0, 53, 60);
delay(100);
}

myservo.write(pos=90);
}
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);
}


Nick Gammon

How to use this forum


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
http://www.gammon.com.au/electronics

marco_c

What do you expect it to do and what is it doing that does not work?
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

Nantonos

Code: [Select]
//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);
}


You are not doing anything with data2.

Code: [Select]
//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);
  mySerial.write(data2);
}

Go Up