Need help, My code is not working

Hello everyone, I want to turn a servo for certain number of times then stop. I was able to write a code that works but when I add a button, that is the servo should turn only when the button is pressed, this to be hard for me to figure out to write it correctly.

The code below(without button) works fine

#include <VarSpeedServo.h>

VarSpeedServo myservo;

const int servoPin = 9;

int count = 0;
int readcount;

void setup() {
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  myservo.write(0, 255, true); // set the intial position of the servo, as fast as possible, wait until done
 
}



void loop() {

    count++;
     readcount = count;

     myservo.write(180, 100, true);      // move the servo to 180, max speed, wait until done
      // write(degrees 0-180, speed 1-255, wait to complete true-false)
      myservo.write(0, 100, true);         // move the servo to 180, slow speed, wait until done
   
    
    if (readcount == 3)
    {
      myservo.detach();
      count = 0;
    }
  }

This is the not working code

#include <VarSpeedServo.h>

VarSpeedServo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created
int Button_pin3 = 8;
const int servoPin = 9; // the digital pin used for the servo
int Button_Pressed3;
int count = 0;
int readcount;

void setup() {
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  myservo.write(0, 255, true); // set the intial position of the servo, as fast as possible, wait until done
  pinMode(Button_pin3, INPUT);
}

void loop() {
  Button_Pressed3 = digitalRead(Button_pin3);
  count++;
  readcount = count;
  if (Button_Pressed3 == HIGH)
  {
    if (pressBt == true)
    {
      myservo.write(180, 100, true);      // move the servo to 180, max speed, wait until done
      // write(degrees 0-180, speed 1-255, wait to complete true-false)
      myservo.write(0, 100, true);         // move the servo to 180, slow speed, wait until done
      pressBt = false;
    }
    
    if (readcount == 3)
    {
      myservo.detach();
      count = 0;
    }
  }
}

Thanks

You don't state what type of servo you're using, but this: "I want to turn a servo for certain number of times then stop" gives the impression that it's a continuous-rotation servo, not a standard one. Is this true?

its the standard one that goes from 0 to 180, I just wont it to go back and forth for a certain number of times when I press a button.

Instead of saying "turn", perhaps "move back and forth" would have been a better choice of words. :slight_smile:

now, to the code:-

You use this:-

if (pressBt == true)

yet 'pressBt' is declared nowhere.

Sort that out first.....

And why is "Button_pin3" given that name, since it's the only button and is actually on pin 8 anyway.
It's a good idea to use appropriate labels that convey an idea of the purpose of the variable/pin.
"runButton" perhaps?

It makes it much easier when asking others to help.

I only forgot to post with the code while copying from ide, but this is it again.

#include <VarSpeedServo.h>

VarSpeedServo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created
int Button_pin3 = 8;
const int servoPin = 9; // the digital pin used for the servo
int Button_Pressed3;
int count = 0;
int readcount;
bool pressBt = true;

void setup() {
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  myservo.write(0, 255, true); // set the intial position of the servo, as fast as possible, wait until done
  pinMode(Button_pin3, INPUT);
}

void loop() {
  Button_Pressed3 = digitalRead(Button_pin3);
  count++;
  readcount = count;
  if (Button_Pressed3 == HIGH)
  {
    if (pressBt == true)
    {
      myservo.write(180, 100, true);      // move the servo to 180, max speed, wait until done
      // write(degrees 0-180, speed 1-255, wait to complete true-false)
      myservo.write(0, 100, true);         // move the servo to 180, slow speed, wait until done
      pressBt = false;
    }
    
    if (readcount == 3)
    {
      myservo.detach();
      count = 0;
    }
  }
}

OK, you say "it's not working".
You need to be more specific.
Do you mean that nothing happens at all, and that the servo never moves?
Or does it move back and forth once then stop?

You might want to clean things up with better chosen names and comment this a bit more appropriately, too. What's the point of comments when they're wrong? You should make it as easy as possible for people to help you if you really want help.

myservo.write(0, 100, true);         // move the servo to 180, slow speed, wait until done

Or does it move back and forth once then stop?

Yes it moved back and forth just once then stops

Sorry the comments are from the original sample code, I am just too lazy to re-write them and I did a lot of copy and paste or simply put 're-using', hence the mismatch labels. Please forgive me on that.

Thanks

ati105:
Yes it moved back and forth just once then stops

Now how did I guess that, I wonder?

Sorry the comments are from the original sample code, I am just too lazy to re-write them and I did a lot of copy and paste or simply put 're-using', hence the mismatch labels. Please forgive me on that.
Thanks

