Created
April 1, 2017 04:50
-
-
Save Tiriree/2607f4f92706e8fd9e2fbe512a4b8bb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Tiri Midi controller for Tom Igoe's tangible interaction class 2017 | |
//inspied by Tom Igoe | |
#include <SoftwareSerial.h> | |
const int switchPin = 10; // The switch is on Arduino pin 10 | |
const int LEDpin = 13; // Indicator LED | |
// Variables: | |
byte 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 | |
int currentSwitchState = 0; | |
//software serial | |
SoftwareSerial midiSerial(2, 3); | |
void setup() { | |
pinMode(switchPin, INPUT); | |
pinMode(LEDpin, OUTPUT); | |
Serial.begin(9600); | |
blink(3); | |
midiSerial.begin(31250); | |
} | |
void loop() { | |
AnalogValue = analogRead(0); | |
//convert to 0-127 | |
note = AnalogValue/8; | |
currentSwitchState = digitalRead(switchPin); | |
// Check to see that the switch is pressed | |
if (currentSwitchState == 1) { | |
// check to see that the switch wasn't pressed last time | |
if (lastSwitchState == 0) { | |
// set the note value based on the analog value, plus a couple octaves: | |
// note = note + 60; | |
// start a note playing: | |
noteOn(0x90, note, 0x40); | |
lastNotePlayed = note; | |
digitalWrite(LEDpin, HIGH); | |
} | |
} | |
else { | |
if (lastSwitchState == 1) { | |
noteOn(0x90, lastNotePlayed, 0x00); | |
digitalWrite(LEDpin, LOW); | |
} | |
} | |
lastSwitchState = currentSwitchState; | |
} | |
void noteOn(byte cmd, byte data1, byte data2) { | |
midiSerial.write(cmd); | |
midiSerial.write(data1); | |
midiSerial.write(data2); | |
Serial.print("cmd: "); | |
Serial.print(cmd); | |
Serial.print(", data1: "); | |
Serial.print(data1); | |
Serial.print(", data2: "); | |
Serial.println(data2); | |
} | |
void blink(int howManyTimes) { | |
int i; | |
for (i=0; i< howManyTimes; i++) { | |
digitalWrite(LEDpin, HIGH); | |
delay(100); | |
digitalWrite(LEDpin, LOW); | |
delay(100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment