Guys, In this project we are going to control a Servomotor by a Touch Sensor. Here a touch on the Touch Sensor will make a rotation on Servomotor of 90 degree and no touch means the Servomotor will be at zero degree. Okey, Let's see how it works.
Components Required
1. | Arduino UNO | * 1 |
2. | Touch Sensor | * 1 |
3. | Servomotor | * 1 |
4. | LED | * 1 |
5. | Resistor(220 ohm) | * 1 |
6. | Jumper Wires(Male to Male) | * 1 |
7. | Jumper Wires(Female to Female) | * 1 |
Circuit Diagram
Program
#include <Servo.h>
Servo myservo;
int pos = 0;
int in = 2;
int out = 13;
int state = HIGH;
int r;
int p = LOW;
long time = 0;
long debounce = 200;
void setup()
{
myservo.attach(9);
pinMode(in, INPUT);
pinMode(out, OUTPUT);
}
void loop()
{
r = digitalRead(in);
if (r == HIGH && p == LOW && millis() - time > debounce) {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(out, state);
p = r;
}