From 4897269a97a708812520def98efa473891cd84b6 Mon Sep 17 00:00:00 2001 From: AceVest Date: Tue, 2 Sep 2014 22:33:07 +0800 Subject: [PATCH] add arduino Ultrasonic Sensor Code --- arduino/UltrasonicSensor/UltrasonicSensor.ino | 27 +++++++++++++++++++ tools/htn.c | 17 ++++++++++++ tools/nth.cc | 23 ++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 arduino/UltrasonicSensor/UltrasonicSensor.ino create mode 100644 tools/htn.c create mode 100644 tools/nth.cc diff --git a/arduino/UltrasonicSensor/UltrasonicSensor.ino b/arduino/UltrasonicSensor/UltrasonicSensor.ino new file mode 100644 index 0000000..4462e41 --- /dev/null +++ b/arduino/UltrasonicSensor/UltrasonicSensor.ino @@ -0,0 +1,27 @@ +const int TrigPin = 2; // 2 +const int EchoPin = 3; // ~3 + +float dist; + +void setup() +{ + Serial.begin(9600); + pinMode(TrigPin, OUTPUT); + pinMode(EchoPin, INPUT); + Serial.println("Ultrasonic Sensor:"); +} + +void loop() +{ + digitalWrite(TrigPin, LOW); + delayMicroseconds(2); + digitalWrite(TrigPin, HIGH); + delayMicroseconds(10); + digitalWrite(TrigPin, LOW); + + dist = pulseIn(EchoPin, HIGH) / 58.0; + + Serial.print(dist); + Serial.println("cm"); + delay(100); +} diff --git a/tools/htn.c b/tools/htn.c new file mode 100644 index 0000000..256f393 --- /dev/null +++ b/tools/htn.c @@ -0,0 +1,17 @@ +/* + * ------------------------------------------------------------------------ + * File Name: htn.c + * Author: Zhao Yanbai + * Sat Jul 12 17:00:31 2014 + * Description: none + * ------------------------------------------------------------------------ + */ +#include + +int main(int argc, char *argv[]){ + unsigned int ch; + while(scanf("%x",&ch) != EOF) + printf("%u\n",ch); + + return 0; +} diff --git a/tools/nth.cc b/tools/nth.cc new file mode 100644 index 0000000..aada7fc --- /dev/null +++ b/tools/nth.cc @@ -0,0 +1,23 @@ +/* + * ------------------------------------------------------------------------ + * File Name: nth.cc + * Author: Zhao Yanbai + * Sat Jul 12 16:20:37 2014 + * Description: none + * ------------------------------------------------------------------------ + */ +#include +#include +using namespace std; + +int main(int argc, char *argv[]) { + + unsigned int n; + + while(cin >> n) + { + printf("%08x\n", n); + } + + return 0; +} -- 2.44.0