Button not always responding when pressed

#include <Servo.h>
Servo Myservo;
int pos=0;
void setup()
{
Myservo.write(169);
pinMode(2,INPUT);
Myservo.attach(3);
}

void loop()
{for(pos =60; pos <= 169; pos += 1)
{
if(digitalRead(2)==LOW){
Myservo.write(60);

}}
for(pos = 169; pos>=60; pos-=1) {
if(digitalRead(2)==LOW){
Myservo.write(169);

}}
}

In the setup() function, you possibly need to set the mode to INPUT_PULLUP instead of INPUT, but without seeing a schematic or wiring diagram, it is all guesswork. I hate playing guessing games.

That is because of the floating input. See this Arduino button tutorial for detail and how to solve it

It might not be the lack of pullup; a missing pullup may result in false positives, ie thinking it's pressed when it's not, but a press will still be seen as a press, and the op's subject is about presses not being seen.

I'm thinking it's perhaps to do with the stage in the for() at which the button gets pressed and released. If you pressed the button towards the end of the first for() and it started moving to 60, if the button's still held when it gets into the second for() it will abort that and start moving to 169.

No idea what those for()s are doing btw. What are you actually trying to do?

@Poster

1. Check that your setup agrees with Fig-1.
(1) Button K1 is connected with DPin-2. Internal pull-up resistor (Rip) is enabled. N0 external pull-down or pull-up resistor is to be connected at DPin-2.

(2) Servo Motor (SG-90) is connected with DPin-3.

SG90-1.png
Fig-1:

2. Upload the following sketch (a variant of your sketch) (tested on UNO)

#include <Servo.h>
Servo Myservo;
int pos = 0;

void setup()
{
  Myservo.attach(3);
  Myservo.write(0);     //initial test position at 0 degree
  delay(5000);            //test time
  Myservo.write(169); //test position at 169 degree.
  delay(1000);           //test time
  pinMode(2, INPUT_PULLUP);    //DPin-2 works as input with internal pull-up connected
}

void loop()
{
  for (pos = 60; pos <= 169; pos += 5)
  {
    if (digitalRead(2) == LOW)
    {
      Myservo.write(60);
    }
    else
    {
      Myservo.write(pos);
      delay(100);
    }
  }
  for (pos = 169; pos >= 60; pos -= 5)
  {
    if (digitalRead(2) == LOW)
    {
      Myservo.write(169);
    }
    Myservo.write(pos);
    delay(100);
  }
}

3. Check that the shaft of the SG-90 stepper motor is moving to-and-fro.
4. Press K1 switch and check that the MCU responses to button press and the shaft of the motor rotates by 600.
5. Repeat the process.
6. Compare the codes of the given sketch with your sketch and follow the modifications in the logic of the program.

SG90-1.png

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: