CCCPaste

Arduino UltraSonic Theramin lol

#include 
// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 0;
String msg = "";

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  delay(5000);
}
long distance(){
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm= microsecondsToCentimeters(duration);
   
 // Serial.print(cm);
 // Serial.print(" cm. ");
  //Serial.println();

  return inches;
  
  }


void loop() {
  long d = distance();
  Serial.println(d);
  piano(d);
  //Serial.print(distance2text(d));
  //Serial.println();
 // if(msg.length()>20){
 //       Serial.println(msg);
 //   msg="";
 //   }
  delay(100);
}
long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;
}

String distance2text(long cm){
if (cm < 28){
  switch(cm){
  case 0 : 
    msg+="\n";
      break;  
  case 1 :
    msg+="A";
      break;
  case 2 :
    msg+="B";
      break;  
  case 3 :
    msg+="C";
      break;
  case 4 :
      msg+="D";
      break;  
  case 5 :
      msg+="E";
      break;  
  case 6 :
      msg+="F";
      break;
  case 7 :
      msg+="G";
      break;  
  case 8 :
      msg+="H";
      break;
  case 9 :
        msg+="I";
      break;
  case 10 :
        msg+="J";
      break;
  case 11 :
        msg+="K";
      break;
  case 12 :
      msg+="L";
      break;
  case 13 :
        msg+="M";
      break;
  case 14 :
        msg+="N";
      break;
  case 15 :
       msg+="O";
      break; 
  case 16 :
       msg+="P";
      break;
  case 17 :
      msg+="Q";
      break;
  case 18 :
      msg+="R";
      break;  
  case 19 :
       msg+="S";
      break;
  case 20 :
      msg+="T";
      break;
  case 21 :
      msg+="U";
      break;
  case 22 :
      msg+="V";
      break;  
  case 23 :
        msg+="W";
      break;
  case 24 :
      msg+="X";
      break;  
  case 25 :
        msg+="Y";
      break;
  case 26 :
      msg+="Z";
      break;  
  case 27 :
      msg+="\n";
      break;        
  defualt: Serial.print(" ");  
    }
  }
  return msg;
  }


String piano(long inches){
 //wetyu asdfghjk
  
  if (inches < 13){
    int n = 100;
  switch(inches){
  case 0 : 
    Keyboard.press(KEY_W);
    delay(n);
    Keyboard.release(KEY_W);
      break;  
  case 1 :
    Keyboard.press(KEY_E);
    delay(n);
    Keyboard.release(KEY_E);
      break;
  case 2 :
     Keyboard.press(KEY_T);
    delay(n);
    Keyboard.release(KEY_T);
      break;  
  case 3 :
    Keyboard.press(KEY_Y);
    delay(n);
    Keyboard.release(KEY_Y);
      break;
  case 4 :
    Keyboard.press(KEY_U);
    delay(n);
    Keyboard.release(KEY_U);
      break;  
  case 5 :
    Keyboard.press(KEY_A);
    delay(n);
    Keyboard.release(KEY_A);
      break;  
  case 6 :
    Keyboard.press(KEY_S);
    delay(n);
    Keyboard.release(KEY_S);
      break;
  case 7 :
    Keyboard.press(KEY_D);
    delay(n);
    Keyboard.release(KEY_D);
      break;  
  case 8 :
    Keyboard.press(KEY_F);
    delay(n);
    Keyboard.release(KEY_F);
    break;
  case 9 :
    Keyboard.press(KEY_G);
    delay(n);
    Keyboard.release(KEY_G);
      break;
  case 10 :
    Keyboard.press(KEY_H);
    delay(n);
    Keyboard.release(KEY_H); 
      break;
    Keyboard.press(KEY_J);
    delay(n);
    Keyboard.release(KEY_J); 
      break;
  case 12 :
    Keyboard.press(KEY_K);
    delay(n);
    Keyboard.release(KEY_K);  
    
      break;
                  
 default: Serial.print(" ");  
  
  }
    }else{
    
    ;
  }
}