Inputs: Digital & Analog

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

  1. Arduino UNO
  2. XY Dual Axis Joystick Module
  3. Wire jumpers
  4. Breadboard
  5. Servo Motor SG90
Joystick+Servo-Motor_DarioNarvaez.JPG

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*/
}