File tree 2 files changed +57
-5
lines changed
2 files changed +57
-5
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Ultrasonic sensor Pins:
3
+ VCC: +5VDC
4
+ Trig : Trigger (INPUT) - Pin11
5
+ Echo: Echo (OUTPUT) - Pin 12
6
+ GND: GND
7
+ */
8
+
9
+ #define trigPin 11
10
+ #define echoPin 12
11
+
12
+ long duration;
13
+ int cm;
14
+
15
+ void setup () {
16
+ // Serial Port begin
17
+ Serial.begin (9600 );
18
+ // Define inputs and outputs
19
+ pinMode (trigPin, OUTPUT);
20
+ pinMode (echoPin, INPUT);
21
+ pinMode (LED_BUILTIN, OUTPUT);
22
+ digitalWrite (LED_BUILTIN, LOW);
23
+ }
24
+
25
+
26
+ void measureDistance (){
27
+ // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
28
+ // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
29
+ digitalWrite (trigPin, LOW);
30
+ delayMicroseconds (5 );
31
+ digitalWrite (trigPin, HIGH);
32
+ delayMicroseconds (10 );
33
+ digitalWrite (trigPin, LOW);
34
+
35
+ // Read the signal from the sensor: a HIGH pulse whose
36
+ // duration is the time (in microseconds) from the sending
37
+ // of the ping to the reception of its echo off of an object.
38
+ duration = pulseIn (echoPin, HIGH);
39
+
40
+ // Convert the time into a distance
41
+ cm = (duration/2 ) / 29.1 ; // Divide by 29.1 or multiply by 0.0343
42
+ Serial.print (cm);
43
+ Serial.println ();
44
+ }
45
+
46
+ void loop () {
47
+ measureDistance ();
48
+ delay (50 );
49
+ }
Original file line number Diff line number Diff line change 1
- #include < unistd.h> // Just for sleep()
1
+ #include < chrono>
2
+ #include < thread>
2
3
3
4
#include < iostream>
4
5
#include < string>
5
6
6
7
#include " arduinoSerial.h"
7
8
8
9
int main (){
9
- arduinoSerial serial (" /dev/ttyACM0" , true );
10
+ arduinoSerial serial (" /dev/ttyACM0" , false );
10
11
serial.begin (B9600);
11
- // ParseFloat test
12
12
while (true ){
13
- std::cout << serial.parseFloat () << std::endl;
14
- sleep (1 );
13
+ if (serial.available ()){
14
+ std::this_thread::sleep_for (std::chrono::milliseconds (10 )); // Wait for the data to come in, otherwise it will be incomplete
15
+ std::cout << serial.parseFloat () << std::endl;
16
+ serial.flush ();
17
+ }
15
18
}
16
19
return 0 ;
17
20
}
You can’t perform that action at this time.
0 commit comments