_09_serial.ino 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /***********************************************************
  2. File name: 09_serial.ino
  3. Description: If you send a character ‘1’ or ‘0’ on the
  4. serial monitor, the status of LED will be lit
  5. or gone out.
  6. Website: www.adeept.com
  7. E-mail: support@adeept.com
  8. Author: Tom
  9. Date: 2015/05/02
  10. ***********************************************************/
  11. int ledpin=11; //definition digital 11 pins as pin to control the LED
  12. void setup()
  13. {
  14. Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  15. pinMode(ledpin,OUTPUT);//Set digital 11 port mode, the OUTPUT for the output
  16. }
  17. void loop()
  18. {
  19. char receiveVal; // Defined receive data
  20. if(Serial.available() > 0) //Receive serial data
  21. {
  22. receiveVal = Serial.read(); //Save the serial data received
  23. if(receiveVal == '1') //Receive data is 1, lit LED lights
  24. {
  25. digitalWrite(ledpin,HIGH); //print out the value of the LED
  26. Serial.println("LED:ON"); //send data to the serial monitor
  27. }
  28. if(receiveVal == '0') //Receive data is 0, off LED lights
  29. {
  30. digitalWrite(ledpin,LOW); //print out the value of the LED
  31. Serial.println("LED:OFF");//send data to the serial monitor
  32. }
  33. }
  34. delay(50); //delay 50ms
  35. }