-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ino
More file actions
141 lines (116 loc) · 3.54 KB
/
main.ino
File metadata and controls
141 lines (116 loc) · 3.54 KB
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
#include <FalconRobot.h>
#include <Wire.h>
FalconRobotMotors motors(5, 7, 6, 8);
FalconRobotDistanceSensor distanceSensor(2, 3);
FalconRobotLineSensor left(A2);
FalconRobotLineSensor right(A3);
#define DISTANCETHRESHOLD 8 //distância em cm limite do obstáculo
#define LINETHRESHOLD 800
#define SPEED 30
int leftSpeed; // variable used to store the leftMotor speed
int rightSpeed; // variable used to store the rightMotor speed
int distance; // variable to store the distance value
int leftValue;
int rightValue;
int speed;
/* GYROSCOPE STUFF */
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, "%6d", i);
return tmp_str;
}
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
empurraozada();
}
void loop(){
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, 7*2, true);
accelerometer_x = Wire.read()<<8 | Wire.read();
distance = distanceSensor.read();
leftValue = left.read();
rightValue = right.read();
/* TESTING SENSORS
Serial.print("leftV = "); Serial.print(leftValue);
Serial.print(" | rightV = "); Serial.print(rightValue);
Serial.print(" | dist = "); Serial.print(distance);
Serial.print(" | aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
*/
if(distance <= DISTANCETHRESHOLD && distance != 4 && distance != 0) {
motors.stop();
delay(500);
reverse();
turnRight();
motors.drive(100, FORWARD);
delay(200);
motors.stop();
turnLeft();
motors.drive(100, FORWARD);
delay(300);
motors.stop();
turnLeft();
motors.drive(100, FORWARD);
delay(200);
motors.stop();
turnRight();
empurraozada();
}
else{
if(accelerometer_x < 10000)
speed = 100;
else
speed = SPEED;
if((leftValue > LINETHRESHOLD) && (rightValue > LINETHRESHOLD)) {
leftSpeed = speed;
rightSpeed = speed;
}
else if(rightValue > LINETHRESHOLD) {
leftSpeed = speed;
rightSpeed = speed - 10;
}
else if(leftValue > LINETHRESHOLD) {
leftSpeed = speed - 10 ;
rightSpeed = speed;
}
motors.leftDrive(leftSpeed, FORWARD);
motors.rightDrive(rightSpeed, FORWARD);
}
}
void empurraozada(){
motors.drive(100, FORWARD);
delay(60);
motors.stop();
}
void reverse() {
motors.stop();
//delay(500);
motors.drive(50, BACKWARD);
delay(200);
motors.stop();
delay(500);
}
void turnRight() {
motors.leftDrive(50, FORWARD);
motors.rightDrive(50, BACKWARD);
delay(250);
motors.stop();
delay(1000);
}
void turnLeft() {
motors.leftDrive(50, BACKWARD);
motors.rightDrive(50, FORWARD);
delay(250);
motors.stop();
delay(1000);
}