Project 1 – Joystick Servo Control

Arduino is new to me, Research Research Research!!

What is Arduino:

“Arduino is a tool for making computer that can sense and control more of the physical world than your desktop computer. It’s an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board.

Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can be stand-alone, or they can communicate with software running on your computer (e.g. Flash, Processing, MaxMSP.) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free.

The Arduino programming language is an implementation of Wiring, a similar physical computing platform, which is based on the Processing multimedia programming environment.”

This sounds interesting!

Arduino

The project brief:

“Undertake a 3 week physical computing design project. Present this project in your tutorial. This project should constitute 25 hours of research, design and development (8- 10 hours per week). Your project should have a minimum of 3 sensors and actuators (eg 2 inputs and 1 output, or 1 input and 2 outputs etc)

While it is possible to complete this project, using only the sensors and actuators in your Arduino kits, it will be more interesting to explore the range of components available. You can often cannibalise sensors and actuators from old products and there is a list of suppliers at the back of this document. For prototyping you are welcome to make use of the laser cutter and 3D printer.”

Idea:

I want to explore the use of use of servos and how the user is able to interact with them. The way the user can control their movements will be explored.

Servos:

Servo

Definition: “A servo mechanism, which is shortened to servo, is a small device that incorporates a two wire DC motor, a gear train, a potentiometer, and integrated circuit, and an output shaft. Of the three wires that stick out from the motor casing, one is for power, one is for ground, and one is a control input line. The shaft of the servo can be positioned to specific angular positions by sending a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. If the coded signal changes, then the angular position of the shaft changes.

Common Use: A very common use of servos is in Radio Controlled models like cars, airplanes, robots, and puppets. They are also used in powerful heavy-duty sail boats. Servos are rated for Speed and Torque. Normally there are two servos of the same kind, one geared towards speed (sacrificing torque), and the other towards torque (sacrificing speed).

Construction: Servos are constructed from three basic pieces; a motor, a potentiometer (variable resister) that is connected to the output shaft, and a control board. The potentiometer allows the control circuitry to monitor the current angle of the servo motor. The motor, through a series of gears, turns the output shaft and the potentiometer simultaneously. The potentiometer is fed into the servo control circuit and when the control circuit detects that the position is correct, it stops the motor. If the control circuit detects that the angle is not correct, it will turn the motor the correct direction until the angle is correct. Normally a servo is used to control an angular motion of between 0 and 180 degrees. It is not mechanically capable (unless modified) of turning any farther due to the mechanical stop build on to the main output gear.

The amount of power applied to the motor is proportional to the distance it needs to travel. So, if the shaft needs to turn a large distance, the motor will run at full speed. If it needs to turn only a small amount, the motor will run at a slower speed. This is called proportional control.”

This information has given me and great understanding of not only what a servo is, but also what is possible with one. They might just look like small useless boxes that turn an output shaft, but there is potential to create something exciting!

Servo Code: 

“The position of the servo motor is set by the length of a pulse. The servo expects to receive a pulse roughly every 20 milliseconds. If that pulse is high for 1 millisecond, then the servo angle will be zero, if it is 1.5 milliseconds, then it will be at its centre position and if it is 2 milliseconds it will be at 180 degrees.

Learn Servos

I found a really interesting video on YouTube that provided me with a lot of knowledge in just 1 minute!..

Servo Example:

Taking my knowledge of servos, I now plan to find an example of a servo control code online and try it out myself!

Sweep Example:

“Sweeps the shaft of a RC servo motor back and forth across 180 degrees.

This example makes use of the Arduino servo library.”

Circuit: 

sweep

This schematic shows how i have connected up my servo to the arduino board. I have a 5V wire, Ground Wire, and Signal Pin Wire.

The code used was:

#include <Servo.h> 

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 

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
}

void loop()
{
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
{                                  // in steps of 1 degree 
myservo.write(pos);              // tell servo to go to position in variable ‘pos’ 
delay(15);                       // waits 15ms for the servo to reach the position 
}
for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
{
myservo.write(pos);              // tell servo to go to position in variable ‘pos’ 
delay(15);                       // waits 15ms for the servo to reach the position 
}
}

