Programming a servo motor

.

servo30x5.ino (514 Bytes)

Here is my code:

#include <Servo.h>
Servo servo1;
int pos1 = 150;
bool firstTime = true;
int x = 5;
int angle = 30;

void setup() 
{
servo1.attach(9);
}

void loop() 
{
if (firstTime) {
firstTime = false;
for (int i = 1; i<=(x); i=i+1){
for(pos1 = (90-angle); pos1 < (90+angle); pos1 += 1)
{
 servo1.write(pos1);
 delay(10);
}
for(pos1 = (90+angle); pos1 >= (90-angle); pos1 -= 1)
{
 servo1.write(pos1);
 delay(10);
}
}
}else{
 digitalWrite(servo1.attach(9),LOW);
}
}

To read button;
in void setup:

pinMode([the pin you connect to the button with a pull-down], INPUT)

and to read if the button is pressing (in void loop):

if(digitalRead([pin you connected the button])) {
  servo.write([where it will stop]); //to stop servo
}

I have to program a servo motor with Arduino

Do you really have a programmable servo motor? Not that it really matters, the Arduino can't program it.

You CAN program the Arduino to make a servo motor move.

So, the servo has to stop turning when it reaches the x amount of twists, OR when you push the stop-button.

If you have a continuous rotation "servo", it is NOT a servo. You have NO control over the position. All that you can control is the speed of the motor.

#include <Servo.h>
Servo servo1;
int pos1 = 150;
bool firstTime = true;
int cycli = 5;
int x = 30;

void setup() 
{
 servo1.attach(9);
 pinMode(2, INPUT);
}

void loop() 
{
  if (firstTime) {
  firstTime = false;
  for (int i = 1; i<=(cycli); i=i+1){
  for(pos1 = (90-x); pos1 < (90+x); pos1 += 1)
  {
    servo1.write(pos1);
    delay(10);
  }
  for(pos1 = (90+x); pos1 >= (90-x); pos1 -= 1)
  {
    servo1.write(pos1);
    delay(10);
  }
  }
  }
  if(digitalRead(2)) {
    servo1.write(pos1);
  }
  else{
    digitalWrite(servo1.attach(9),LOW);
  }
}

Like this?

I tried it but it still doesn’t work. The servo does what I programmed before (turn to the given angle for a x amount of times), but when I press the button the servo doesn’t stop

PaulS:
Do you really have a programmable servo motor? Not that it really matters, the Arduino can't program it.

You CAN program the Arduino to make a servo motor move.
If you have a continuous rotation "servo", it is NOT a servo. You have NO control over the position. All that you can control is the speed of the motor.

I don't understand what you are saying. I have written a code that turns the servo from 60 to 120 degrees and back and it works. Doesn't that mean that I have control over the position?

I have written a code that turns the servo from 60 to 120 degrees and back and it works.

When you talked about "turns" I thought you were talking about the servo rotating completely around that number of times. Now, I see that you are referring to turning back and forth some number of times.

PaulS:
When you talked about "turns" I thought you were talking about the servo rotating completely around that number of times. Now, I see that you are referring to turning back and forth some number of times.

Yes, I have to turn the clamp -30 and +30 degrees (it has to go clock- and counter clockwise)

Hi EEFAFCA, welcome to the forum.
A small note - when you are writing your code, auto format. CTRL-T on windows boxes. This makes the code much easier to read.

What do you want to happen if the user pushes the button? Simple stop where it is?

An easy way to do that would be to put in an infinite loop if you detect the button push.
Something like this (untested) snippet.

 if (digitalRead(2)) {
    while (1)
    {
    }
  }

Edit: I should say easy but not elegant.
A more elegant solution would use a state machine to define what should be happening in the various states (maybe processingloops and done). Go google arduino state machines.
Also google for Robin2's Demonstration code for several things at the same time.

An easy way to do that would be to put in an infinite loop if you detect the button push.
Something like this (untested) snippet.

But, you need to put that code inside both for loops nested in the outer for loop.

Far better is to remember that loop() loops, and let it handle all the looping. On any iteration, it may, or may not, be time to make the servo move a little. If it is, the servo may, or may not, need to move, because it has completed all the moving necessary, and it may need to move clockwise or counterclockwise.

All these things are easy to keep track of, so you only need to concern yourself with "is the stop switch pressed"? If it is, set the variables to values that mean that all the moving is done.

You'll need to define what stop means, when loop() will iterate over and over. Your code now will wave the servo back and forth n times.

Then loop() will get called again, and you'll wave the servo back and forth another n times.
Then loop() will get called again, and you'll wave the servo back and forth another n times.
Then loop() will get called again, and you'll wave the servo back and forth another n times.
Then loop() will get called again, and you'll wave the servo back and forth another n times.
Then loop() will get called again, and you'll wave the servo back and forth another n times.
Then loop() will get called again, and you'll wave the servo back and forth another n times.

Hi vinceherman, thank you for your response!

When I push the button, I want that the clamp moves to his 0-position and then stops. The machine goes from 90 to 60 degrees, from 60 to 120 degrees and back to his 0-position (90 degrees, in the middle). In the machine, a solar cell will be clamped. This solar cell will be tested on (torsion)fatigue. When the machine stops, you have to do measurements and therefore it is important that the solar cell is 'flat', if you know what I mean.

When I push the button, I want that the clamp moves to his 0-position and then stops.

We are talking about servos, not clamps. Discuss what the servo needs to do, not what whatever the servo is controlling needs to do.

In the machine, a solar cell will be clamped. This solar cell will be tested on (torsion)fatigue. When the machine stops, you have to do measurements and therefore it is important that the solar cell is 'flat',

So, pressing the switch is NOT supposed to immediately stop the servo. It should stop the servo from cycling, after completing the current cycle. That is considerably different from how you first described what you wanted to have happen.

It's clear to you but if you want others to be able to help you then you need to not only make it clear to them but also make it clear that you're not making those mistakes out of a misunderstanding of how things work. Programming is an exacting science. If you think Paul is being tough on you then the compiler is going to probably make you cry.

Slow down and try to be careful what and how you say. I know you feel like you're being picked on but it really is a lesson that will help you in many facets of life besides programming.

Also keep in mind that if you are really that butt hurt by people trying to help you for FREE then you are always welcome to just leave and learn it all by yourself.

Ok cool!