Monday, December 21, 2009

My First Instrument

My Arduino arrived today, and after making sure everything worked properly, I built my first musical instrument from the thing.

It's just a little example to make sure I understand how to do MIDI with it. There's a button. You press it, and it plays a random note until you release it. Press it again, and it plays a different random note.

And here'smy first performance.


Elliot Carter, watch out.


#include "MIDI.h"


MIDI OUT to Arduino Serial pin 1
MIDI IN from Arduino Serial pin 0

Digital pin 2 to a pushbutton switch, and to 5v through a 10k resistor
other leg of switch to 5v


#define LED 13
#define BUTTON 2

int buttonState = LOW;
int prevButtonState = LOW;
int note = 43;

void setup() {
pinMode(LED, OUTPUT);

void loop() {
buttonState = digitalRead(BUTTON);
if (buttonState == HIGH && prevButtonState == LOW) {
prevButtonState = HIGH;
digitalWrite(LED, HIGH);
MIDI.sendNoteOn(note, 127, 1);
} else if (buttonState == LOW) {
if (prevButtonState == HIGH) {
prevButtonState = LOW;
MIDI.sendNoteOff(note, 0, 1);
note = random(30, 70);
digitalWrite(LED, LOW);

No comments:

Post a Comment