Ive have stitched together this code just for fun to try to make some sound experiments with ping sensors. This however get boring quite fast. You might have some suggestions to further experiments? Thanks alot!
const int pingPin = 7;
int soundpin = 9;
void setup() {
 Serial.begin(9600);
 pinMode(soundpin, OUTPUT);
}
void loop()
{
 long duration, inches, cm;
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);
 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);
 cm = microsecondsToCentimeters(duration);
Â
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();
 delay(100);
 int level = cm;
  level = map(level, 0, 40, 100, 400);
Â
  if(cm < 40){
   Â
   tone(9, level);
  }
  else {
   noTone(9);
  }
}
long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}