Thursday, May 5, 2011

Lab 4 Part 2- Servo Motors! (Part 1 of 2)

Servo motors! Mechatronics + Arduino time! Time for some pictures =)



It's kind of hard to tell from these pictures, but the Servo motor was set up such that the black wire went to ground, the red wire went to 5 Volts, and the white wire went to the Arduino pin that was reserved for such inputs (10 in this case). I am going to post a bit on this, but Paul gets the honors of also writing about the servo motor too, so this is officially Part 1 of 2!

Part 2 Code-- Sending pulses to Servo motor!

int pin = 10;
int i = 0;

void setup()
{
Serial.begin(9600);
pinMode(pin,OUTPUT);

}

void loop()
{

for (i=200;i<800;i++)
{
digitalWrite(pin, HIGH);
delayMicroseconds(i);
digitalWrite(pin, LOW);
delayMicroseconds(19000-i);
}

delay(100);

for (i=800;i>200;i--)
{
digitalWrite(pin, HIGH);
delayMicroseconds(i);
digitalWrite(pin, LOW);
delayMicroseconds(19000-i);
}
delay(100);

}

Part 3 Code- Rotating servo according to input!
int input;
int input2;
int pin = 10;
int degs;

void setup()
{
Serial.begin(9600);
pinMode(pin,OUTPUT);
}

void loop()
{
input = Serial.read();
input2 = input2 * 10 + (input-48);
degs = (input2/180)+1;

digitalWrite(pin,HIGH);
delay(degs);
digitalWrite(pin,LOW);
delay(20-degs);
}

Part 4 Code- Rotating servo back and forth
int pin = 10;
int i;

void setup()
{
Serial.begin(9600);
pinMode(pin,OUTPUT);
}

void loop()
{
if(i<=150)
{
digitalWrite(pin,HIGH);
delayMicroseconds(2000);
digitalWrite(pin,LOW);
delayMicroseconds(18000);
}

if(i>150 && i<=300)
{
digitalWrite(pin,HIGH);
delayMicroseconds(2000);
digitalWrite(pin,LOW);
delayMicroseconds(18000);
}
}

Part 5 Code- Controlling servo speed!
int pin = 10;
int i;
int t = 1000;

void setup()
{
Serial.begin(9600);
pinMode(pin,OUTPUT);
}

void loop()
{
for(t=1000;t<=2000;t++)
{
digitalWrite(pin,HIGH);
delayMicroseconds(t);
digitalWrite(pin,LOW);
delayMicroseconds(20000-t);
}

for(i=0;i<200;i++)
{
digitalWrite(pin,HIGH);
delayMicroseconds(2000);
digitalWrite(pin,LOW);
delayMicroseconds(18000);
}
}

No comments:

Post a Comment