1
1
/*
2
- Using a servo as a car Wiper, activated by switch.
2
+ Using a servo as a car Wiper, activated by switch.
3
3
4
- modified on 19 March 2020
5
- by Durgesh Pachghare
4
+ The circuit:
5
+ - switch is attached from pin 2 to ground
6
+ - Servo's signal pin is attached to Pin 9
7
+
8
+ modified on 21 March 2020
9
+ by Durgesh Pachghare
6
10
*/
7
11
8
12
#include < Servo.h>
@@ -14,26 +18,32 @@ int val; // variable to read the value from the analog pin
14
18
int startpos = 10 ; // angle from which the servo wiper starts
15
19
int endpos = 145 ; // angle till the servo wiper rotates
16
20
17
- void setup ()
21
+ void setup ()
18
22
{
19
23
myservo.attach (9 ); // attaches the servo on pin 9 to the servo object
20
- pinMode (button_pin,INPUT); // declares the pin as digital input
24
+ pinMode (button_pin, INPUT_PULLUP); // declares the pin as digital input
21
25
myservo.write (startpos); // Initialize the servo wiper to start angle
26
+ pinMode (13 , OUTPUT);
22
27
}
23
28
24
- void loop ()
29
+ void loop ()
25
30
{
26
- if (digitalRead (button_pin)) // Start the servo if the button is pressed and complete the entire rotation regardless of button state later
31
+ // The button_pin is declared as INPUT_PULLUP which means switch logic is inverted.
32
+ // It gives LOW when the switch is Pressed and HIGH when it is open
33
+ if (digitalRead (button_pin) == LOW) // Start the servo if the button is pressed and complete the
34
+ // entire rotation regardless of button state later
35
+ {
36
+ digitalWrite (13 , HIGH); // status that wiper servo is activated
37
+ for (int pos = startpos; pos <= endpos; pos += 1 ) // goes from starting angle to end angle in steps of 1 degree
38
+ {
39
+ myservo.write (pos); // tell servo to go to position in variable 'pos'
40
+ delay (5 ); // waits 15ms for the servo to reach the position
41
+ }
42
+ for (int pos = endpos; pos >= startpos; pos -= 1 ) // goes from end angle to start angle again
27
43
{
28
- for (int pos = startpos; pos <= endpos; pos += 1 ) // goes from starting angle to end angle in steps of 1 degree
29
- {
30
- myservo.write (pos); // tell servo to go to position in variable 'pos'
31
- delay (15 ); // waits 15ms for the servo to reach the position
32
- }
33
- for (int pos = endpos; pos >= startpos; pos -= 1 ) // goes from end angle to start angle again
34
- {
35
- myservo.write (pos); // tell servo to go to position in variable 'pos'
36
- delay (15 ); // waits 15ms for the servo to reach the position
37
- }
44
+ myservo.write (pos); // tell servo to go to position in variable 'pos'
45
+ delay (5 ); // waits 15ms for the servo to reach the position
38
46
}
47
+ }
48
+ digitalWrite (13 , LOW); // status that wiper servo has stopped
39
49
}
0 commit comments