Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Midi_Theremin
- This program uses a ping sonar sensor to make a midi-compatible Theremin,
- using the sparkfun Midi Breakout board
- */
- //we will need time for the ping to work
- #include <time.h>
- //define the pins for the sonar ping sensor
- #define trigPin 9
- #define echoPin 10
- //define the the pins for the button and send ON/OFF led:
- #define LED 13
- #define BUTTON 12
- //define some Midi message definitions
- #define OFF 1
- #define ON 2
- #define WAIT 3
- //keep track of the button state
- int buttonState = LOW;
- //keep track of the last midi note played:
- byte lastMidiNote = 0;
- // the setup routine runs once when you press reset:
- void setup() {
- //pin 13 is output for now
- pinMode(LED, OUTPUT);
- //enable the button for reading
- pinMode(BUTTON, INPUT);
- // initialize the ping pins
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- //start serial with midi baudrate 31250
- Serial.begin(31250);
- }
- // the loop routine runs over and over again forever:
- void loop() {
- //read all the various inputs, e.g. buttons, keys, knobs, etc.
- readInputs();
- //if the button is currently HIGH, also read the ping sensor and send midi data:
- if(buttonState == HIGH)
- doTheremin();
- }//loop()
- //read the ping sensor and send midi
- void doTheremin(){
- //first get the ping itme
- int pingTime = (int)getPing();
- //make sure it's within an acceptable range
- if(pingTime < 0) pingTime=0;
- if(pingTime > 127) pingTime=127;
- //Serial.println(pingTime);
- pingTime = (int)(((float)pingTime / 2.0f) + 30.0f);
- //convert ping time to a value between 0 and 127
- byte midiNote = (byte)(127 - pingTime);
- Midi_Send(0x90, (byte)midiNote, 0x64);
- //check if the note changed:
- if(midiNote != lastMidiNote){
- //first, turn off all midi notes:
- allMidiOff();
- //turn off the last midi note:
- //Midi_Send(0x80, (byte)lastMidiNote, 0x0);
- //next, turn on just this new midi note:
- Midi_Send(0x90, (byte)midiNote, 0x64);
- //save the midi note!
- lastMidiNote = midiNote;
- }//end if midi note changed
- }//doTheremin()
- //turn off all midi:
- void allMidiOff(){
- //loop thru all 128 midi keys and turn them all off
- for(int i=9; i < 128; i++){
- //turn off midi note:
- Midi_Send(0x80, i, 0x0);
- }//next i
- }//allMidiOff()
- //read all the various inputs, e.g. buttons, keys, knobs, etc.
- void readInputs(){
- //check the state of the button
- int buttonNow = digitalRead(BUTTON);
- //check if the button CHANGED
- if(buttonNow != buttonState){
- buttonState = buttonNow;
- event_buttonChange();
- }//end if
- }//readInputs()
- //when the button changes!
- void event_buttonChange(){
- //send the correct midi based ont he current state of the button
- if(buttonState == HIGH){
- ////turn "transmit" LED on
- digitalWrite(LED, HIGH);
- }else{
- //turn "transmit" LED off
- digitalWrite(LED, LOW);
- //turn off all midi, since we are disabling the output by letting go of the button
- allMidiOff();
- }//endif button
- }//event_button_Change()
- //send a midi message!
- void Midi_Send(byte cmd, byte data1, byte data2) {
- Serial.write(cmd);
- Serial.write(data1);
- Serial.write(data2);
- }//Midi_Send
- //read the ping sensor and get the miliseconds between the ping and echo
- long getPing(){
- //declare varibles for the ping duration, and the calculated distance
- long duration, distance;
- //turn the trigger pin OFF
- digitalWrite(trigPin, LOW);
- //wait 2 microseconds
- delayMicroseconds(2);
- //turn the trigger pin ON
- digitalWrite(trigPin, HIGH);
- //wait 10 microseconds
- delayMicroseconds(10);
- //turn the trig pin back OFF
- digitalWrite(trigPin, LOW);
- //pulse in the bits for the duration as determined by the ping sensor
- duration = pulseIn(echoPin, HIGH);
- //mysterymath to convert the ping duration into a distance
- distance = (duration/2) / 29.1;
- //return the distance we calculated
- return distance;
- }//long getPing()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement