Arduino – Control Servos with Joystick
What we do
What we need
How we do (Hardware)
How we do (Software)
Still shots Outcome
What we do
We know how to make Servo move in previous lessons.
but furthermore, we're going to learn how we can control Servos with Joystick! 🙂
What we need
How we do (Hardware)
How we do (Software)
Let's take a look at Software side.
If anyone does not know 'How to upload our sketch to Arduino', then please click the following article before you go.
Step 1) Include the Servo library for controlling Servo motor
#include <Servo.h>
Step 2) Declare Servo pins and Joystick pins we want to use with
int ServoPin1 = 3;
int ServoPin2 = 5;
int joyX = 0;
int joyY = 1;
int joyVal;
Step 3) Create Servo objects to control Servos
Servo servo1;
Servo servo2;
Step 4) Attach the servo to the 3 and 5 pins
void setup()
{
servo1.attach(ServoPin1);
servo2.attach(ServoPin2);
}
Step 5) Reading analog values from joyX and joyY and mapping them into servo1 and servo2 with 15ms delay.
void loop()
{
joyVal = analogRead(joyX);
joyVal = map(joyVal, 0, 1023, 0, 180);
servo1.write(joyVal);
joyVal = analogRead(joyY);
joyVal = map(joyVal, 0, 1023, 0, 180);
servo2.write(joyVal); delay(15);
}
Leave a Reply
Want to join the discussion?Feel free to contribute!