EasyManua.ls Logo

SunFounder GalaxyRVR - Page 52

Default Icon
120 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder GalaxyRVR Kit for Arduino, Release 1.0
(continued from previous page)
SoftPWMSet(in2, 0); // Stop
// Set the right motors rotate clockwise
SoftPWMSet(in3, 0); // Stop
SoftPWMSet(in4, 255); // Full speed
}
In this code, we’re speaking to our rover, telling it exactly what to do. With the SoftPWMSet() function, we’re acting
like the car’s accelerator and brakes, controlling the speed and direction of each motor. We tell the left motors to spin
counterclockwise and the right motors to spin clockwise, and just like that, our rover moves forward!
Absolutely, the concept of reversing the rover is straightforward once you understand how to move it forward. To make
the rover move backward, we just need to reverse the direction of rotation of each motor.
Here’s how we’d do that in code, we do just the opposite. The right wheels should now rotate counterclockwise, and
the left wheels should rotate clockwise.
#include <SoftPWM.h>
// Define the pins of motors
const int in1 = 2;
const int in2 = 3;
const int in3 = 4;
const int in4 = 5;
void setup() {
// Initialize SoftPWM
SoftPWMBegin();
}
void loop() {
// Set the left motors to rotate clockwise
SoftPWMSet(in1, 0); // Stop
SoftPWMSet(in2, 255); // Full speed
// Set the right motors to rotate counterclockwise
SoftPWMSet(in3, 255); // Full speed
SoftPWMSet(in4, 0); // Stop
}
In this code, we use SoftPWMSet() to tell the left motors to rotate clockwise and the right motors to rotate counter-
clockwise.
Isn’t it fascinating that we can control our rover’s journey simply with code? The next time you’re in a car, take a
moment to think about the journey of your rover, exploring the world one rotation at a time. Stay tuned, because our
rover’s journey is just beginning!
Step 3: Making the Rover Move in Other Directions
Now that we know how to move our Mars Rover forward and backward, what if we want it to turn left or right?
Just as in real life driving, there are two main ways for a car to turn left.
• The first way is by having the wheels on the left side rotate slower than the ones on the right. This difference in
48 Chapter 3. Course Mode

Related product manuals