-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiburones.ino
143 lines (109 loc) · 3.23 KB
/
tiburones.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
// ---------------------------------------------------------------- //
// Tiburones - Motorized Arduino Bot
// Created by - Team 17
// Using Arduino IDE 2.2.1
// Prototype (Test Code) v2
// ---------------------------------------------------------------- //
// for LCD display
#include <LiquidCrystal_I2C.h>
// initalization for display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// define variables for lcd display
char array1[] = "Robot State: ";
char array2[] = "STOPPED ";
char array3[] = "BACKWARD ";
char array4[] = "FORWARD ";
char array5[] = "OUT OF RANGE ";
// define echo and trig for ultrasonic sensor
int ePin = 8;
int tPin = 7;
// define motor control pins
#define IN1 4 // connect IN1 on the H-Bridge to Arduino pin 4
#define IN2 9 // connect IN2 on the H-Bridge to Arduino pin 5
// SWITCH AS NECESSARY
// define variables to update LCD tracks
int LCDprev = 99;
int LCDcurr = 100;
// define duration and distance variables
long duration;
int distance;
void setup() {
// initialize LCD and backlight
lcd.init();
lcd.backlight();
// set sonar digital pin mode
pinMode (tPin, OUTPUT);
pinMode (ePin, INPUT);
// initialize motors
pinMode (IN1, OUTPUT); // all L298N digital pins are outputs
pinMode (IN2, OUTPUT); // all L298N digital pins are outputs
// start debug communication
Serial.begin (9600);
// set initial cursor and print "Robot State:"
lcd.setCursor(0, 0);
lcd.print (array1);
}
void loop() {
/*
NOTE: it is important only to update the LCD screen when a change of state has occured
Otherwise, the program become inefficient. We only want to write to the LCD when
there is a change in what the robot is doing. The LCDprey and LCDcurr variables
help us to do this
*/
LCDprev = LCDcurr;
// receive distance measurement + delays, clears trig condition
digitalWrite (tPin, LOW);
delayMicroseconds (2);
digitalWrite (tPin, HIGH);
delayMicroseconds (10) ;
digitalWrite (tPin, LOW);
//receives pulse and duration info to convert to distance
duration = pulseIn (ePin, HIGH);
distance = duration * 0.034 / 2;
// displays the distance
Serial.print ("Distance: ");
Serial.print (distance);
Serial.println(" cm");
if (distance <= 25) {
LCDcurr = 1;
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
if (LCDprev != LCDcurr) {
lcd.setCursor(0, 1);
lcd.print(array3);
delay(10);
}
} else if (distance <= 50) {
LCDcurr = 2;
// Turn left 90 degrees
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
delay(500); // Adjust the delay based on the time it takes for the robot to turn 90 degrees
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
if (LCDprev != LCDcurr) {
lcd.setCursor(0, 1);
lcd.print(array2);
delay(10);
}
} else if (distance <= 75) {
LCDcurr = 3;
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
if (LCDprev != LCDcurr) {
lcd.setCursor(0, 1);
lcd.print(array4);
delay(10);
}
} else {
LCDcurr = 4;
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
if (LCDprev != LCDcurr) {
lcd.setCursor(0, 1);
lcd.print(array5);
delay(10);
}
}
delay(10);
}