If you're too lazy to make it easy for people to help you, you'll find that people are too lazy to help. Please make an effort in future.

This, of course, means that nothing will happen if 'pressBt' is false:-

if (pressBt == true)

Yet you make it false after the first pass and never make it true again.
Of course nothing can happen after the first pass.

As written the program will only sweep the servo whilst the button is pressed. A simple but clumsy way to make it sweep several times would be to wrap the sweep code in a for loop. Also in the current program the pressBt variable is never set back to true so the servo sweep will not happen a second time even if the button is pressed.

UKHeliBob:
As written the program will only sweep the servo whilst the button is pressed.

That's exactly what he said he wanted, Bob:-

the servo should turn only when the button is pressed,

UKHeliBob:
Also in the current program the pressBt variable is never set back to true so the servo sweep will not happen a second time even if the button is pressed.

That's what I pointed out at the end of my last reply.

And it's why I asked this in the reply before that:-

Do you mean that nothing happens at all, and that the servo never moves?
Or does it move back and forth once then stop?

(I was trying to push him to describe things more fully. Most of us aren't too good at mind-reading :smiley: )

the servo should turn only when the button is pressed,

To me that is ambiguous. Let's see what he says.

Thanks @oldesteve and @UKHeliBob its working now

@oldesteve

Yet you make it false after the first pass and never make it true again.
Of course nothing can happen after the first pass.

That was the problem, even thou I don't really need to use it, But now I know what to do when using boolean statement.

I cleaned up my comments a little, care to correct me on any mistake I make. Thanks

@UKHeliBob

As written the program will only sweep the servo whilst the button is pressed. A simple but clumsy
way to make it sweep several times would be to wrap the sweep code in a for loop. Also in the
current program the pressBt variable is never set back to true so the servo sweep will not happen a
second time even if the button is pressed.

It worked, I did use that for the arduino servo library sample code but didn't work and 'cause there are too much of delays which is bad for what I am working.
I thought the forloop won't work at all, reason why I decide to use counter instead.

Here's the complete code for anyone interest (newbie like)

#include <VarSpeedServo.h>

VarSpeedServo myservo;  // create servo object to control a servo
                        // a maximum of eight servo objects can be created

int Button_pin8 = 8;      // the digital pin used for Button_pin8
const int servoPin = 9;   // the digital pin used for the servo

int Button_Pressed8;      // State read for Button_pin8

void setup() {
  myservo.attach(servoPin);     // attaches the servo on pin 9 to the servo object
  myservo.write(0, 255, true);  // set the intial position of the servo, as fast as possible, wait until done
  pinMode(Button_pin3, INPUT);
}

void loop() {
  Button_Pressed8 = digitalRead(Button_pin8);
 
  if (Button_Pressed8 == HIGH)
  {
    for (int a = 0; a <= 5; a++)  // move servo for "a" number of times
    {
      myservo.write(180, 100, true);      // move the servo to 0, speed(100), wait until done
      myservo.write(0, 100, true);         // move the servo to 180, speed(100), wait until done
      pressBt = false;
    }
  }
}

Comments :

The code won't compile because pressBt and Button_pin3 are not declared.

int Button_pin8 = 8;      // the digital pin used for Button_pin8
const int servoPin = 9;   // the digital pin used for the servo

Both of these variables could be declared const byte to save space. Not important here but it is good practice to use variables of the correct type.

Also Button_pin8 is not very descriptive and a better name would be preferable. Again, not important here but a good habit to get into. Especially important when you have several buttons to deal with in a larger program. Get into good habits early and you will benefit later.

The code won't compile because pressBt and Button_pin3 are not declared.

Forgot to delete before posting, Any who needs the code can just delete pressBt and change Button_pin3 to whatever suits you.

Both of these variables could be declared const byte to save space.
Not important here but it is good practice to use variables of the correct type.

Now I know why some sketch contain const, will definitely start using it too

Also Button_pin8 is not very descriptive and a better name would be preferable.
Again, not important here but a good habit to get into. Especially important when
you have several buttons to deal with in a larger program. Get into good habits
early and you will benefit later.

Just the kind of description I am used to, I needs to start adopting a better name. Good practice, good understanding of code and good response time from experts when I need help.

Thanks.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Also show how and what you have to supply power to the servos.

Tom.... :slight_smile:

@Tom
this is the hand drawn diagram of my setup. Hope it helps.
As attachment

ati105:
@Tom
this is the hand drawn diagram of my setup. Hope it helps.
As attachment

You didn't attach it.

And I thought your code was working properly now?

Sorry the image size was to large. Shrink it.