It works!: I have for the first time set up a circuit and used coding to control a servo!

Next Component: 

Joystick

I spent a long time researching what a joystick consists of, and how to interface one with an arduino.

joy_pic_480

Here is a schematic that I sourced from online:

joy_sch_480

“The joystick in the picture is nothing but two potentiometers that allow us to measure the movement of the stick in 2-D. Potentiometers are variable resistors and, in a way, they act as sensors providing us with a variable voltage depending on the rotation of the device around its shaft.

The kind of program that we need to monitor the joystick has to make a polling to two of the analog pins. We can send these values back to the computer, but then we face the classic problem that the transmission over the communication port has to be made with 8bit values, while our DAC (Digital to Analog Converter – that is messuring the values from the potentiometers in the joystick) has a resolution of 10bits. In other words this means that our sensors are characterized with a value between 0 and 1024.

The following code includes a method called treatValue() that is transforming the sensor’s messurement into a value between 0 and 9 and sends it in ASCII back to the computer. This allows to easily send the information into e.g. Flash and parse it inside your own code.”

Reading this gives me a great understanding how how the component works! I will now get the code and create a circuit to test the joystick.

Code: 

int ledPin = 13;
int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
int joyPin2 = 1;                 // slider variable connecetd to analog pin 1
int value1 = 0;                  // variable to read the value from the analog pin 0
int value2 = 0;                  // variable to read the value from the analog pin 1

void setup() {
pinMode(ledPin, OUTPUT);              // initializes digital pins 0 to 7 as outputs
Serial.begin(9600);
}

int treatValue(int data) {
return (data * 9 / 1024) + 48;
}

void loop() {
// reads the value of the variable resistor 
value1 = analogRead(joyPin1);
// this small pause is needed between reading
// analog pins, otherwise we get the same value twice
delay(100);
// reads the value of the variable resistor 
value2 = analogRead(joyPin2);

digitalWrite(ledPin, HIGH);
delay(value1);
digitalWrite(ledPin, LOW);
delay(value2);
Serial.print(‘J’);
Serial.print(treatValue(value1));
Serial.println(treatValue(value2));
}

It Works!:

I have made the led blink with the values from the sensor as a direct visual feedback of how we control the joystick!

Rubber Joystick:

joystick3

I have purchased this joystick and will now try and get this to control my two servos.

My schematic:

Below is a schematic I have drawn up using an application called Fritzing. I have used the 5v pin, ground, Vx, Vy, A0,A1,D5, and D6.

Arduino Sketch - JoyStick_bb

I next tired to put together the code for the task:

#include <Servo.h>

const int servo1 = 5; const int servo2 = 6; const int joyH = 0; const int joyV = 1;

int servoVal; Servo myservo1; Servo myservo2;

void setup() {

// Servo myservo1.attach(servo1); myservo2.attach(servo2);

// Inizialize Serial Serial.begin(9600);

}
void loop(){

servoVal = analogRead(joyH);
servoVal = map(servoVal, 0, 1023, 0, 180);

myservo2.write(servoVal);

// Read the horizontal joystick value (value between 0 and 1023) servoVal = analogRead(joyV);
servoVal = map(servoVal, 0, 1023, 70, 180); myservo1.write(servoVal);

delay(15); }11146050_10152926521399608_121303049_n

It worked!

11149020_10152926521489608_1424180618_n

Below is a video I made of my circuit working!! It was a happy moment!
Making a Frame:
11104040_10152926521394608_869187252_n
Here you can see I have made a quick box for my circuit to go in. I have done this to it up neaten, doing this adds to the users experience and allows them to focus on what is happening.
Conclusion: 
To conclude, I am very proud with the progress I have made. I have learnt a lot about software and hardware. Learning the principles of my components before trying to piece them together was very helpful. Online has a great amount of work that can help a newbie understand how things work.
I loved getting involved in the project and have enjoyed being focused in on a small area of electronics.
I feel I have made a circuit and product that has major potential. It functions as it should and with not too much extra time, I would be able to connect many interesting things to the servos.
I can’t wait to get involved in the next project!
Nate.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s