-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrove.ino
151 lines (140 loc) · 3.36 KB
/
grove.ino
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#define TRG 8
#define ECH 11
#include <LiquidCrystal.h>
LiquidCrystal lcd = LiquidCrystal(2, 3, 4, 5, 6, 7);
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
bool trg = true; // initiale value for the timer
int m1 = 0, m2 = 0, s1 = 0, s2 = 0;
int toogle = 1;
bool mode_1 = true;
bool mode_2 = false;
bool mode_3 = false;
void display_timer() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("time: ");
lcd.print(m1);
lcd.print(m2);
lcd.print(":");
lcd.print(s2);
lcd.print(s1);
}
void run_sensor() {
int counter = 1 ;
// Clears the trigPin condition
digitalWrite(TRG, LOW);
delayMicroseconds(2);
//------------------------------
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(TRG, HIGH);
delayMicroseconds(10);
digitalWrite(TRG, LOW);
//-------------------------------
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(ECH, HIGH);
//-------------------------------
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
//-------------------------------
// Displays the distance and the state of timer on the Serial Monitor [for debugging purpos]
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
//--------------------------------
if (distance > 70 && mode_1 == true) {
while (counter != 3 && trg == true) {
Serial.print("counter: ");
Serial.println(counter);
duration = pulseIn(ECH, HIGH);
distance = duration * 0.034 / 2;
Serial.println("increamenting");
counter++;
s1++;
timer_func();
display_timer();
delay(1000);
}
if (counter == 3) {
Serial.print("switchin trigger");
trg = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("timer paused");
delay(2000);
}
}
}
void timer_func() {
// first digit
if (m2 > 9) {
m1++;
m2 = 0;
}
//second digit
if (s2 == 5 && s1 > 9) {
m2++;
s2 = 0;
s1 = 0;
}
// third digit
if (s1 > 9) {
s2++;
s1 = 0;
}
if (s1 % 2 == 0) {
toogle = 0;
} else {
toogle = 1;
}
}
void select_mode() {
// push buttons
}
void setup() {
lcd.begin(16, 2);
lcd.clear();
//--- setting up sensor---
pinMode(TRG, OUTPUT);
pinMode(ECH, INPUT);
//------------------------
Serial.begin(9600);
lcd.setCursor(0, 0);
lcd.print("select mode");
while (mode_1 == false && mode_2 == false && mode_3 == false) {
select_mode();
}
}
// case check if distance is > 100cm && pause
void loop() {
while (s1 <= 10 && trg == true) {
s1++;
run_sensor();
timer_func();
delay(1000);
display_timer();
}
if (trg == false && mode_1 == true) {
run_sensor();
lcd.setCursor(1, 1);
if (distance < 70 && mode_1 == true) {
int c = 0;
while (distance < 70) {
c++;
delay(1000);
Serial.println(c);
if (c < 3) {
run_sensor();
}
if (c == 3) {
trg = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("continue");
delay(2000);
break;
}
}
}
}
}