Wednesday, 10 April 2013

Servo fun

   Yesterday received 2 servo's in the post from cool components, one is a standard HS-422 servo from Hi-Tec and the other is a GoTeck continuous rotation servo. These will be the servo's I used to lay the foundation work for a small robotic arm which will hopefully lead to bigger and better things later on. I'm pleased to say the code I had written for the Arduino worked perfectly even though I had no opportunity to test with with a servo until now. The only problem I encountered was powering the servo, a issue I had not thought of until now. Asking the Arduino 5V pin to supply upwards of 250mA was not the brightest of idea but it did manage it for a while. It was clear the Arduino was not happy because the LCD screen would fade from crisp white text to a much darker colour. Because of this I spent part of my evening quickly putting together a better way to supply the Arduino and the servo's
 
GoTeck is on A, Hi-Tec is on C.

   What little testing I did manage to do before being forced to make a power supply was very promising, both servo's responded quickly and accurately without any stuttering or faults to mention. The LCD screen is my visual aid in controlling the precise angle of the standard servo and the rotation speed and direction of the GoTeck.

Presented to you by my aid Haruhi Suzumiya.

   Eventually this stick thing was born... Its not pretty or amazing and I will put the time and effort into creating a better version when I have more time but for now it does the job. It is protected by a poly fuse which I am a little unsure about as I have never used them before.
   The pins are directly supplied by a power source and the signal wire is redirected through the board to the output pin which then goes to the Arduino. The stick is supplied by a mutilated usb cable so the supply pins can be used as a power source, these simply plug into the stick to supply power. Any servo's used with the Arduino must share a common ground with it, this is accomplished by the usb hub that is currently powering the Arduino and the Stick. Its not a ideal solution but it works for now. at some point in the future I will make a extra long wire I can use with my 0-30V dc power supply so I can supply the correct voltage and not worry about killing the usb hub by accident, I'm not entirely sure what current it can supply.

So far so good.
   Everything Is progressing nicely at the moment, I will spend a few days tinkering with the servo's and see what I can learn from them. hopefully I will also find the time to put together a better power supply system with a capacitor, on/off switch and fuse, you never know when a short circuit might appear and its better to be safe than sorry. 

Last but not least is the code I have been using so far, bear in mind I have written it all myself using only the basic resources available to me, I have never joined any forums or spoken to more educated people on the subject so there is most likely a better way to do this.

---

#include <LiquidCrystal.h>;  //Activate the LCD libary
#include <Servo.h>;             //Activate the servo libary

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  //tell the arduino which pins to use for the LCD

Servo servoA;  //creating servo names
Servo servoB;  
Servo servoC;
Servo servoD;

void setup () {
  lcd.begin(16, 2);    //tell Arduino the LCD is 16 colums long and 2 rows deep
  servoA.attach(6);  //attaching servoA to pin 6
  servoB.attach(7);  //attaching servoB to pin 7
  servoC.attach(8);  //attaching servoC to pin 8
  servoD.attach(9);  //attaching servoD to pin 9
}


void loop () {

  //Display number Identity  
  lcd.setCursor(4, 0);      //Set lcd position to top left middle
  lcd.print("<A");            //Print Pot ID <A
  lcd.setCursor(10, 0);   //Set lcd position to top right middle
  lcd.print("B>");            //Print Pot ID B>
  lcd.setCursor(4, 1);     //Set lcd position to bottom left middle
  lcd.print("<C");           //Print Pot ID <C
  lcd.setCursor(10, 1);   //Set lcd position to bottom right middle
  lcd.print("D>");           //Print Pot ID D>


  //Pot A read and print on LCD
  int sensorValue0 = analogRead(A0);                            //Read value from pin A0
  sensorValue0 = map(sensorValue0, 0, 1023, 0, 179);  //Convert 0-1023 to 0-179 degrees
  lcd.setCursor(0, 0);                                                     //Set lcd position to top left 
  lcd.print(sensorValue0);                                              //Print value from A0 in degrees on lcd

  //Pot B read and print on LCD
  lcd.setCursor(13, 0);                                                     //Set lcd position to top right
  int sensorValue1 = analogRead(A1);                             //Read value from pin A1
  sensorValue1 = map(sensorValue1, 0, 1023, 0, 179);   //Convert 0-1023 to 0-179 degrees 
  lcd.setCursor(13, 0);                                                    //Set lcd position to top right 
  lcd.print(sensorValue1);                                               //Print value from A1 in degrees on lcd

  //Pot C read and print on LCD
  int sensorValue2 = analogRead(A2);                             //Read value from pin A2
  sensorValue2 = map(sensorValue2, 0, 1023, 0, 179);   //Convert 0-1023 to 0-179 degrees 
  lcd.setCursor(0, 1);                                                      //Set lcd position to bottom left
  lcd.print(sensorValue2);                                               //Print value from A2 in degrees on lcd

  //Pot D read and print on LCD
  int sensorValue3 = analogRead(A3);                             //Read value from pin A3
  sensorValue3 = map(sensorValue3, 0, 1023, 0, 179);   //Convert 0-1023 to 0-179 degrees 
  lcd.setCursor(13, 1);                                                    //Set lcd position to bottom right 
  lcd.print(sensorValue3);                                               //Print value from A3 in degrees on lcd

  //Servo Control
  servoA.write(sensorValue0);  //sets servo A to position dictated by sensorValue0
  servoB.write(sensorValue1);  //sets servo B to position dictated by sensorValue1
  servoC.write(sensorValue2);  //sets servo C to position dictated by sensorValue2
  servoD.write(sensorValue3);  //sets servo D to position dictated by sensorValue3

  delay(10);  //wait 0.01 seconds before repeating script  
}
---

No comments:

Post a Comment