Ultrasonic Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
/* Ultrasonic 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]