This week I'm experimenting with digital and analog inputs. In the following case, I used the x axis of a joystick in order to control the linear rotation of a servo motor.
Materials
- Arduino UNO
- XY Dual Axis Joystick Module
- Wire jumpers
- Breadboard
- Servo Motor SG90
Code
#include <Servo.h>
Servo servo; /*Create a servo object*/
int portCom; /*Create a variable to almacenate all the data coming from the joystick*/
int servoPin = 11; /*Digital Pin Connected to Servo*/
void setup(){
servo.attach(servoPin);
servo.write(90); /*Initiate the servo in 90 degreess*/
}
void loop(){
portCom = analogRead(0); /*X Pin to receive data*/
portCom = map(portCom, 0, 1023, 0, 180); /*Map of th received data*/
servo.write(portCom); /*Servo moves accorurding with the position of the joystick*/
delay(15); /*Reading time of the Analog Pin A0*/
}