2013年3月31日 星期日

Using the Serial port!

const unsigned int LED_PIN = 13; //Declare LED_PIN= 13pin
const unsigned int BAUD_RATE = 9600; //BAUD RATE=9600
void setup()
{
pinMode(LED_PIN, OUTPUT); //Define this 13pin is output
Serial.begin(BAUD_RATE); // Using this "Serial.begin()" function to start the class
//You can specify the Baud Rate of the Arduino from the computer to exchange messages
//Usually we use 9600 bps。Of course, you can also use other speed
//But usually no more than115,200 bps..
}
void loop()
{
if (Serial.available() > 0) //Serial.available() whether a message is received?
//If the received data Will return the byte If no data is returned -1
{
int command = Serial.read(); //Declare "command" is "Serial.read()" ,
//using "Serial.read()" Read data
if (command == '1') // if typing 1
{
digitalWrite(LED_PIN, HIGH); // LED on!!
Serial.println("LED on"); //Serial show ON
}
else if (command == '2') //if typing 2
{
digitalWrite(LED_PIN, LOW); // LED off
Serial.println("LED off"); //Serial show OFF
}
else
{
Serial.print("Unknown command: "); //if typing any key!
Serial.println(command); // show any key
}
}
}

沒有留言:

張貼留言