a77b88c5 |
1 | /* |
2 | This program is free software: you can redistribute it and/or modify |
3 | it under the terms of the GNU Affero General Public License as |
4 | published by the Free Software Foundation, either version 3 of the |
5 | License, or (at your option) any later version. |
6 | |
7 | This program is distributed in the hope that it will be useful, |
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | GNU Affero General Public License for more details. |
11 | |
12 | You should have received a copy of the GNU Affero General Public License |
13 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
14 | */ |
15 | /* |
16 | Motor. |
17 | Runs both motors back and forth. |
18 | |
19 | This example code is in the public domain. |
20 | */ |
21 | |
22 | // Pin 13 has an LED connected on most Arduino boards. |
23 | // give it a name: |
24 | int led = 13; |
25 | int motor1_enable = 10; |
26 | int motor1a = 9; |
27 | int motor1b = 8; |
28 | int motor2_enable = 3; |
29 | int motor2a = 4; |
30 | int motor2b = 5; |
31 | |
32 | // the setup routine runs once when you press reset: |
33 | void setup() { |
34 | // initialize the digital pin as an output. |
35 | pinMode(led, OUTPUT); |
36 | pinMode(motor1_enable, OUTPUT); |
37 | pinMode(motor1a, OUTPUT); |
38 | pinMode(motor1b, OUTPUT); |
39 | pinMode(motor2_enable, OUTPUT); |
40 | pinMode(motor2a, OUTPUT); |
41 | pinMode(motor2b, OUTPUT); |
42 | |
43 | digitalWrite(led, HIGH); |
44 | digitalWrite(motor1_enable, LOW); |
45 | digitalWrite(motor1a, LOW); |
46 | digitalWrite(motor1b, LOW); |
47 | digitalWrite(motor2_enable, LOW); |
48 | digitalWrite(motor2a, LOW); |
49 | digitalWrite(motor2b, LOW); |
50 | } |
51 | |
52 | void motorsRun(int left, int right, int ms_delay) { |
53 | // Set left motor direction: |
54 | if (left > 0) { |
55 | // Set left motor to go forward: |
56 | digitalWrite(motor1a, HIGH); |
57 | digitalWrite(motor1b, LOW); |
58 | } else { |
59 | // Set left motor to go backward: |
60 | digitalWrite(motor1a, LOW); |
61 | digitalWrite(motor1b, HIGH); |
62 | left = -left; // Make left a positive value: |
63 | } |
64 | |
65 | analogWrite(motor2_enable, left); // Start motor in right direction |
66 | // Set left motor direction: |
67 | if (right > 0) { |
68 | // Set right motor to go forward: |
69 | digitalWrite(motor2a, HIGH); |
70 | digitalWrite(motor2b, LOW); |
71 | } else { |
72 | // Set right motor to go backward: |
73 | digitalWrite(motor2a, LOW); |
74 | digitalWrite(motor2b, HIGH); |
75 | right = -right; // Make right a positive value: |
76 | } |
77 | analogWrite(motor1_enable, left); // Start motor in right direction |
78 | |
79 | delay(ms_delay); // Wait the specified amount of time |
80 | |
81 | // Stop both motors: |
82 | analogWrite(motor1_enable, 0); |
83 | analogWrite(motor2_enable, 0); |
84 | } |
85 | |
86 | // the loop routine runs over and over again forever: |
87 | void loop() { |
88 | |
89 | digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) |
90 | motorsRun(100, 100, 2000);// Run the robot forward |
91 | digitalWrite(led, LOW); // turn the LED off by making the voltage LOW |
92 | motorsRun(-100, -100, 2000); // Run the robot backward |
93 | } |