-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALRo.ino
131 lines (113 loc) · 2.81 KB
/
ALRo.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
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
#define US1_TRIGGER 19
#define US1_ECHO 17
#define US2_TRIGGER 18
#define US2_ECHO 16
#define M1 4
#define M2 23
#define LED_1 25
#define LED_2 26
#define S_WIDTH 128
#define S_HEIGHT 64
int M1_STATE;
int M2_STATE;
long TIME1;
int DIST_CM1;
long TIME2;
int DIST_CM2;
Adafruit_SSD1306 display(S_WIDTH, S_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
// Ultrasonic Sensor
pinMode(US1_TRIGGER, OUTPUT);
pinMode(US1_ECHO, INPUT);
pinMode(US2_TRIGGER, OUTPUT);
pinMode(US2_ECHO, INPUT);
// Motors
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
// LEDs
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
// OLED Display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for(;;);
}
display.setFont(&FreeSerif9pt7b);
}
void loop() {
// Ultrasonic Sensor
DIST_CM1 = measureDist1();
DIST_CM2 = measureDist2();
// Serial Monitor
printDist(DIST_CM1, DIST_CM2);
// Motors + LEDs
if (DIST_CM1 > 35 && DIST_CM2 < 35) {
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, LOW);
}
if (DIST_CM2 > 35 && DIST_CM1 < 35) {
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, HIGH);
}
// OLED Display
displayDist(DIST_CM1, DIST_CM2);
}
// Measure Distance
int measureDist1(){
// Clearing the trigger
digitalWrite(US1_TRIGGER, LOW);
delayMicroseconds(2);
// Setting the trigger on HIGH state for 10 usec
digitalWrite(US1_TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(US1_TRIGGER, LOW);
// Reading the echo, returns the sound wave travel time in usec
TIME1 = pulseIn(US1_ECHO, HIGH);
// Calculating the distance
DIST_CM1 = TIME1*0.034/2;
return DIST_CM1;
}
int measureDist2(){
// Clearing the trigger
digitalWrite(US2_TRIGGER, LOW);
delayMicroseconds(2);
// Setting the trigger on HIGH state for 10 usec
digitalWrite(US2_TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(US2_TRIGGER, LOW);
// Reading the echo, returns the sound wave travel time in usec
TIME2 = pulseIn(US2_ECHO, HIGH);
// Calculating the distance
DIST_CM2 = TIME2*0.034/2;
return DIST_CM2;
}
// Print Distance
void printDist(int a, int b){
// Printing the distance on the Serial Monitor
Serial.print("Dist_1: ");
Serial.println(a);
Serial.print("Dist_2: ");
Serial.println(b);
}
// Display Distance
void displayDist(int a, int b){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(30, 15);
display.println("Distancia:");
display.setCursor(30, 40);
display.println(a);
display.setCursor(30, 60);
display.println(b);
display.display();
}