Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2017
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. /*
  2.   Midi_Theremin
  3.  
  4.   This program uses a ping sonar sensor to make a midi-compatible Theremin,
  5.   using the sparkfun Midi Breakout board
  6.  */
  7.  
  8. //we will need time for the ping to work
  9. #include <time.h>
  10.  
  11. //define the pins for the sonar ping sensor
  12. #define trigPin 9
  13. #define echoPin 10
  14.  
  15. //define the the pins for the button and send ON/OFF led:
  16. #define LED 13
  17. #define BUTTON 12
  18.  
  19. //define some Midi message definitions
  20. #define OFF 1
  21. #define ON 2
  22. #define WAIT 3
  23.  
  24. //keep track of the button state
  25. int buttonState = LOW;
  26.  
  27. //keep track of the last midi note played:
  28. byte lastMidiNote = 0;
  29.  
  30.  
  31.  
  32.  
  33.  
  34. // the setup routine runs once when you press reset:
  35. void setup() {                
  36.  
  37.   //pin 13 is output for now
  38.   pinMode(LED, OUTPUT);    
  39.  
  40.   //enable the button for reading
  41.   pinMode(BUTTON, INPUT);
  42.  
  43.   // initialize the ping pins
  44.   pinMode(trigPin, OUTPUT);
  45.   pinMode(echoPin, INPUT);
  46.  
  47.   //start serial with midi baudrate 31250
  48.   Serial.begin(31250);  
  49. }
  50.  
  51. // the loop routine runs over and over again forever:
  52. void loop() {
  53.  
  54.   //read all the various inputs, e.g. buttons, keys, knobs, etc.
  55.   readInputs();
  56.  
  57.   //if the button is currently HIGH, also read the ping sensor and send midi data:
  58.   if(buttonState == HIGH)
  59.     doTheremin();
  60.  
  61. }//loop()
  62.  
  63.  
  64.  
  65.  
  66.  
  67. //read the ping sensor and send midi
  68. void doTheremin(){
  69.  
  70.   //first get the ping itme
  71.   int pingTime = (int)getPing();
  72.    
  73.   //make sure it's within an acceptable range
  74.   if(pingTime < 0) pingTime=0;
  75.   if(pingTime > 127) pingTime=127;
  76.  
  77.   //Serial.println(pingTime);
  78.   pingTime = (int)(((float)pingTime / 2.0f) + 30.0f);
  79.  
  80.   //convert ping time to a value between 0 and 127
  81.   byte midiNote = (byte)(127 - pingTime);
  82.   Midi_Send(0x90, (byte)midiNote, 0x64);
  83.  
  84.   //check if the note changed:
  85.   if(midiNote != lastMidiNote){
  86.    
  87.     //first, turn off all midi notes:
  88.     allMidiOff();
  89.     //turn off the last midi note:
  90.     //Midi_Send(0x80, (byte)lastMidiNote, 0x0);
  91.    
  92.     //next, turn on just this new midi note:
  93.     Midi_Send(0x90, (byte)midiNote, 0x64);
  94.    
  95.     //save the midi note!
  96.     lastMidiNote = midiNote;
  97.    
  98.   }//end if midi note changed
  99.  
  100. }//doTheremin()
  101.  
  102.  
  103. //turn off all midi:
  104. void allMidiOff(){
  105.  
  106.   //loop thru all 128 midi keys and turn them all off
  107.   for(int i=9; i < 128; i++){
  108.      
  109.     //turn off midi note:
  110.     Midi_Send(0x80, i, 0x0);
  111.    
  112.   }//next i
  113.  
  114. }//allMidiOff()
  115.  
  116.  
  117. //read all the various inputs, e.g. buttons, keys, knobs, etc.
  118. void readInputs(){
  119.  
  120.   //check the state of the button
  121.   int buttonNow = digitalRead(BUTTON);
  122.  
  123.   //check if the button CHANGED
  124.   if(buttonNow != buttonState){
  125.     buttonState = buttonNow;
  126.     event_buttonChange();
  127.  
  128.   }//end if
  129.  
  130. }//readInputs()
  131.  
  132.  
  133. //when the button changes!
  134. void event_buttonChange(){
  135.    
  136.   //send the correct midi based ont he current state of the button
  137.   if(buttonState == HIGH){
  138.    
  139.     ////turn "transmit" LED on
  140.     digitalWrite(LED, HIGH);
  141.    
  142.   }else{
  143.    
  144.     //turn "transmit" LED off
  145.     digitalWrite(LED, LOW);
  146.    
  147.     //turn off all midi, since we are disabling the output by letting go of the button
  148.     allMidiOff();
  149.    
  150.   }//endif button
  151.  
  152. }//event_button_Change()
  153.  
  154.  
  155. //send a midi message!
  156. void Midi_Send(byte cmd, byte data1, byte data2) {
  157.  
  158.   Serial.write(cmd);
  159.   Serial.write(data1);
  160.   Serial.write(data2);
  161.  
  162. }//Midi_Send
  163.  
  164.  
  165. //read the ping sensor and get the miliseconds between the ping and echo
  166. long getPing(){
  167.  
  168.   //declare varibles for the ping duration, and the calculated distance
  169.   long duration, distance;
  170.  
  171.   //turn the trigger pin OFF
  172.   digitalWrite(trigPin, LOW);
  173.  
  174.   //wait 2 microseconds
  175.   delayMicroseconds(2);
  176.  
  177.   //turn the trigger pin ON
  178.   digitalWrite(trigPin, HIGH);
  179.  
  180.   //wait 10 microseconds
  181.   delayMicroseconds(10);
  182.  
  183.   //turn the trig pin back OFF
  184.   digitalWrite(trigPin, LOW);
  185.  
  186.   //pulse in the bits for the duration as determined by the ping sensor
  187.   duration = pulseIn(echoPin, HIGH);
  188.  
  189.   //mysterymath to convert the ping duration into a distance
  190.   distance = (duration/2) / 29.1;
  191.  
  192.   //return the distance we calculated
  193.   return distance;
  194.  
  195. }//long getPing()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement