-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.ino
130 lines (112 loc) · 2.43 KB
/
robot.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
/*
PIN DEFINITIONS
---------------
pwmPinM1 = D3
sensePinM1 = D5
sense2PinM1 = D6
pwmPinM2 = D11
sensePinM2 = D7
sense2PinM2 = D8
PING_PIN = D2
ECHO_PIN = D9
PING_PIN2 = D4
ECHO_PIN2 = D10
TONE_PIN = D12
PING_PIN_F = A2 (D16)
A0 & A1(D14, D15) = RX, TX bluetooth
LED_PIN = A3 (D17)
*/
//#define DEBUG_M
//#define SERVOS
//#define I2C_T
#define RVER 1
#include <DualMotor.h>
//#include <SoftwareSerial.h>
#include <NewPing.h>
//#include <SoftTimer.h>
//#include <Heartbeat.h>
#ifdef I2C_T
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Wire.h>
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#define OUTPUT_READABLE_ACCELGYRO
#endif
#ifdef SERVOS
//#include <Servo.h>
#endif
//#define PING_PIN_F 16
#define PING_PIN_L 2 // Arduino pin tied to trigger ping on the ultrasonic sensor.
#define ECHO_PIN_L 9
#define PING_PIN_R 4 // Arduino pin tied to trigger ping on the ultrasonic sensor.
#define ECHO_PIN_R 10
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400cm.
//#define LED_PIN 17
#define TONE_PIN 12
//MOTORS && CONTROL STUFF
#define MAX_SPEED 115
#define OFFSET_SPEED 5
#define OBSTACLE 30
#define STUCK 10
byte speed = MAX_SPEED;
//NewPing sonar_F(PING_PIN_F, PING_PIN_F, MAX_DISTANCE);
NewPing sonar_L(PING_PIN_L, ECHO_PIN_L, MAX_DISTANCE);
NewPing sonar_R(PING_PIN_R, ECHO_PIN_R, MAX_DISTANCE);
//SoftwareSerial BT(14, 15); //A0 y A1 // RX, TX
DualMotor robotmotor;
//Task task1(100, control);
//Heartbeat heartbeat(LED_PIN);
#ifdef SERVOS
Servo myservo;
Servo myservo2;
byte pos = 90;
byte pos2 = 90;
bool servos = false;
#endif
#ifdef I2C_T
MPU6050 mpu;
#endif
bool automove = false;
bool firstrun = true;
bool follow = false;
void setup()
{
//~30Hz on pwm pins 3 & 11
// - minimize noise
// - minimize heat (saturation of motor --> L293=RIP)
TCCR2B = TCCR2B & B11111000 | B00000111;
pinMode(LED_BUILTIN, OUTPUT);
#ifdef DEBUG_M
//Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
#endif
Serial.begin(115200);
#ifdef SERVOS
myservo.attach(9);
myservo2.attach(10);
myservo.write(pos);
myservo2.write(pos2);
#endif
#ifdef I2C_T
mpu.initialize();
mpu.testConnection();
#endif
stop(1);
//SoftTimer.add(&task1);
beep(1);
}
void loop()
{
if (Serial.available() > 0)
{
BlueControl();
}
else if (automove)
{
SonarControl();
}
else if (follow && !automove)
{
Follow();
}
}