#include<Servo.h>
Servo Servo;
int a=A0;
int led1G=11; // Led1G is the Green led that represents open or unlocked.
int led2R=12; // Led2R is the Red led that represents closed or locked.
int potpin = 0; // analog pin used to connect the potentiometer.
int val; // variable to read the value from the analog pin.
void setup() {
// put your setup code here, to run once:
pinMode(a, INPUT);
pinMode(led1G, OUTPUT);
pinMode(led2R, OUTPUT);
Serial.begin(9600);
Servo.attach(9);
}
void loop() {
int value= analogRead(a);
int v=map(value, 0,1023, 0,255);
Serial.println(v);
if (v<=107)
{
digitalWrite(led2R, HIGH);
digitalWrite(led1G, LOW); // From 0-107 the door is locked and red led is on.
}
else if(v<=255 and v>107)
{
digitalWrite(led1G, HIGH);
digitalWrite(led2R, LOW); // From 107-255 the door is unlocked and greed led is on.
}
else
{
}
{val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 255); // scale it to use it with the servo (value between 0 and 1255)
Servo.write(val); // sets the servo position according to the scaled value
delay(50);
}
}