help with servo project !

I am very new at this.

I have a project on the go where I need my servo motor to be continuously sweeping when a button is pushed down. Once the button is let go of I need the servo to stop. While this is going on I need to have a LCD that is displaying the current angle of rotation (1-180) total number of degrees swept and the number of complete 180 degrees cycles, there are some other requirements, but this is the main issue.

I haven't been able to come up with anything that actually works.

ANY assistance at all would be appreciated.

I have now gotten the code to this. Only problem is the script runs whether the servo is actually moving or not.
anyone know how to solve this issue?

#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{

Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
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(15);
Serial.println(“Current Angle”);
Serial.print(pos); // waits 15ms for the servo to reach the position
}
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); // waits 15ms for the servo to reach the position
}

}

you have no check for a buttonpress in your whole code, that's why it just keeps on going. just add a statement for a buttonpress in the main loop and it should work.

void loop() {
   if (digitalRead(pin) == HIGH) { 
      //whole loop code
   }
}

this should work

thanks for the help. I have it now where the program will now wait for you to push the button and then being it’s cycle through.

my problem now is to figure out how to get it to stop when the button is released. so example, I push the button, it starts counting 1 to 180 and then I release it at 59. How do I get the program to stop there, so that my LCD will display thats where the servo is stopped. I guess I just need to know the code for telling the Servo to ‘stop’ or ‘rest’ right?

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo                 
int pos = 0;    // variable to store the servo position   
int pin = 10;
void setup() 
{ 
  pinMode(pin, INPUT);
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
if (digitalRead(10) == HIGH)
 { 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                     
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); 
    Serial.println("Current Angle");
    Serial.print(pos);    // waits 15ms for the servo to reach the position 
  } 
  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);    // waits 15ms for the servo to reach the position 
  } 
}
}

The servo will stop moving when you stop telling it to move.

At the moment, your code checks the button, and if it's pressed, starts the whole moving routine, without checking again until the routine is finished.

the servo itself stops moving.

im refering to the actual display that i have showing in the monitor.

the display shows 0 and when i push the button it goes through the whole 0-180 and then 180-0, whether or not the servo is actually moving.

So I can push the button for two seconds and then the servo stops, but the monitor still displays the 0-180 and back again. i am trying to find a way to freeze the display whenever the actual servo stops moving.

I guess what I want, is a way to interupt the code as soon as the button is released.

is that possible?

I guess what I want, is a way to interupt the code as soon as the button is released.

What you're looking for is a "while" loop.