@@ -14,15 +14,18 @@ View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/s
1414Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
1515*/
1616
17- // the left motor will be controlled by the motor B pins on the motor driver
18- const int PWMB = 13 ; // speed control pin on the motor driver for the left motor
19- const int BIN2 = 12 ; // control pin 2 on the motor driver for the left motor
20- const int BIN1 = 11 ; // control pin 1 on the motor driver for the left motor
2117
2218// the right motor will be controlled by the motor A pins on the motor driver
23- const int AIN1 = 10 ; // control pin 1 on the motor driver for the right motor
24- const int AIN2 = 9 ; // control pin 2 on the motor driver for the right motor
25- const int PWMA = 8 ; // speed control pin on the motor driver for the right motor
19+ const int AIN1 = 13 ; // control pin 1 on the motor driver for the right motor
20+ const int AIN2 = 12 ; // control pin 2 on the motor driver for the right motor
21+ const int PWMA = 11 ; // speed control pin on the motor driver for the right motor
22+
23+ // the left motor will be controlled by the motor B pins on the motor driver
24+ const int PWMB = 10 ; // speed control pin on the motor driver for the left motor
25+ const int BIN2 = 9 ; // control pin 2 on the motor driver for the left motor
26+ const int BIN1 = 8 ; // control pin 1 on the motor driver for the left motor
27+
28+ int switchPin = 7 ; // switch to turn the robot on and off
2629
2730const int driveTime = 20 ; // this is the number of milliseconds that it takes the robot to drive 1 inch
2831 // it is set so that if you tell the robot to drive forward 25 units, the robot drives about 25 inches
@@ -40,6 +43,8 @@ String distance; //the distance to travel in each direction
4043/* *******************************************************************************/
4144void setup ()
4245{
46+ pinMode (switchPin, INPUT_PULLUP); // set this as a pullup to sense whether the switch is flipped
47+
4348 // set the motor contro pins as outputs
4449 pinMode (AIN1, OUTPUT);
4550 pinMode (AIN2, OUTPUT);
@@ -48,7 +53,7 @@ void setup()
4853 pinMode (BIN1, OUTPUT);
4954 pinMode (BIN2, OUTPUT);
5055 pinMode (PWMB, OUTPUT);
51-
56+
5257 Serial.begin (9600 ); // begin serial communication with the computer
5358
5459 // prompt the user to enter a command
@@ -60,49 +65,57 @@ void setup()
6065/* *******************************************************************************/
6166void loop ()
6267{
63- if (Serial.available () > 0 ) // if the user has sent a command to the RedBoard
64- {
65- botDirection = Serial.readStringUntil (' ' ); // read the characters in the command until you reach the first space
66- distance = Serial.readStringUntil (' ' ); // read the characters in the command until yuo reach the second space
67-
68- // print the command that was just received in the serial monitor
69- Serial.print (botDirection);
70- Serial.print (" " );
71- Serial.println (distance.toInt ());
72-
73- if (botDirection == " f" ) // if the entered direction is forward
68+ if (digitalRead (7 ) == LOW)
69+ { // if the switch is in the ON position
70+ if (Serial.available () > 0 ) // if the user has sent a command to the RedBoard
7471 {
75- rightMotor (200 ); // drive the right wheel forward
76- leftMotor (200 ); // drive the left wheel forward
77- delay (driveTime * distance.toInt ()); // drive the motors long enough travel the entered distance
78- rightMotor (0 ); // turn the right motor off
79- leftMotor (0 ); // turn the left motor off
80- }
81- else if (botDirection == " b" ) // if the entered direction is backward
82- {
83- rightMotor (-200 ); // drive the right wheel forward
84- leftMotor (-200 ); // drive the left wheel forward
85- delay (driveTime * distance.toInt ()); // drive the motors long enough travel the entered distance
86- rightMotor (0 ); // turn the right motor off
87- leftMotor (0 ); // turn the left motor off
88- }
89- else if (botDirection == " l" ) // if the entered direction is right
90- {
91- rightMotor (-200 ); // drive the right wheel forward
92- leftMotor (255 ); // drive the left wheel forward
93- delay (turnTime * distance.toInt ()); // drive the motors long enough turn the entered distance
94- rightMotor (0 ); // turn the right motor off
95- leftMotor (0 ); // turn the left motor off
96- }
97- else if (botDirection == " r" ) // if the entered direction is left
98- {
99- rightMotor (255 ); // drive the right wheel forward
100- leftMotor (-200 ); // drive the left wheel forward
101- delay (turnTime * distance.toInt ()); // drive the motors long enough turn the entered distance
102- rightMotor (0 ); // turn the right motor off
103- leftMotor (0 ); // turn the left motor off
72+ botDirection = Serial.readStringUntil (' ' ); // read the characters in the command until you reach the first space
73+ distance = Serial.readStringUntil (' ' ); // read the characters in the command until yuo reach the second space
74+
75+ // print the command that was just received in the serial monitor
76+ Serial.print (botDirection);
77+ Serial.print (" " );
78+ Serial.println (distance.toInt ());
79+
80+ if (botDirection == " f" ) // if the entered direction is forward
81+ {
82+ rightMotor (200 ); // drive the right wheel forward
83+ leftMotor (200 ); // drive the left wheel forward
84+ delay (driveTime * distance.toInt ()); // drive the motors long enough travel the entered distance
85+ rightMotor (0 ); // turn the right motor off
86+ leftMotor (0 ); // turn the left motor off
87+ }
88+ else if (botDirection == " b" ) // if the entered direction is backward
89+ {
90+ rightMotor (-200 ); // drive the right wheel forward
91+ leftMotor (-200 ); // drive the left wheel forward
92+ delay (driveTime * distance.toInt ()); // drive the motors long enough travel the entered distance
93+ rightMotor (0 ); // turn the right motor off
94+ leftMotor (0 ); // turn the left motor off
95+ }
96+ else if (botDirection == " r" ) // if the entered direction is right
97+ {
98+ rightMotor (-200 ); // drive the right wheel forward
99+ leftMotor (255 ); // drive the left wheel forward
100+ delay (turnTime * distance.toInt ()); // drive the motors long enough turn the entered distance
101+ rightMotor (0 ); // turn the right motor off
102+ leftMotor (0 ); // turn the left motor off
103+ }
104+ else if (botDirection == " l" ) // if the entered direction is left
105+ {
106+ rightMotor (255 ); // drive the right wheel forward
107+ leftMotor (-200 ); // drive the left wheel forward
108+ delay (turnTime * distance.toInt ()); // drive the motors long enough turn the entered distance
109+ rightMotor (0 ); // turn the right motor off
110+ leftMotor (0 ); // turn the left motor off
111+ }
104112 }
105113 }
114+ else
115+ {
116+ rightMotor (0 ); // turn the right motor off
117+ leftMotor (0 ); // turn the left motor off
118+ }
106119}
107120/* *******************************************************************************/
108121void rightMotor (int motorSpeed) // function for driving the right motor
0 commit comments