Ultrasonic Code Example
By Roee Bloch
All right Reserved
Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com
This source is subject to the Roee Bloch License.
Please see the License.txt file for more information.
All other rights reserved.
THIS CODE AND INFORMATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
/*
* HCSR04Ultrasonic/examples/UltrasonicDemo/UltrasonicDemo.pde
*
* SVN Keywords
* ———————————-
* $Author: cnobile $
* $Date: 2011-09-17 02:43:12 -0400 (Sat, 17 Sep 2011) $
* $Revision: 29 $
* ———————————-
*/
#include <Ultrasonic.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 13
#define RED 7
#define GREEN 6
#define BLUE 5
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
void setup()
{
pinMode(RED,OUTPUT);
pinMode(GREEN,OUTPUT);
pinMode(BLUE,OUTPUT);
Serial.begin(9600);
}
void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
if (cmMsec<10){
digitalWrite(RED,HIGH);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,LOW);
}
if ((cmMsec<20)&& (cmMsec>=10)){
digitalWrite(RED,LOW);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,LOW);
}
if (cmMsec>30){
digitalWrite(RED,LOW);
digitalWrite(GREEN,LOW);
digitalWrite(BLUE,HIGH);
}
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
Serial.print(“MS: “);
Serial.print(microsec);
Serial.print(“, CM: “);
Serial.print(cmMsec);
Serial.print(“, IN: “);
Serial.println(inMsec);
delay(100);
}
[adsenseyu1]