Read button status continuously

I've been trying to figure this out for days and can't seem to find the answer. I'm new to coding and learning what I can from videos and forums. I have a continuous servo motor, Arduino Nano, and 2 buttons. I want one button to rotate the motor CW and stop when released, the other button to rotate CCW and stop when released. I found this code and it works except for the motor doesn't stop when the button is released. If I press the opposite button the motor will slow or reverse but not stop when released. I copied the code from Arduino user.
here is my code:

int button = 2; //pin of the first button
int button1 = 3; //pin of the second button
#include<Servo.h> //include the servo library
Servo servo; //create a servo object
int pos = 90; //initial position of the servo

void setup() {
// put your setup code here, to run once:
servo.attach(9); //pin used by the servo
pinMode(button, INPUT); //define first button as input pullup
pinMode(button1, INPUT); //define second button as input pullup

}

void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(button) == LOW) { //if Value read of the button ==LOW:
pos++; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
if (digitalRead(button1) == LOW) { //if Value read of the button ==LOW:
pos--; //decreases the value of the "pos" variable each time the push button of the right is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
}

Look for switch change in state not switch level.

Wire as S3 below:

Make the servo signal go to 90° to stop the servo.

Please read the sticky topics on top of the forum, how to present code using Code Tags </>.

See the StateChangeDetection example in the IDE.

To make those comments be true you have to use

Unless you have an external pull up resistor between input and +5V.
Your buttons should be wired between input pin and ground.

The big problem is that the code you found simply doesn't do that. It just moves the position of a normal servo to the left or the right for as long as you hold down the buttons. That code is not designed to be used with a continuous rotation servo.

Thanks Mike,

Can you help me with what code I need or where to find it?

A continuous rotation servo has a setting where the motor will be stopped. You have to find that point first.

It looks like you are overshooting this value probably because the value you feed to the servo change every 5mS which is stupidly fast. So first of all change the delay to 100mS, and print out the value to the serial monitor every time the pos variable is changed and find the value where it is stopped. Then report back here and we can take it from there.

Not much better. I couldn't figure out how to use monitor. After reading some of the other forums I'm starting to wonder if the issue isn't with trying to use a servo motor. as mentioned I need motor to go one direction while one button is pressed and the opposite direction when another is pressed. The distance of travel will vary. The project is an extending and retracting rod, if that helps. I'm open to other ideas on how to accomplish this. Thanks again for your help.

You move the motor, then you print to the serial monitor that value you sent to the servo . . .

Please confirm the servo is one that goes 360/continuous and not a 180° servo.

Yes, 360/ continuous. Model is FS90R.

Wonder no more, your problem is with what you are doing with the servo motor. But as you can't seem to be able to do something as simple as a Serial.println you are in dire need of learning some basics. You are trying to walk before you can crawl.

So I would advise you to look at some of the examples in the IDE. Try them, try and read what they are doing, try and make small changes and predict what will happen, then see if you are right.

Note you will have to save an example under a different name so you can change it.

1 Like

Hello bdrews
Did you post the sketch well formatted, with code comments and in code tags to see how we can help, didn´t you?
In most cases, the basics of key operation need to be rethought.
Have a nice day and enjoy coding in C++.

Try this test sketch to find the servo's stop value, should be a small range of numbers near 1500. Type a number between 544 and 2400 in top of serial monitor and click [send] or type [ENTER]. Enter the value that stops the motor in the "stopVal" variable. Enter numbers less than and greater than stopVal to increase speed forward and reverse until you find the maximum speed in both directions, no use entering speeds any higher.
Compiles on Nano but not tested!

#include <Servo.h>
Servo servo;

int stopVal = 1500;

void setup() {
  // initialize serial:
  Serial.begin(9600); // set serial monitor to match,
                     // and line ending to Newline
  servo.writeMicroseconds(stopVal);
  servo.attach(9);
}
void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int speed = Serial.parseInt(); 
    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {}
    speed = constrain(speed,544,2400);
    servo.writeMicroseconds(speed);
    Serial.println(speed);
  }
}

It is worth noting that this this data sheet data sheet on this servo says

The reset point they talk of is the motor stop point I talk of.
However this is given as a time of servo pulse rather than a position as you have used so you should use the function given here

So 1.5 milliseconds, the stop point is 1500 microseconds.

I found a code that works from RoboJax on Youtube. Had to add a 3rd button for stop, but hey i can deal with that. Thanks for all the help.

Quite simply there is no need for that. It looks like poor programming techniques. Still if you don't have any skill it is the sort of thing you have to put up with.

Thanks for the kind words Mike. My intention was never to become a professional coder. I thought that's what forums like this were for is to get help from people with more knowledge, you know to learn from others and for people like you to share their knowledge. I guess not. As far as I'm concerned my mission was accomplished. The code was completely different than what I started with.

There is a big big gap between anything you can do with an Arduino and a professional coder.

Yes this is exactly what we aim to do. But many people make the mistake of thinking that simply means writing code for you. We aim to educate you, to make you at least code literate. That means you coming to us with with a problem, some code you have written or found and us helping you to learn how to fix it.

And ask yourself what have you learned? I know, it is nothing. So in that respect both you and us have failed.
You never even tried the code you were offered, or if you did you never reported back.

You had virtually zero skill, not being able to insert a simple print statement. We hoped to at least spur you on to have a glimmer of what is going on. We do not expect you to be a become a professional coder that would take many years. But what we did hope was that you would eventually pick up enough of the basics for you to understand advice. It makes me very sad your attitude to this, and I feel you have wasted a lot of time from many people here.

2 Likes

Thanks again Mike. I actually learned quite a bit. I apologize if you feel your time was wasted.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.