{"id":32,"date":"2006-01-19T03:43:01","date_gmt":"2006-01-19T08:43:01","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/32"},"modified":"2007-08-06T23:40:30","modified_gmt":"2007-08-07T04:40:30","slug":"midi-player-in-wiringarduino","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/32\/","title":{"rendered":"MIDI player in Wiring\/Arduino"},"content":{"rendered":"<p>Ryan Holsopple got a basic MIDI out working using an Arduino board. Inspired, I borrowed from his example and re-wrote my MIDI player to work on an Arduino board.<\/p>\n<p><!--more--><\/p>\n<pre>\n\/*\n  MIDI player\n by Tom Igoe\n \n Reads an analog value in on analog channel 0, and if a switch on pin 10 is pressed,\n plays a note in the middle C octave corresponding to the analog value. \n \n Thanks to Ryan Holsopple for getting MIDI out working using Serial.print()\n \n Created 19 Jan. 2006\n updated 14 June 2006\n \n *\/\n\/\/ The switch is on Arduino pin 10:\n#define switchPin 10\n\/\/ Middle C (MIDI note value 60) is the lowest note we'll play:\n#define middleC 60\n\/\/  Indicator LED:\n#define LEDpin 13\n\n\/\/ Variables: \nchar note = 0;            \/\/ The MIDI note value to be played\nint AnalogValue = 0;           \/\/ value from the analog input\nint lastNotePlayed = 0;   \/\/ note turned on when you press the switch\nint lastSwitchState = 0;  \/\/ state of the switch during previous time through the main loop\nint currentSwitchState = 0;\n\nvoid setup() {\n  \/\/  set the states of the I\/O pins:\n  pinMode(switchPin, INPUT);\n  pinMode(LEDpin, OUTPUT);\n  \/\/  Set MIDI baud rate:\n  Serial.begin(31250);\n  blink(3);\n}\n\nvoid loop() {\n  \/\/  My potentiometer gave a range from 0 to 1023:\n  AnalogValue = analogRead(0);\n  \/\/  convert to a range from 0 to 11:\n  note = AnalogValue \/ 100;\n  currentSwitchState = digitalRead(switchPin);\n  \/\/ Check to see that the switch is pressed:\n  if (currentSwitchState == 1) {\n    \/\/  check to see that the switch wasn't pressed last time\n    \/\/  through the main loop:\n    if (lastSwitchState == 0) {\n      \/\/ set the note value based on the analog value, plus a couple octaves:\n      note = note + middleC;\n      \/\/ start a note playing:\n      noteOn(0x90, note, 0x40);\n      \/\/ save the note we played, so we can turn it off:\n      lastNotePlayed = note;\n      digitalWrite(LEDpin, HIGH);\n    }\n    else    \/\/ if the switch is not pressed:\n    \/\/  but the switch was pressed last time through the main loop:\n    if (lastSwitchState == 1) {\n      \/\/  stop the last note played:\n      noteOn(0x90, lastNotePlayed, 0x00);\n      digitalWrite(LEDpin, LOW);\n    } \n  }\n\n  \/\/  save the state of the switch for next time\n  \/\/  through the main loop:\n  lastSwitchState = currentSwitchState;\n}\n\n\/\/  plays a MIDI note.  Doesn't check to see that\n\/\/  cmd is greater than 127, or that data values are  less than 127:\nvoid noteOn(char cmd, char data1, char data2) {\n  Serial.print(cmd, BYTE);\n  Serial.print(data1, BYTE);\n  Serial.print(data2, BYTE);\n}\n\n\/\/ Blinks an LED 3 times\nvoid blink(int howManyTimes) {\n  int i;\n  for (i=0; i< howManyTimes; i++) {\n    digitalWrite(LEDpin, HIGH);\n    delay(100);\n    digitalWrite(LEDpin, LOW);\n    delay(100);\n  }\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/* MIDI player by Tom Igoe Reads an analog value in on analog channel 0, and if a switch on pin 10 is pressed, plays a note in the middle C octave corresponding to the analog value.  Thanks to Ryan Holsopple for getting MIDI out working using serialWrite() Created 19 Jan. 2006 updated *\/ \/\/ The switch is on Arduino pin 10: #define switchPin 10 \/\/ Middle C (MIDI note value 60) is the lowest note we&#8217;ll play: #define middleC 60 \/\/ Indicator LED: #define LEDpin 13 \/\/ Variables: char note = 0; \/\/ The MIDI note value to be played int AnalogValue = 0; \/\/ value from the analog input int lastNotePlayed = 0; \/\/ note turned on when you press the switch int lastSwitchState = 0; \/\/ state of the switch during previous time through the main loop \/\/Function prototype: void noteOn(char cmd, char data1, char data2); void blink(); void setup() { \/\/ set the states of the I\/O pins: pinMode(switchPin, INPUT); pinMode(LEDpin, OUTPUT); \/\/ Set MIDI baud rate: beginSerial(31250); blink(3); } void loop() { \/\/ My potentiometer gave a range from 0 to 1023: AnalogValue = analogRead(0); \/\/ convert to a range from 0 to 11: note = AnalogValue \/ 100; \/\/ Check to see that the switch is pressed: if (digitalRead(switchPin) == 1) { \/\/ check to see that the switch wasn&#8217;t pressed last time \/\/ through the main loop: if (lastSwitchState == 0) { \/\/ set the note value based on the analog value note = note + middleC; \/\/ start a note playing: noteOn(0x90, note, 0x40); \/\/ save the note we played, so we can turn it off: lastNotePlayed = note; } else \/\/ if the switch is not pressed: \/\/ but the switch was pressed last time through the main loop: if (lastSwitchState == 1) { \/\/ stop the last note played: noteOn(0x90, lastNotePlayed, 0x00); } } \/\/ save the state of the switch for next time \/\/ through the main loop: lastSwitchState = switchPin; } \/\/ plays a MIDI note.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}