Arduino – LS-3006 Servo controlled by Keyes SJoys joystick

IMG_20131201_141242

IMG_20131201_141311

Basic code to control a servo:

// Sweep
// by BARRAGAN  
// This example code is in the public domain.

//arduino joystick test
//Janne Huotari

#include  
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

int joy1 = 3;
int joy2 = 4;
int val1 = 0;
int val2 = 0;
int val_final = 0;

void setup() { 

  Serial.begin(9600);
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() {
  
  // JOYSTICK
  val1 = analogRead(joy1);
  
  //servoVal = analogRead(joyV);           
  //servoVal = map(servoVal, 0, 1023, 70, 180);     // scale it to use it with the servo (result between 70 and 180)
  val_final = map(val1, 0, 1023, 0, 180);     // scale it to use it with the servo (result between 70 and 180)
  
  delay(10);
  val2 = analogRead(joy2);

  Serial.print(val_final);
  Serial.print(val2);
  Serial.println();
  
  myservo.write(val_final);
}