Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <Servo.h> // Include the library for the servo
  2.  
  3. #define RAIN_SENSOR A0
  4. #define SERVO1_PIN 10
  5. #define SERVO2_PIN 11
  6.  
  7. Servo myservo1; // Create servo object to control a servo
  8. Servo myservo2;
  9.  
  10. int rainAdc = 0;
  11. boolean rainState = false;
  12. boolean previousRainState = false;
  13. boolean changeState = false;
  14. boolean moveServo = false;
  15. int servoLoop = 0;
  16. int pos1 = 0;
  17. int pos2 = 0;
  18.  
  19. long currentMillis = 0;
  20. long previousMillis = 0;
  21. int interval = 2000;
  22.  
  23. void setup()
  24. {
  25. pinMode(RAIN_SENSOR, INPUT_PULLUP);
  26.  
  27. Serial.begin(9600);
  28.  
  29. myservo1.attach(SERVO1_PIN);
  30. myservo2.attach(SERVO2_PIN);
  31. }
  32.  
  33. void loop()
  34. {
  35. rainAdc = analogRead(RAIN_SENSOR);
  36. if (rainAdc < 800) {
  37. if (rainState == false && changeState == false) {
  38. changeState = true;
  39. previousMillis = millis();
  40. }
  41. else if (rainState == true && changeState == true) {
  42. changeState = false;
  43. }
  44. }
  45. else {
  46. if (rainState == true && changeState == false) {
  47. changeState = true;
  48. previousMillis = millis();
  49. }
  50. else if (rainState == false && changeState == true) {
  51. changeState = false;
  52. }
  53. }
  54.  
  55. if (changeState == false) {
  56. previousMillis = millis();
  57. }
  58.  
  59. if (changeState == true) {
  60. currentMillis = millis();
  61. if (currentMillis - previousMillis > interval) {
  62.  
  63. changeState == false;
  64.  
  65. if (rainState == false) {
  66. rainState = true;
  67. moveServo = true;
  68. }
  69. else {
  70. rainState = false;
  71. moveServo = true;
  72. }
  73. }
  74. }
  75.  
  76. if (moveServo == true) {
  77. if (rainState == true) {
  78. pos1 = 0;
  79. pos2 = 90;
  80. for (servoLoop = 0; servoLoop < 90; servoLoop++) {
  81. myservo1.write(pos1++);
  82. myservo2.write(pos2--);
  83. delay(30);
  84. }
  85. myservo1.write(103);
  86. myservo2.write(0);
  87.  
  88. moveServo = false;
  89. }
  90.  
  91. else {
  92. pos1 = 103;
  93. pos2 = 0;
  94. for (servoLoop = 0; servoLoop < 90; servoLoop++) {
  95. myservo1.write(pos1--);
  96. myservo2.write(pos2++);
  97. delay(30);
  98. }
  99. myservo1.write(0);
  100. myservo2.write(90);
  101.  
  102. moveServo = false;
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement