Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Servo.h>
  3.  
  4. Servo myservo;
  5.  
  6. int val;
  7. String input_string = "";
  8. int serv = 0;
  9. byte state = false;
  10.  
  11. void setup()
  12. {
  13. pinMode(13, OUTPUT);
  14. Serial.begin(9600);
  15. myservo.attach(10);
  16. }
  17.  
  18. void loop()
  19. {
  20. while (Serial.available() > 0) {
  21. char c = Serial.read();
  22. if (c == '\n') {
  23. digitalWrite(13, HIGH);
  24. Command();
  25. digitalWrite(13, LOW);
  26. input_string = "";
  27. } else {
  28. input_string += c;
  29. }
  30. }
  31. }
  32.  
  33. void Command()
  34. {
  35. //Serial.print("Input_string is: ");
  36. //Serial.println(input_string);
  37.  
  38.  
  39. if (input_string.startsWith("s") == true)
  40. {
  41. input_string.replace("s", ""); // заменяем строку
  42. serv = input_string.toInt(); // преобразовываем строку в число
  43. if (serv >170) serv = 180;
  44. if (serv <10) serv = 0;
  45. myservo.write(serv);
  46. Serial.print("Servo set to ");
  47. Serial.println(serv);
  48. delay(15);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement