Home » Adobe, Arduino, _Favourites

Arduino: control a LED and one servo motor using a joystick

28 October 2009 6 Comments

In this article we’ll control a servo motor using a joystick and the microcontroller Arduino.
We’ll also set the value of a LED-2-colors in according with the stick axis.

It’s just an introduction for the next article: Arduino and Flex: connect a joystick to your application

The Prototype:
mainphototmb

The final result (I’m sorry for the video quality):

REQUIRED HARDWARE
To accomplish this task you need the following hardware:

- Microcontroller Arduino Board Duemilanove
- A Bread Board
- LED 2Colors (or 2 different LED)
- Servo motor (i’m using a simple hitec HS-55)
- Joystick (i’m using a Mini Stick connected to a joystick board)

THE PROTOTYPE

First of all we need to assemble the circuit as you can see in the following images:

circuittmb

photo 1 | photo 2 | photo 3 | photo 4 | photo 5

So:
1) The Joystick is connected to analog pins 4 and 5.
2) The Servo to digital pin 3
3) LED to pins 6 and 11 (instead of 6 you could have a better experience using pin 9 or 10)

SKETCH FILE

/*
  Joystick + Servo + Led 2 Colors 
 
 
  Features: 
 
  - Move the servo using a Joystick 
  - Change LED1 value in according to the horizontal joystick position
  - Change LED2 value in according to the vertical joystick position
 
  NOTE1: I have used a LED with two Colors but you could use 2 different LED
  NOTE2: Joystick Button is not used in this sketch
 
 The circuit:
 * LED 2 COLORS attached to digital PWM pin 3 and 11. 
 * Servo attached to digital PWM pin 6
 * Joystick attached to analog pins 4 and 5
 
 * created 28 october 2009
 * by Fabio Biondi  <http://www.fabiobiondi.com/blog>
*/
 
#include <Servo.h> 
 
const int servo = 3;       // the number of the servo
const int ledPin1 =  6;    // the number of the LED pin 1
const int ledPin2 =  11;   // the number of the LED pin 2
const int joyH = 4;        // the number of the Joystick Horizontal Axis
const int joyV = 5;        // the number of the Joystick vertical Axis
 
int servoVal;         	 // variable to read the value from the analog pin 
 
Servo myservo;  		 // create servo object to control a servo 
 
 
 
void setup() {
 
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);      
  pinMode(ledPin2, OUTPUT);      
 
  // Servo   
  myservo.attach(servo);  // attaches the servo on pin 9 to the servo object 
 
  // Inizialize Serial
  Serial.begin(9600);
}
 
 
void loop(){
 
    // Check Joystick values  using the serial monitor
    outputJoystick();
 
    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyH);           
    servoVal = map(servoVal, 0, 1023, 0, 20);     // scale it to use it with the servo (value between 0 and 20) 
    analogWrite(ledPin1, servoVal);         		 // Set the LED1 Value
 
    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyV);            
    servoVal = map(servoVal, 0, 1023, 0, 20);     // scale it to use it with the servo (value between 0 and 20) 
    analogWrite(ledPin2, servoVal);              	 // Set the LED2 Value
 
 
    // Move Servo
    servoVal = analogRead(joyV);                  	 // reads the value of the potentiometer (value between 0 and 1023) 
    servoVal = map(servoVal, 0, 1023, 0, 179);   // scale it to use it with the servo (value between 0 and 180) 
 
    myservo.write(servoVal);                     	// sets the servo position according to the scaled value 
    delay(15);                                     		// waits for the servo to get there 
 
}
 
 
/**
* Display joystick values
*/
void outputJoystick(){
 
    Serial.print(analogRead(joyH));
    Serial.print ("---");  
    Serial.print(analogRead(joyV));
    Serial.println ("----------------");
}

In the next article will see how to control a Flex application using the same circuit

6 Comments »

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.