Control Servo Motor With Joystick PS2
Today I will give you a tip how to control "Control Servo Motor with Joystick PS2".
So what we need for this:
  • Arduino UNO
  • Servo Motor
  • Joystick PS2
#include <servo.h>

Servo Wheel;

const int xPin = A0;
const int yPIn = A1;
const int swPin = 8;

int xVal = 0;
int yVal = 0;
int zVal = 0;

void setup(){
 Wheel.attach(9);
 wheelDegree(90,250);

 pinMode(swPin, INPUT);
 digitalWrite(swPin, HIGH);
 Serial.begin(9600);
}

void loop(){
 yVal = analogRead(yPin);
 if(yVal > 527){
 wheelDegree(135,250);
 }else if(yVal <504){
 wheelDegree(45,250);
 }else{
 wheelDegree(90,250);
 }
 SerialMonitor();
}

void wheelDegree(int degree, int ms){
 Wheel.write(degree);
 delay(ms);
}

void SerialMonitor(){
 Serial.print("X:");
 Serial.print(analogRead(xPin),DEC);
 Serial.print("\tY:");
 Serial.print(yVal,DEC);
 Serial.print("\tZ:");
 Serial.print(digitalRead(swPin):
 delay(500):
}
Category: Joystick PS2 | Added by: Dominus (07/09/2015)
Views: 1260 | Tags: Servo, PS2, motor, arduino, Arduino Uno, control | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *: