/* Serial communication with the Arduino * using any serial communication application * Setup: -Wire an LED from digital pin 7 thru 330ohm resistor to gnd * - Connect a servo to +, gnd and digital pin 9 */ #include <Servo.h> // Arduino library for servo motors byte inByte; int LEDpin = 7; int servoPin = 9; // variable for servo pin number int servoPosition; Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created void setup(){ Serial.begin(9600); // Begin serial communication pinMode(LEDpin, OUTPUT); myservo.attach(servoPin); // attaches the servo on pin 9 to servo object } void loop(){ if (Serial.available() > 0){ // only if serial data's there inByte = Serial.read(); // read it, store in inByte if (inByte == 49){ // if serial value = "1" key digitalWrite(LEDpin, HIGH); // light LED servoPosition = 5; myservo.write(servoPosition); // tell servo to go to position } else if(inByte == 50) { // Key is "2" digitalWrite(LEDpin, LOW); // turn LED off servoPosition = 175; myservo.write(servoPosition); // tell servo to go to position } } } |