In this project we will control 2 Servos Using a Joystick (thumbstick) with an Arduino. I use usually arduino mega 2560. You can use the other arduino cards. Good Luck.
Schema:
Materials:
Arduino UNO / Other Arduino Cards x 1
Breadboard x 1
Servo Motor x 2
Joystick x 1
Jumper Cable
Program:
// 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;
Servo HorizontalServo;
Servo VerticalServo;
int HorizontalPotValue;
int HorizontalServoPosition;
int VerticalPotValue;
int VerticalServoPosition;
void setup()
{
HorizontalServo.attach(ServoHorizontalPin);
VerticalServo.attach(ServoVerticalPin);
}
void loop()
{
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(20);
}
Servo Motor Pins:
Project Related Images:
Không có nhận xét nào:
Đăng nhận xét