Ultrasonic Sensor + Light Alarm
/* Ultrasonic Module Distance Measurement http://shop34791330.taobao.com/ // Defy Lai Flagship Store http://qixingchong.tmall.com/ // Qixing Insect Flagship Store http://www.doflye.net // Defy Lai Technology Forum http://www.doflye.net/forum-98-1.html // Defy Lai Forum Arduino Section Ultrasonic Distance Measurement: Trigger Signal Trig: Trigger high level pulse greater than 10us Feedback Signal Echo: The length of the returned high level is the distance in us digital Calculate the distance through the speed of sound and the collected time. Ultrasonic connection reference URL: http://www.doflye.net/viewthread.php?tid=5307&extra= */
/*// Basic Function: Measure Distance
// Pin Definition const int trig = 8; // Trigger Signal const int echo = 9; // Feedback Signal
// Initialization void setup() { pinMode(echo, INPUT); pinMode(trig, OUTPUT); // Set trigger port to output, feedback port to input Serial.begin(9600);}// Main Loop void loop() { long IntervalTime=0; // Define a time variable while(1){ digitalWrite(trig, 1);// Set high level delayMicroseconds(15);// Delay 15us digitalWrite(trig, 0);// Set to low level IntervalTime=pulseIn(echo, HIGH);// Use the built-in function to sample the width of the returned high level, in us float S=IntervalTime/58.31; // Use floating-point to calculate the distance, in cm // The speed of sound in air is 343 meters/second, or 34300 centimeters/second Serial.println(S);// Output distance value through serial S=0;IntervalTime=0;// Clear corresponding value. delay(500);// Delay interval determines the sampling frequency, change parameters according to actual needs }}*/
// Extended Function
// Define Pins const int echo = 9; // Ultrasonic feedback pin const int trig = 8; // Ultrasonic trigger pin const int redLed = 7; // Red LED pin const int greenLed = 10; // Green LED pin
// Initialization void setup() { pinMode(echo, INPUT); pinMode(trig, OUTPUT); pinMode(redLed, OUTPUT); // Set red LED as output pinMode(greenLed, OUTPUT); // Set green LED as output // Set trigger port to output, feedback port to input Serial.begin(9600); // Start serial communication}
// Main Loop void loop() { long IntervalTime = 0; // Define a time variable // Emit ultrasonic pulse signal digitalWrite(trig, LOW); // Ensure trig pin is initially low delayMicroseconds(2); // Ensure trig pin is stable digitalWrite(trig, HIGH); // Set high level delayMicroseconds(15); // Delay 15us digitalWrite(trig, LOW); // Set to low level
// Collect feedback high level time IntervalTime = pulseIn(echo, HIGH); // Use the built-in function to sample the width of the returned high level, in us float S = IntervalTime / 58.31; // Use floating-point to calculate the distance, in cm // The speed of sound in air is 343 meters/second, or 34300 centimeters/second
// Output distance to serial Serial.println(S); // Output distance value through serial // Determine distance and control LED lights if (S < 20.0) { // Distance less than 5 centimeters digitalWrite(redLed, HIGH); // Light up red LED digitalWrite(greenLed, LOW); // Turn off green LED } else { // Distance greater than or equal to 5 centimeters digitalWrite(greenLed, HIGH); // Light up green LED digitalWrite(redLed, LOW); // Turn off red LED } // Clear values and delay S = 0; IntervalTime = 0; delay(500); // Delay interval determines the sampling frequency, change parameters according to actual needs}