unsigned long timeRef = 0; //This is used for the servo function void setup() { pinMode(9, OUTPUT); //The servo pin must be set as a digital out } void loop() { for(int i=0; i<40; i++){ //Repeat the servo command so it will actually get to it's destination setServo(9, 0, 575, 2260); //575us is the extreme counter clockwise pulse, 2260us is for clockwise delay(15); //Wait for a bit, or the servo function will ignore us } for(int i=0; i<40; i++){ //Repeat the servo command so it will actually get to it's destination setServo(9, 180, 575, 2260); delay(15); //Wait for a bit, or the servo function will ignore us } } /******************************************************************************* setServo Input: byte pin - the digital pin which is connected to the servo's control line byte angle - the angle to which we want to go (0-180¼) int min - this value makes the servo go totally counter clockwise int max - this value makes the servo go totally clockwise *******************************************************************************/ void setServo(byte pin, byte angle, int min, int max){ if( (millis()-timeRef) >= 15 ){ //Have we waited long enough since we last called this function? digitalWrite(pin, HIGH); //Start the pulse /* Here's how the math works: (angle/180) - converts the angle into a percentage (max-min) - makes sure that we won't go below min when we multiply +min - adds the minimum back in, becuase we need it */ delayMicroseconds( ((double)angle/180)*(max-min)+min ); //Keep the pulse high for a bit... digitalWrite(pin, LOW); //End the pulse timeRef = millis(); //Reset our time reference } }