This is an Arduino Code used to move a small robot forward. The arduino is conne
ID: 3847846 • Letter: T
Question
This is an Arduino Code used to move a small robot forward. The arduino is connected to a Servo and Stepper motors that move the robot. Please provide an explanation of the code and how it achieves this motion.
// CONSTANTS
#include <Stepper.h>
#include <Servo.h>
const int stepsPerRevolution = 200;
const float DARK_THRESHOLD = 500;
Stepper leftStepper(stepsPerRevolution, 7, 8, 9, 10);
Stepper rightStepper(stepsPerRevolution, 3, 4, 5, 6);
int pos =255;
Servo myServo;
int amount = 1;
int powerpinLeftUltra = 50;
int powerpinRightUltra = 51;
const int trigPinLeft = 33;
const int echoPinLeft = 30;
const int trigPinRight = 32;
const int echoPinRight = 31;
int switch1 = 39;
int switch2 = 41;
int distanceRight = 0;
int distanceLeft = 0;
int durationLeft = 0;
int durationRight = 0;
// VARIABLES
long duration;
void setup()
{ pinMode(trigPinLeft, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinLeft, INPUT); // Sets the echoPin as an Input
pinMode(trigPinRight, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinRight, INPUT); // Sets the echoPin as an Input
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(powerpinLeftUltra, OUTPUT);
pinMode(powerpinRightUltra, OUTPUT);
const int LIGHT_PIN = A0; // Pin connected to voltage divider output
const float VCC = 5.0;
const float R_DIV = 10000;
int spud = 60;
int first = 0;
int stepCount = 0;
int readPin = 3;
leftStepper.setSpeed(spud);
rightStepper.setSpeed(spud);
pinMode(readPin, INPUT);
myServo.attach(11);
delay(10);
}
void loop()
{
while (true)
{
leftStepper.step(amount);
rightStepper.step(amount);}
Explanation / Answer
Answer:
In a robot, a servo motor at a joint is used to actuate the movements, while a stepper motor is used to produce the movement by a given amount. In the given code, included header files Servo.h and Stepper.h provide functions corresponding the functions (tasks) of respective motors. Intitally, apart from creating required constant values, objects leftStepper and rightStepper of type Stepper are created corresponding to left and right foot of robot.
Setup() function in specifies instructions to configure both the motors as per the movement requirements of robot. pinMode() function in Setup() function configures various pins to be either as INPUT or OUTPUT pins. Calls to setSpeed() function set the speed by which leftStepper and rightStepper will move. attach() function attaches a function to a pin.
After Setup() function, loop() will be used to produce robot movements with leftStepper first, then rightStepper.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.