I have a code that when I push a button my servo starts to sweep 0-180 then back 180-0. That part works good, my problem is that I am displaying the degree of 0-180 on an LCD display and as soon as I push the button the display does it's count 0-180 whether or not the servo is actually still moving. I am trying to get it to freeze at whatever value the servo is at the time I release the button. So I think what I need is a way to interupt the code as soon as the servo is stopped but unsure on how to accomplish that.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int pin = 3;
#include <LiquidCrystal.h>
LiquidCrystal lcd(10, 11, 7, 6, 5, 4);
void setup()
{
pinMode(pin, INPUT);
lcd.begin(16, 2);
Serial.begin(9600);
myservo.attach(2); // attaches the servo on pin 9 to the servo object
}
void loop()
{{
if (digitalRead(3) == LOW)
myservo.write(pos);
delay(15);
Serial.println("Current Angle");
Serial.print(pos);
lcd.print(pos);
delay(100);
lcd.clear();}
{ if (digitalRead(3) == HIGH)
{ for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ myservo.write(pos);
delay(15);
Serial.println("Current Angle");
Serial.print(pos);
lcd.print(pos);
delay(100);
lcd.clear();}
for(pos = 180; pos>=1; 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("Current Angle");
Serial.print(pos);
lcd.print(pos);
delay(100);
lcd.clear();}}
}}