-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.ino
75 lines (59 loc) · 1.5 KB
/
Main.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
#include "Ultrasonic.h"
#include <U8x8lib.h>
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(U8X8_PIN_NONE);
bool isInReverse = false;
Ultrasonic ultrasonic(7);
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
Serial.begin(9600);
u8x8.begin();
u8x8.setFont(u8x8_font_5x8_r);
}
void loop()
{
if(digitalRead(6) == HIGH){
if(isInReverse){
isInReverse = false;
u8x8.clear();
u8x8.setCursor(0,0);
u8x8.print("Current gear: D");
} else {
isInReverse = true;
u8x8.clear();
u8x8.setCursor(0,0);
u8x8.print("Current gear: R");
}
}
long RangeInCentimeters;
RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
Serial.print(RangeInCentimeters);//0~400cm
Serial.println(" cm");
if(isInReverse){
if(RangeInCentimeters < 15 && RangeInCentimeters > 10){ //15CM of minder
delay(200);
digitalWrite(4, HIGH);
analogWrite(5, 127);
delay(200);
digitalWrite(4, LOW);
analogWrite(5, 0);
}
else if(RangeInCentimeters < 10 && RangeInCentimeters > 5) {
delay(100);
digitalWrite(4, HIGH);
analogWrite(5, 127);
delay(100);
digitalWrite(4, LOW);
analogWrite(5, 0);
} else if(RangeInCentimeters < 5){
digitalWrite(4, HIGH);
analogWrite(5, 127);
}
else {
digitalWrite(4, LOW);
analogWrite(5, 0);
}
}
}