Advertisement
j0h

Arduino UltraSonic Theramin lol

j0h
Jun 3rd, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. #include <string.h>
  2. // this constant won't change. It's the pin number of the sensor's output:
  3. const int pingPin = 0;
  4. String msg = "";
  5.  
  6. void setup() {
  7.   // initialize serial communication:
  8.   Serial.begin(9600);
  9.   delay(5000);
  10. }
  11. long distance(){
  12.   // establish variables for duration of the ping, and the distance result
  13.   // in inches and centimeters:
  14.   long duration, inches, cm;
  15.  
  16.   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  17.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  18.   pinMode(pingPin, OUTPUT);
  19.   digitalWrite(pingPin, LOW);
  20.   delayMicroseconds(2);
  21.   digitalWrite(pingPin, HIGH);
  22.   delayMicroseconds(5);
  23.   digitalWrite(pingPin, LOW);
  24.  
  25.   // The same pin is used to read the signal from the PING))): a HIGH pulse
  26.   // whose duration is the time (in microseconds) from the sending of the ping
  27.   // to the reception of its echo off of an object.
  28.   pinMode(pingPin, INPUT);
  29.   duration = pulseIn(pingPin, HIGH);
  30.  
  31.   // convert the time into a distance
  32.   inches = microsecondsToInches(duration);
  33.   cm= microsecondsToCentimeters(duration);
  34.    
  35.  // Serial.print(cm);
  36.  // Serial.print(" cm. ");
  37.   //Serial.println();
  38.  
  39.   return inches;
  40.  
  41.   }
  42.  
  43.  
  44. void loop() {
  45.   long d = distance();
  46.   Serial.println(d);
  47.   piano(d);
  48.   //Serial.print(distance2text(d));
  49.   //Serial.println();
  50.  // if(msg.length()>20){
  51.  //       Serial.println(msg);
  52.  //   msg="";
  53.  //   }
  54.   delay(100);
  55. }
  56. long microsecondsToInches(long microseconds) {
  57.   // According to Parallax's datasheet for the PING))), there are 73.746
  58.   // microseconds per inch (i.e. sound travels at 1130 feet per second).
  59.   // This gives the distance travelled by the ping, outbound and return,
  60.   // so we divide by 2 to get the distance of the obstacle.
  61.   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  62.   return microseconds / 74 / 2;
  63. }
  64.  
  65. long microsecondsToCentimeters(long microseconds) {
  66.   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  67.   // The ping travels out and back, so to find the distance of the object we
  68.   // take half of the distance travelled.
  69.   return microseconds / 29 / 2;
  70. }
  71.  
  72. String distance2text(long cm){
  73. if (cm < 28){
  74.   switch(cm){
  75.   case 0 :
  76.     msg+="\n";
  77.       break;  
  78.   case 1 :
  79.     msg+="A";
  80.       break;
  81.   case 2 :
  82.     msg+="B";
  83.       break;  
  84.   case 3 :
  85.     msg+="C";
  86.       break;
  87.   case 4 :
  88.       msg+="D";
  89.       break;  
  90.   case 5 :
  91.       msg+="E";
  92.       break;  
  93.   case 6 :
  94.       msg+="F";
  95.       break;
  96.   case 7 :
  97.       msg+="G";
  98.       break;  
  99.   case 8 :
  100.       msg+="H";
  101.       break;
  102.   case 9 :
  103.         msg+="I";
  104.       break;
  105.   case 10 :
  106.         msg+="J";
  107.       break;
  108.   case 11 :
  109.         msg+="K";
  110.       break;
  111.   case 12 :
  112.       msg+="L";
  113.       break;
  114.   case 13 :
  115.         msg+="M";
  116.       break;
  117.   case 14 :
  118.         msg+="N";
  119.       break;
  120.   case 15 :
  121.        msg+="O";
  122.       break;
  123.   case 16 :
  124.        msg+="P";
  125.       break;
  126.   case 17 :
  127.       msg+="Q";
  128.       break;
  129.   case 18 :
  130.       msg+="R";
  131.       break;  
  132.   case 19 :
  133.        msg+="S";
  134.       break;
  135.   case 20 :
  136.       msg+="T";
  137.       break;
  138.   case 21 :
  139.       msg+="U";
  140.       break;
  141.   case 22 :
  142.       msg+="V";
  143.       break;  
  144.   case 23 :
  145.         msg+="W";
  146.       break;
  147.   case 24 :
  148.       msg+="X";
  149.       break;  
  150.   case 25 :
  151.         msg+="Y";
  152.       break;
  153.   case 26 :
  154.       msg+="Z";
  155.       break;  
  156.   case 27 :
  157.       msg+="\n";
  158.       break;        
  159.   defualt: Serial.print(" ");  
  160.     }
  161.   }
  162.   return msg;
  163.   }
  164.  
  165.  
  166. String piano(long inches){
  167.  //wetyu asdfghjk
  168.  
  169.   if (inches < 13){
  170.     int n = 100;
  171.   switch(inches){
  172.   case 0 :
  173.     Keyboard.press(KEY_W);
  174.     delay(n);
  175.     Keyboard.release(KEY_W);
  176.       break;  
  177.   case 1 :
  178.     Keyboard.press(KEY_E);
  179.     delay(n);
  180.     Keyboard.release(KEY_E);
  181.       break;
  182.   case 2 :
  183.      Keyboard.press(KEY_T);
  184.     delay(n);
  185.     Keyboard.release(KEY_T);
  186.       break;  
  187.   case 3 :
  188.     Keyboard.press(KEY_Y);
  189.     delay(n);
  190.     Keyboard.release(KEY_Y);
  191.       break;
  192.   case 4 :
  193.     Keyboard.press(KEY_U);
  194.     delay(n);
  195.     Keyboard.release(KEY_U);
  196.       break;  
  197.   case 5 :
  198.     Keyboard.press(KEY_A);
  199.     delay(n);
  200.     Keyboard.release(KEY_A);
  201.       break;  
  202.   case 6 :
  203.     Keyboard.press(KEY_S);
  204.     delay(n);
  205.     Keyboard.release(KEY_S);
  206.       break;
  207.   case 7 :
  208.     Keyboard.press(KEY_D);
  209.     delay(n);
  210.     Keyboard.release(KEY_D);
  211.       break;  
  212.   case 8 :
  213.     Keyboard.press(KEY_F);
  214.     delay(n);
  215.     Keyboard.release(KEY_F);
  216.     break;
  217.   case 9 :
  218.     Keyboard.press(KEY_G);
  219.     delay(n);
  220.     Keyboard.release(KEY_G);
  221.       break;
  222.   case 10 :
  223.     Keyboard.press(KEY_H);
  224.     delay(n);
  225.     Keyboard.release(KEY_H);
  226.       break;
  227.     Keyboard.press(KEY_J);
  228.     delay(n);
  229.     Keyboard.release(KEY_J);
  230.       break;
  231.   case 12 :
  232.     Keyboard.press(KEY_K);
  233.     delay(n);
  234.     Keyboard.release(KEY_K);  
  235.    
  236.       break;
  237.                  
  238.  default: Serial.print(" ");  
  239.  
  240.   }
  241.     }else{
  242.    
  243.     ;
  244.   }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement