Tasked to build a robot, using a Mega 2560 Arduino board, that went forwards, backwards, turned left, turned right, stopped, and made an led blink after the program has run its course. I came up with the specimen in the gif above. A total of about 3 hours was spent wiring, coding, and debuging.
Wiring:
This photo is a tad misleading but what was done was the black wire goes to the ground buss of the breadboard while the red wire goes to the hot buss of the bread board. One of the blue wires (PWM 2) goes to the positive end of the LED (to allow for programming/voltage control) while the other two (PWM 12 for the left servo and PWM 13 for the right servo) go to the white leads of the servos (one per servo). The ground leads for the servos are in the same row and then jumped to the ground buss of the breadboard to allow for wiring simplicity. This mentality also applies to the hot leads of the servos.
This photo is also a bit misleading but the blue wire which seems to disappear underneath the battery is connected to one of the ends of the resistor. The resistor then spans to the positive end of the green LED (programmed to flash 5 times at the end of the program, noted it only does 4 times in the gif) which is hooked to the ground buss of the breadboard.
Code:
#include <Servo.h>
Servo rightServo;
Servo leftServo;
int pos = 180; //sets a turn value
int led = 2; //initializes led variable to make it assignment easier
int counter = 0; //initializes an integer for the ledBlink counter loop
void forward() //this function makes the robot go forward
{
rightServo.write(-pos);
leftServo.write(pos);
}
void backward() //this function makes the robot go backward
{
rightServo.write(pos);
leftServo.write(-pos);
}
void leftTurn() //this function makes the robot turn left
{
rightServo.write(-pos);
leftServo.write(-pos);
}
void rightTurn() //this function makes the robot turn right
{ 98
rightServo.write(pos);
leftServo.write(pos);
}
void stopRobot() //this function stops the robot
{
rightServo.write(90);//90 is the servo stop point
leftServo.write(90);
}
void ledBlink() //this function makes the led blink 5 times
{
while(counter != 5)
{
counter = counter + 1;
digitalWrite(led, HIGH); //turns led on
delay(500);
digitalWrite(led, LOW);//turns led off
delay(500);
}
}
void setup()
{
rightServo.attach(13);
leftServo.attach(12);
pinMode (led, OUTPUT);// this sets pin 2 (as assigned via led = 2) as an output pin
forward();
delay(1500);
stopRobot();
delay(1500);
leftTurn();
delay(1500);
stopRobot();
delay(1500);
rightTurn();
delay(1500);
stopRobot();
delay(1500);
backward();
delay(1500);
stopRobot();
delay(1500);
ledBlink();
}
void loop() //nothing here because we don't want it to loop
{
}
Issues:
It was quite difficult trying to get the thing to move properly. I found often during the troubleshooting phase that having a negative (pos) value would stop the servo's movement altogether. It wasn't until I looped a run-stop program in the void loop() and was able to physically configure the servos to move during the run section and stop during the stop sections that the robot then began to perform as initially intended. Also, I had absolutely no clue how to do an LED pulse so I needed to use reference materials for that, the source for which I cannot find any longer (will make a note to keep track of sources for future use/reference of others). Mounting all the necessary parts was also a pain, but I have found my current configuration is the best for now.
No comments:
Post a Comment