#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservo; // create servo object to control a servo
volatile int state = HIGH; // value to show normal operation, anything else will cause interupt
int buttonPin = 6; // push button
int pos = 0; // variable to store the servo position
int lastValue = pos; // saves last known value for servo to display
int ledPin = 13;
int ledPin1 = 10;
int val;
void setup()
{
val = pos;
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode (buttonPin, INPUT); // sets button as an input
attachInterrupt(1, rest, FALLING); //sets interrupt to sense any change from HIGH. If it does then goes to void rest.
Serial.begin(9600); // serial monitor
lcd.begin(16, 2);
myservo.attach(7); // attaches the servo on pin 9 to the servo object
lcd.setCursor(0, 0);
}
void loop()
{
while (digitalRead(buttonPin) == HIGH) //if the button is pushed, then do normal sweep
{
for (pos = 0; pos < 180; pos += 1)
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
Serial.println(pos);
lcd.write(pos);
lcd.print("current");
delay(15);
lcd.clear();
if (pos == 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
if (pos > 1)
{
digitalWrite(ledPin1, HIGH);
}
else
{
digitalWrite(ledPin1, LOW);
}
}
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(15);
Serial.println(pos);
lcd.write(pos);
lcd.print("current");
delay(15); // waits 15ms for the servo to reach the position
if (pos == 180)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
if (pos < 180)
{
digitalWrite(ledPin1, HIGH);
}
else
{
digitalWrite(ledPin1, LOW);
}
}
}
}
void rest()
{
while (digitalRead(buttonPin) == LOW)
{
if (lastValue <= pos)
{
//Do Nothing
}
if (lastValue >= pos)
{
//Do Nothing
}
}
}