Arduino code for team 4.1’s U-box

Team 4.1 Arduino controled armSince our 5v servo motor needed more juice to run than our A3BU could provide we opted to use an Arduino to power the motor for the arm. Below is the fairly simple code needed to flip the switch on our U-box. An initial position is set, then once the pin state is read high the arm position is moved to flip the switch and then return to its initial position.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

// twelve servo objects can be created on most boards

const int pin = 2;

int pos = 0;    // variable to store the servo position

int state = 0;

void setup()

{

myservo.attach(9);  // attaches the servo on pin 9 to the servo object

pinMode(pin, INPUT);

}  

void loop()

{

state = digitalRead(pin);

if (state == HIGH){

for(myservo.read(); pos <= 35; pos += 1) // goes from 0 degrees to 35 degrees

{                                  // in steps of 1 degree

myservo.write(pos);              // tell servo to go to position in variable ‘pos’

delay(20);                       // waits 15ms for the servo to reach the position

}

}

else {

for(myservo.read(); pos>=5; pos-=1)     // goes from 35 degrees to 0 degrees

{

myservo.write(pos);              // tell servo to go to position in variable ‘pos’

delay(20);                       // waits 15ms for the servo to reach the position

}

}

delay(100);

}