Skip to content

Instantly share code, notes, and snippets.

@ruisantos16
Created February 12, 2013 20:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruisantos16/4773241 to your computer and use it in GitHub Desktop.
Save ruisantos16/4773241 to your computer and use it in GitHub Desktop.
Control Servo with Visual Basic (Arduino code) visit my website for more information. www.randomnerdtutorials.com
/*
* created by Rui Santos, http://randomnerdtutorials.wordpress.com
* Control a servo motor with Visual Basic
* 2013
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); //begins serial communication
}
void loop()
{
int pos;
if (Serial.available()){
delay(100);
while(Serial.available()>0){
pos=Serial.read(); //reads the value sent from Visual Basic
if(pos=='0')
myservo.write(90); //rotates the servo 90 degrees (Left)
else if(pos=='1')
myservo.write(-90); //rotates the servo 90 degrees (right)
else if(pos=='2')
myservo.write(180); //rotates the servo 180 degrees (Left)
else if(pos=='3')
myservo.write(-180); //rotates the servo 180 degrees (right)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment