How do i stop the continous loop on servo running on a push button

I have been trying to run a servo in slow rotation from 0 to 180 by pushing a push button once and then by using the same push button, when pressed again the servo will slowly rotate from 180 to 0. I have gotten the servo to work at normal speed fine, but when I put the sweep program in it, the servo continuously keeps going from 180 to 0 without stopping.

HERE IS THE WORKING CODE

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
#define servoPin 6 //~
#define pushButtonPin 8 
int val = 0; // push value from pin 2
int angle =180;    // initial angle  for servo (beteen 1 and 179)

Servo servo3;
int buttonPushed =0;
int pos = 0; 
int lightON = 0;//light status
int pushed = 0;//push status
void setup() {
  
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin); 
   servo3.attach(5); // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin,INPUT_PULLUP);
   Serial.println(" Servo Button ");
   myservo.write(angle);//initial position
}

void loop() {
 
 val = digitalRead(pushButtonPin);// read the push button value

  if(val == HIGH && lightON == LOW){

     pushed = 1-pushed;
    delay(100);
  }    

  lightON = val;
       if(pushed == HIGH){
        Serial.println("Light ON");
        
        
    servo3.write(0);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits 15ms for the servo to reach the position
  }
      
 if(pushed == LOW){
        Serial.println("Light OFF");
        
    servo3.write(180);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits 15ms for the servo to reach the position
  }
   
      }     

HERE IS THE CODE I AM HAVING TROUBLE WITH

String readString = "";
#include "AFMotor.h"
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
#define servoPin 6 //~
#define pushButtonPin 8 
int val = 0; // push value from pin 2
int angle =180;    // initial angle  for servo (beteen 1 and 179)

Servo servo3;
int buttonPushed =0;
int pos = 0; 
int lightON = 0;//light status
int pushed = 0;//push status
void setup() {
  
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin); 
   servo3.attach(5); // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin,INPUT_PULLUP);
   Serial.println(" Servo Button ");
   myservo.write(angle);//initial position
}

void loop() {
  while (Serial.available()) {
char c = (char)Serial.read();
readString += c;
}

 
 val = digitalRead(pushButtonPin);// read the push button value

  if(val == HIGH && lightON == LOW){

     pushed = 1-pushed;
    delay(100);
  }    

  lightON = val;
       if(pushed == HIGH){
          
         for (int i = 0; i<110; i++){
     servo3.write(i);  // waits 15ms for the servo to reach the position
     delay(10); 
  }
  }
       }   
 if(pushed == LOW){
    
    for (int j = 110; j>0; j--){
    servo3.write(j);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
  }
      }   
      
     
       }

CAN SOMEBODY PLZ HELP. I'M NEW TO ARDUINO

edit your post. put code in code-tags.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

sorry for the inconvenience. i made the code tag

Hello thamaraw

Welcome to the worldbest Arduino forum ever.

What is going wrong ?

You seem to be unaware of some very basic things that you should learn and to take care of.

you should use pressing ctrl-T for autoformatting your code

In C++ all code must be inside of functions.
Your "not working code" has a part that is outside of function loop
This code does not even compile.
So if you describe the behaviour as "continually sweeping" the cause might be that compiling your latest code failed and was aborted and some earlier code ist still running

In C++ each opening curly brace must have a corresponding closing curly brace

I have commented all your braces with letters "A,B,C,D,E ....
to make you see this

If you use ctrl-t for autoformatting you will see that there are three curly braces most left
and that your if-condition is below the most left closing brace

String readString = "";
#include "AFMotor.h"
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
#define servoPin 6 //~
#define pushButtonPin 8
int val = 0; // push value from pin 2
int angle = 180;   // initial angle  for servo (beteen 1 and 179)

Servo servo3;
int buttonPushed = 0;
int pos = 0;
int lightON = 0;//light status
int pushed = 0;//push status

void setup() {

  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin);
  servo3.attach(5); // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin, INPUT_PULLUP);
  Serial.println(" Servo Button ");
  myservo.write(angle);//initial position
}

void loop() { //A
  while (Serial.available()) { // B
    char c = (char)Serial.read();
    readString += c;
  } //B


  val = digitalRead(pushButtonPin);// read the push button value

  if (val == HIGH && lightON == LOW) { // C

    pushed = 1 - pushed;
    delay(100);
  } //C

  lightON = val;
  if (pushed == HIGH) { //D 

    for (int i = 0; i < 110; i++) { //E
      servo3.write(i);  // waits 15ms for the servo to reach the position
      delay(10);
    } //E
  } //D
} // A XXX curly brace that marks END function loop()

// outside any function
if (pushed == LOW) { // F

  for (int j = 110; j > 0; j--) { // G
    servo3.write(j);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  } // G
}// F 

} // no opening brace

}  // no opening brace

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

thank you for the advice. I made the adjustments and removed unnecessary parts from the code. but it is still running continuously on the first IF when given power even without pressing button.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
#define pushButtonPin 8 
int val = 0; // push value from pin 2
int angle =180;    // initial angle  for servo (beteen 1 and 179)
Servo servo3;
int buttonPushed =0;
int pos = 0; 
int lightON = 0;//light status
int pushed = 0;//push status
void setup() {
   servo3.attach(5); // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin,INPUT_PULLUP);
   Serial.println(" Servo Button ");
}

void loop() {
 
 
 val = digitalRead(pushButtonPin);// read the push button value

  if(val == HIGH && lightON == LOW){

     pushed = 1-pushed;
    delay(100);
  }    

  lightON = val;
       if(pushed == HIGH){
  
         for (int i = 0; i<110; i++){
     servo3.write(i);  // waits 15ms for the servo to reach the position
     delay(10); 
  }
  }
         
 if(pushed == LOW){
    for (int j = 110; j>0; j--){
    servo3.write(j);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
  }
      }   
      
     
      


it goes from 0 to 110 again and again without stopping. can somebody please tell me how I can make the servo go from 0 to 110 slowly once when the button is pressed once , and go from 110 to 0 slowly when pressed again?

What does this do ?

pushed = 1-pushed;


Highly suggest you handle your switches by looking at a switch change in state rather than looking at switch levels.

#include <Servo.h>
Servo servo3;

const byte pushButtonPin = 8;

int angle = 180;   // initial angle  for servo (beteen 1 and 179)
int pos = 0;
bool buttonPushed = 0;
bool lightON = 0;//light status
bool pushed = 0;//push status

void setup() {
  servo3.attach(5); // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin, INPUT_PULLUP);
  Serial.println(" Servo Button ");
}

void loop() {
  if (digitalRead(pushButtonPin) == LOW) {
    if (pos == 0) {
      for (int i = 0; i < 110; i++) {
        servo3.write(i);  // waits 15ms for the servo to reach the position
        delay(10);
      }
      pos = 110;
    }
    else {
      for (int j = 110; j > 0; j--) {
        servo3.write(j);              // tell servo to go to position in variable 'pos'
        delay(10);                       // waits 15ms for the servo to reach the position
      }
      pos = 0;
    }
  }
}
1 Like

thank you for fixing my code. but I want this code to open and close a door. when pushed once the door will open slowly and stay open. then when the button is pushed again the door will close slowly and stay closed. is this possible? can you please help me. i have been working on this for days.

did you tried not pressing the button a while ?

yes

in your code it comes back again. can you make it stay there after going to that position

probably you connect the button wrong. draw a schematic how you made it.

1 Like

here is a simulation:

when pressed, the button sends a input signal. that's just it.

the button does work. it's the code I am having trouble with. i tested it with a LED and it works fine.

Taking a guess here. If you move the statement:

Outside of the “if . . . else” block you may have success. Put it at the bottom of “loop”.

EDIT: No, that won’t work. Bad idea.

ok fixed one problem. now after going to 110 it stays there. but when the button is pushed again. it just comes directly to 0 and again slowly comes to 110. I think the code is only making the servo go 0 to 110 but not the other way

const byte pushButtonPin = 8;
#include <Servo.h>
int angle = 180;   // initial angle  for servo (beteen 1 and 179)
int pos = 0;
bool buttonPushed = 0;
bool lightON = 0;//light status
bool pushed = 0;//push status
Servo servo3;
void setup() {
  servo3.attach(5); // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin, INPUT_PULLUP);
  Serial.println(" Servo Button ");
}

void loop() {
  if (digitalRead(pushButtonPin) == LOW) {
    if (pos == 0) {
      for (int i = 0; i < 110; i++) {
        servo3.write(i);  // waits 15ms for the servo to reach the position
        delay(10);
      }
      pos = 110;
    }
    else {
      for (int j = 110; j > 0; j--) {
        servo3.write(j);              // tell servo to go to position in variable 'pos'
        delay(10);                       // waits 15ms for the servo to reach the position
      }
      pos = 0;
    }
  }
  pos = 110;
}