/* sonarToBuzz Takes distance data to from an ultrasonic sonar sensor and converts it to and audible frequency through a buzzer. The circuit: ultrasonic sonar sensor connected to positive, negative and analog 1. buzzer from digital 2 to ground. */ int sonarPin = 1; // Ultrasound signal pin ANALOG-1 int buzzPin = 2; // buzz connected to DIGITAL-2 int val = 0; //takes sonar value and creates an audible frequency int ultrasoundValue = 0; //only used for serial read void setup() { // Serial.begin(9600); // Sets the baud rate to 9600 pinMode(buzzPin, OUTPUT); // sets the DIGITAL-2 pin as output } void loop() { val = 0; pinMode(sonarPin, INPUT); // Switch signalpin ANALOG-1 to input val = (analogRead(sonarPin)*10) + 800; // Append signal value to val, ranged to be an audible tone. digitalWrite(buzzPin, HIGH); // sets the buzz on delayMicroseconds(val); // waits, proportional to the sensed value digitalWrite(buzzPin, LOW); // sets the buzz off delayMicroseconds(val); // waits, proportional to the sensed value /* Writing out values to the serial port * ------------------------------------------------------------------- ultrasoundValue = val; // Append analog read to ultrasoundValue Serial.print("Distance in CM= "); // Example identifier for the sensor Serial.print(ultrasoundValue); Serial.println(); */ }