Sample code for Ultrasonic Sensor and buzzer with detailed documentation


#1
   /**
  • @file UltrasonicSensorAndBuzzer.ino
  • @author Benjamin Makowsky
  • @version V1.0.0
  • @date 2016/01/07
  • @brief Description: Code and Documentation for Ultrasonic Sensor and Buzzer

*/
//If using a bluetooth module you must disconnect the module before uploading to baseboard
//or else it will fail as of Arduino 1.6.6 and Library V3 due to hardware conflicts.

#include “MeOrion.h”

MeUltrasonicSensor ultraSensor(PORT_4); // Ultrasonic sensor can only connect to ports 3, 4, 6, 7, 8 of MeOrion

void setup()
{
Serial.begin(9600); //Begins communication with the computer at a baud rate of 9600
}

void loop()
{
Serial.print("Distance : "); //Prints the string "Distance : " over the serial most likely the usb. Can be seen using serial monitor in arduino tools setting

Serial.print(ultraSensor.distanceCm()); //Prints the value received from the Ultrasonic Sensor in Centimeters. Can be changed to inches with .distanceIn()

Serial.println(" cm");//Prints the string “cm” followed by a new line

if(ultraSensor.distanceCm()<20){ //if statement to check if data received is less than the value 20, in this case 20 centimeters

//If value is true the following code executes if false, code will skip this section
buzzerOn(); //turns buzzer on
delay(500); //waits 500 milliseconds or half a second with a minimum value of 100 or 1/10 of a second
buzzerOff(); //turns buzzer off

}
delay(100); //waits 100 milliseconds
}


#2