-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
92 lines (79 loc) · 3.37 KB
/
main.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <Arduino.h>
#include <LiquidCrystal.h>
extern HardwareSerial Serial;
#define TRIGGER_PIN 26 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN 27 // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
unsigned long pingTime;
unsigned long lastPing=0;
unsigned long maxPingTime = MAX_DISTANCE * 58;
void setup() {
Serial.begin(115200);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
lcd.begin(16, 2); // start the library
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Booting up...."); // print a simple message
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Dist: ");
}
void loop() {
while (digitalRead(ECHO_PIN))
{
// Serial.println("***OOPS*** ECHO_PIN is HIGH");
// digitalWrite(TRIGGER_PIN, HIGH);
// delayMicroseconds(10);
// digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(50);
}
//delay(100);
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(4); // Wait for pin to go low.
digitalWrite(TRIGGER_PIN, HIGH); // Set trigger pin high, this tells the sensor to send out a ping.
delayMicroseconds(10); // Wait long enough for the sensor to realize the trigger pin is high. Sensor specs say to wait 10uS.
digitalWrite(TRIGGER_PIN, LOW);
// if (digitalRead(ECHO_PIN))
// {
// Serial.println("***OOPS*** ECHO_PIN is HIGH after trigger");
//// digitalWrite(TRIGGER_PIN, HIGH);
//// delayMicroseconds(10);
//// digitalWrite(TRIGGER_PIN, LOW);
// //delay(50);
// }
pingTime = pulseIn(ECHO_PIN, HIGH, maxPingTime);
if (pingTime != lastPing)
{
lcd.setCursor(6,0);
lcd.print(" ");
lcd.setCursor(6,0);
lcd.print(pingTime/148); // display seconds elapsed since power-up
lastPing = pingTime;
}
// Serial.print("Dist: ");
// Serial.println(pingTime/148);
// if (pingTime == 0)
// {
// lcd.setCursor(0,1);
// lcd.print("Waiting...");
// delay(189);
// lcd.setCursor(0,1);
// lcd.print(" ");
// }else delay(50);
}
//boolean ping_trigger() {
// digitalWrite(TRIGGER_PIN, LOW); // Set the trigger pin low, should already be low, but this will make sure it is.
// delayMicroseconds(4); // Wait for pin to go low.
// digitalWrite(TRIGGER_PIN, HIGH); // Set trigger pin high, this tells the sensor to send out a ping.
// delayMicroseconds(10); // Wait long enough for the sensor to realize the trigger pin is high. Sensor specs say to wait 10uS.
// digitalWrite(TRIGGER_PIN, LOW); // Set trigger pin back to low.
//
// if (digitalRead(ECHO_PIN)) return false; // Previous ping hasn't finished, abort.
// _max_time = micros() + _maxEchoTime + MAX_SENSOR_DELAY; // Maximum time we'll wait for ping to start (most sensors are <450uS, the SRF06 can take up to 34,300uS!)
// while (!digitalRead(ECHO_PIN)) // Wait for ping to start.
// if (micros() > _max_time) return false; // Took too long to start, abort.
//}