About The Loop

Hello everybody,
Couple days I am stuck with this “problem”:slight_smile: I wrote I simple code to understand better what I mean.

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
int pos = 0; 
int ledPin = 12;                // LED is connected to pin 12
int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status


void setup() {
  pinMode(ledPin, OUTPUT);      // Set the LED pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  myservo.attach(9);             // attaches the servo on pin 9 to the servo object 
}


void loop(){
  val = digitalRead(switchPin);   // read input value and store it in val
  if (val == LOW) {               // check if the button is pressed
  digitalWrite(ledPin, HIGH);     // turn LED on
  myservo.write(90);              // servo go 90 degrees
  }
  if (val == HIGH) {              // check if the button is not pressed
  digitalWrite(ledPin, LOW);      // turn LED off
  
  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);                       // 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);                       // waits 15ms for the servo to reach the position
  } 
 }
}

So how you see: I want that when I press the button servo goes from 0 to 180 degrees back and forth. When I release the button servo goes to 90 degrees. And it works so with this code. Now comes the question:
How can I modify the code that the servo immediately stops when I release the button?
How can I modify the code that the servo goes all the time back and forth., and at the same time I will push the button and the led turns on till I hold it, and when I release the button the LED turn off. But the servo in the the same time must go back and forth, without stops.
It is only a example code to understand which way is the best in this situation.
Many thanks!

How can I modify the code that the servo immediately stops when I release the button?

In the loops that move the servo, you must check whether the button is still pressed, or has been released. Break out of the loop if the button has been released.

Now, this won't get the servo to stop immediately. To do that you must get rid of all delays. Look at the Blink Without Delay example.

How can I modify the code that the servo goes all the time back and forth., and at the same time I will push the button and the led turns on till I hold it, and when I release the button the LED turn off.

The Blink Without Delay example will show you how you can make things happen without the use of delay.

You will be creating, in effect, a state machine. On each pass through loop, read the switch state. If pressed, you are in one state. If released, you are in another state.

Then, move the servo to a new position, if it is time.

Then, change the state of the LED (from on to off or from off to on), if you are in the blinking state AND it is time to change.

Thank yo very much PaulS! You show me the right way! I have changed the code so:

#include <Servo.h>
Servo myservo;
int pos = 0;
int switchPin = 2;           
int val;
void setup() {
myservo.attach(3);
}

void loop()
{
  val = digitalRead(switchPin);   
  if (val == LOW) {               
  myservo.write(90);  
  
 }
  if (val == HIGH) 
  { 
   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'
    val = digitalRead(switchPin);
    if (val == LOW)
    {
      break;
    }
    delay(10);                       // 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'
     val = digitalRead(switchPin);
    if (val == LOW)
    {
      break;
    }
    delay(10);                       // waits 15ms for the servo to reach the position
    } 

   }
 }

So now when I press the the button the servo goes back and forth till I hold the button. When I release it, the servo stops immediately. So now this part is working well!

But I really cant understand how to integrate millis to my code especially to servo. So my question "*How can I modify the code that the servo goes all the time back and forth., and at the same time I will push the button and the led turns on till I hold it, and when I release the button the LED turn off. But the servo in the the same time must go back and forth, without stops.*" Is still actual. I dont really get where should I put this time from millis to servo?

You’ve got a watch (that’s millis()), a pad of paper and a pencil (variables in the sketch), a servo, some LEDs and some switches. How would YOU light the LEDs, read the switch(es) and move the servo all at the “same” time.

Of course, you can’t do any two things at the same time, but if you do them 62.5 nanoseconds apart, it’s unlikely that you will observe that they are not happening simultaneously.

Many Thanks PaulS! I think this is my answer "you can't do any two things at the same time". Now I will think another way.

I think this is my answer "you can't do any two things at the same time". Now I will think another way.

I think it isn't. Moving a servo one degree and then turning an LED on will appear as though they are happening at the same time, even though they really are discrete events that occur in some order. As long as the interval is short enough, discrete events will appear to be simultaneous.

Like I told, now I understand that two things cant work at same time. I mean in one and the same "nanosecond" led and servo cant work together. But they can work in array for example in first xnanosecond will work the led and in the second xnanosecond the servo, in 3 led, 4 servo and so on. But we cant see it because the time in which it happens is to short. Like you told! Dont worry I get what you mean! Thanks!