Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. //Example By ArduinoAll
  2. #include <SoftwareSerial.h>
  3. int i = 0;
  4. int led = 13;
  5. char str[11] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
  6. SoftwareSerial mySerial(2, 3); // RX, TX
  7. void setup()
  8. {
  9. pinMode(led,13);
  10. Serial.begin(9600);
  11. mySerial.begin(9600);
  12. }
  13. void loop()
  14. {
  15. if (mySerial.available()) {
  16. i = i + 1;
  17. str[i] = mySerial.read();
  18. /*Serial.print(i);
  19. Serial.print(":");
  20. Serial.println(str[i]);*/
  21. if (i == 9){
  22. i = 0;
  23. }
  24.  
  25. if(str[5]=='1'){ // สวิตช์ 1 กดเข้ามา
  26. if(str[7]=='3'){
  27. digitalWrite(led,1); // เปิดไฟ
  28. }else{
  29. digitalWrite(led,0);
  30. }
  31. }else if(str[5]=='2'){// สวิตช์ 2 กดเข้ามา
  32. if(str[7]=='3'){
  33. Serial.println("Switch 2 ON");
  34. }else{
  35. Serial.println("Switch 2 OFF");
  36. }
  37. }else if(str[5]=='3'){// สวิตช์ 3 กดเข้ามา
  38. if(str[7]=='3'){
  39. Serial.println("Switch 3 ON");
  40. }else{
  41. Serial.println("Switch 3 OFF");
  42. }
  43. }
  44.  
  45. }
  46. if (Serial.available())
  47. mySerial.write(Serial.read());
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement