Sunday, June 20, 2010

Arduino Servo motor code 1

// This code depends on the Servo Software Library. You can get
// this lib from http://www.arduino.cc/playground/ComponentLib/Servo

// To use this app just send the board a number followed by an 's'
// to set the angle of the servo (0-180 are valid)

#include

// This is the pin you need to attach the servo to
#define SERVO_1_PIN 2

Servo g_servo1;

void setup()
{
pinMode(SERVO_1_PIN, OUTPUT);
g_servo1.attach(SERVO_1_PIN);
Serial.begin(9600);
Serial.println("Enter number and press 's' to send it to servo.");
}

void loop()
{
static int val = 0;

if (Serial.available())
{
char ch = Serial.read();

switch(ch)
{
case '0'...'9':
val = val * 10 + ch - '0';
break;
case 's':
g_servo1.write(val);
Serial.print("Servo1 is set to: ");
Serial.print(val, DEC);
Serial.println(" degrees");
val = 0;
break;
}
}

Servo::refresh(); // Should try to call this every 20 ms to insure servo stays set
}
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects

Followers

My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following