Continuing from last class, I attached the "robot arms" to a base using duck tape and screws. I decided to add LED to the set up and added to code for LED. So when one of the arm moves to the right (the control is up/down on the joystick), the LED would turn off. I kind of struggle to put down the correct syntax, but in the end I managed to get right arm coordinated with the LED (bottom in the picture on the left). I'd to connect the set up to the battery because there wasn't enough power to make smooth arm movements. I thought I was going to put this on an automatic robot, but Mr. Robert pointed out that it might not be possible with this set up because the joystick is connected to the breadboard and the jumper wires link the breadboard with the servos. |
// letsarduino.com
// [Project 11] - 2 Servos Using a Joystick
// (thumbstick) + Arduino
#include <Servo.h>
int ServoHorizontalPin = 3;
int ServoVerticalPin = 4;
int HorizontalPotPin = A0;
int VerticalPotPin = A1;
int ServoH_Min = 0;
int ServoH_Max = 180;
int ServoV_Min = 0;
int ServoV_Max = 180;
int ledPin = 13;
Servo HorizontalServo;
Servo VerticalServo;
int HorizontalPotValue;
int HorizontalServoPosition;
int VerticalPotValue;
int VerticalServoPosition;
const int threshold = 10;
void setup()
{
HorizontalServo.attach(ServoHorizontalPin);
VerticalServo.attach(ServoVerticalPin);
}
void loop()
{
if (HorizontalServoPosition > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
delay(1);
}
HorizontalPotValue = analogRead(HorizontalPotPin);
VerticalPotValue = analogRead(VerticalPotPin);
HorizontalServoPosition = map(HorizontalPotValue, 0, 1023, ServoH_Min , ServoH_Max);
VerticalServoPosition = map(VerticalPotValue, 0, 1023, ServoH_Min , ServoH_Max);
HorizontalServo.write(HorizontalServoPosition);
VerticalServo.write(VerticalServoPosition);
delay(10);
}