-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathterriblySimpleRobot.ino
136 lines (111 loc) · 3.19 KB
/
terriblySimpleRobot.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
/******************************
Terribly simple robot
Based on Adafruit DCMotorTest - thanks Adafruit!
11 Sept 2017 - Michael Shiloh - Program creation
12 Sept 2017 - Michael Shiloh - Added comments explaining
what some of the functions do
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor * rightMotor = AFMS.getMotor(1);
Adafruit_DCMotor * leftMotor = AFMS.getMotor(2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
AFMS.begin(); // create with the default frequency 1.6KHz
// Try to do a square:
goForward();
delay (2000); // amount of time to go forward
stop(); // what it says
delay (250); // let motors actually stop
turnRight(300); // turn right for this much time
goForward();
delay (2000);
stop();
delay (250);
turnRight(300); // 300 is the argument
goForward();
delay (2000);
stop();
delay (250);
turnRight(300);
goForward();
delay (2000);
stop();
delay (250); // totally unnecessary
}
void loop() {
}
// Function to make the robot go forward
// This function does not stop the motor
// Note that this function takes no parameters
void goForward() {
Serial.println("goForward");
leftMotor->setSpeed(150);
leftMotor->run(FORWARD);
rightMotor->setSpeed(150);
rightMotor->run(FORWARD);
}
// stop both motors
void stop() {
Serial.println("stop");
leftMotor->run(RELEASE);
rightMotor->run(RELEASE);
}
// Function to make the robot turn right
// for a certain amount of time
// This function stops the motor once the
// time is up
// Note that this function takes one parameter,
// namely, the amount of time in millliseconds
// to turn
void turnRight(int amount) { // amount is the parameter
Serial.println("turnRight");
leftMotor->setSpeed(250);
leftMotor->run(FORWARD);
rightMotor->setSpeed(50);
rightMotor->run(BACKWARD);
delay (amount); // here we are using the parameter
stop();
delay (250);
}
void turnLeft() {
Serial.println("turnLeft");
leftMotor->setSpeed(250);
leftMotor->run(BACKWARD);
rightMotor->setSpeed(50);
rightMotor->run(FORWARD);
}
// I used this function at the begining to test the
// motors and confirm that they were turning in the
// right direction, but once I was done I didn't
// need this anymore; however I'm keeping it in here
// (a) as more examples and (b) in case I need to use
// it again
void testRobot() {
// do a little test:
// Go forward for a bit
leftMotor->setSpeed(150);
leftMotor->run(FORWARD);
rightMotor->setSpeed(150);
rightMotor->run(FORWARD);
delay (500);
// stop for a moment before changing direction
// to avoid stressing the motor
leftMotor->run(RELEASE);
rightMotor->run(RELEASE);
delay (250);
// Go backwards for a bit
leftMotor->setSpeed(150);
leftMotor->run(BACKWARD);
rightMotor->setSpeed(150);
rightMotor->run(BACKWARD);
delay (500);
// and stop.
// Note that the comment in the example is wrong:
// RELEASE turns the motor OFF
leftMotor->run(RELEASE);
rightMotor->run(RELEASE);
}