Make a servo motor stop after x times

Hello!

I have the following code:

#include <Servo.h>
Servo servo1;
const int switch1Pin = 6;    // switch input 1
const int switch2Pin = 7;    // switch input 2
const int led1Pin = 8;

int pos1 = 90;

void setup() {
    // set the switch as an input:
    pinMode(switch1Pin, INPUT); 
    pinMode(switch2Pin, INPUT);
    
    // set all the other pins as outputs:
    pinMode(led1Pin, OUTPUT); 
    pinMode(servo1.attach(9), OUTPUT); 

    // pin servomotor
    servo1.attach(9);
}

void loop() {
    // if the switch is high, motor will work
    if (digitalRead(switch1Pin) == HIGH) {
    digitalWrite(led1Pin, LOW);   // set leg 1 of the H-bridge low 
    digitalWrite(servo1.attach(9), HIGH);  // set leg 2 of the H-bridge high
       {
     for(pos1 = 60; pos1 < 120; pos1 += 1)
        {
          servo1.write(pos1);
          delay(10);
        }
      for(pos1 = 120; pos1 >= 60 ; pos1 -= 1)
        {
          servo1.write(pos1);
          delay(10);
        }
     }
  } 
    // if the switch is low, motor has to stop:
  else if(digitalRead(switch2Pin) == HIGH){
    digitalWrite(led1Pin, HIGH);   // set leg 1 of the H-bridge low
    digitalWrite(servo1.attach(9), LOW); // set leg 2 of the H-bridge low 
    }
  else {
    digitalWrite(led1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(servo1.attach(9), LOW);   // set leg 2 of the H-bridge low    
  }
}

I am programming a servo motor which turns from 60 degrees to 120 degrees and back, this for several times. I want the motor to stop using a switch. The code works the way I want (the motor works and turns off when I switch the switch button), but I want to add something and I can’t succeed…

I also want the motor to stop after x cycles. One cycle means from 60 to 120 degrees, and back from 120 to 60 degrees. I want to fill in the x amount of cycles in my program, but this doesn’t work. I also have a code where you can fill in the x amount of cycles. This code also works the way I want, the motor works for 20 cycles and then stop. This is the following code:

#include <Servo.h>
Servo servo1;
int pos1=90;
bool firstTime=true;
int cycles=20;

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

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

I tried to merge the two seperate codes into one code, but I can’t succeed… When I tried to merge them, the motor worked for the x amount of cycles, but didn’t turn off when I used the switch button. When the motor had achieved the x amount of cycles, the motor stopped working, then I switched the switch button to LOW and back to HIGH and after that, the motor kept working and didn’t stop after the x amount of cycles anymore.

So I want this:
I have a servo motor and a switch. You can fill in the x amount of cycles in the program. The motor stops working when the x amount of cycles is achieved, OR when you use the switch button (to turn the motor off).

I hope anyone can help me. I am very new to Arduino and don’t have much knowledge about programming so I am stuck!

Post your merged code.

You have a lot of code lines like this:

    digitalWrite(servo1.attach(9), LOW); // set leg 2 of the H-bridge low

The confusing comment is rubbish. But what are you actually expecting that code to do?

Steve

wildbill: Post your merged code.

I haven't saved that code because it was rubbish.

slipstick: You have a lot of code lines like this:

    digitalWrite(servo1.attach(9), LOW); // set leg 2 of the H-bridge low

The confusing comment is rubbish. But what are you actually expecting that code to do?

Steve

As I said in the first message, I need a code that turns the servo motor off when the switch is turned off OR when the motor reached the x amount of cycles. When the motor hasn't reached the x amount of cycles, you must be able to stop the motor with the switch button.

EEFAFCA: I haven't saved that code because it was rubbish.

Then have another go and if you get stuck post your best effort. (That's how all programs get written :) )

digitalWrite(servo1.attach(9), LOW); // set leg 2 of the H-bridge low

The confusing comment is rubbish. But what are you actually expecting that code to do?

As I said in the first message, I need a code that turns the servo motor off when the switch is turned off OR when the motor reached the x amount of cycles.

The purpose of the question was to ask you what that specific line of code is intended to do?

If you don't understand a line of code then don't use it.

...R

Robin2: The purpose of the question was to ask you what that specific line of code is intended to do?

If you don't understand a line of code then don't use it.

That line will turn my servo motor off. If I can't use a line of code I don't understand, than I don't have a code at all :'). I am studying Mechanical Engineering and we don't have ANY programming lessons or whatever in our curriculum so this is all new to me. I tried a lot of tutorials and tried to understand them, but I can't succeed programming the code I want... That's why I asked for help here.

EEFAFCA: That line will turn my servo motor off.

Seems very very strange. If you want to detach a servo just do it like this

servo1.detach()

If I can't use a line of code I don't understand, than I don't have a code at all

That's not true. It just means that you need to do some studying.

...R

Robin2: Seems very very strange. If you want to detach a servo just do it like this

servo1.detach()

That's not true. It just means that you need to do some studying.

...R

It may seem strange but it works and one of my teachers at school (who helped me) did this so I assume this is true.

I have done 'some' studying for the last 1,5 months and I still don't understand much, so I guess I am just dumb then...

Anyway, can someone help me with the code I want ?

You don't need to detach a servo to stop it moving. Servos are positional. They stop when they have arrived where you asked them to go to. To get them to stop you just have to not ask them to go somewhere else. Just stop sending new write()s and the servo will naturally stop moving.

We are trying to help with the code but if you mean you just want someone to write it for you then I'm afraid that's not what we do. You write the code, we help you to get it working properly.

Steve

EEFAFCA: It may seem strange but it works and one of my teachers at school (who helped me) did this so I assume this is true.

Show my comment to your teacher ?

...R

slipstick: You don't need to detach a servo to stop it moving. Servos are positional. They stop when they have arrived where you asked them to go to. To get them to stop you just have to not ask them to go somewhere else. Just stop sending new write()s and the servo will naturally stop moving.

That's not how the code (and all servos) works and I don't want the servo to stop naturally. The code makes the servo moves from 60 degrees, to 120 degrees, back to 60 degrees and this for x times (you can choose x by yourself). When the servo moved to this positions for x times, he stops. So he doesn't stop when he have arrived where I asked him to go, he stops when he did this x times.

slipstick: We are trying to help with the code but if you mean you just want someone to write it for you then I'm afraid that's not what we do. You write the code, we help you to get it working properly.

I am not asking for a written code, I am just asking for some tips or thoughts that will point me in the right direction?

I have this code now:

#include <Servo.h>
Servo servo1;
int pos1 = 90;            // start-position 90 degrees
bool firstTime = true;
const int SWITCH1 = 6;    // attach switch to pin 6

// CHANGEABLE VARIABLES 
int cycles = 10;           // amount of cycles
int angle = 30;           // torsion-angle

void GoToPosition(int hoek)
{
   servo1.write(hoek);    // send servo to angle
   delay(500);            // wait for 500 ms
}

void setup()
{
  pinMode(SWITCH1, INPUT);      // set switch as input
  servo1.attach(9);             // attach servo to pin 9
}

void loop()
{
  if (firstTime) {
    firstTime=false;
    for(int i = 1; i <= cycles; i++){
      if (digitalRead(SWITCH1) == HIGH)
        GoToPosition(90-angle);
      if (digitalRead(SWITCH1) == HIGH)
        GoToPosition(90+angle);
      if (digitalRead(SWITCH1) == HIGH)
        servo1.write(90-angle);
        }
  } else {
    servo1.detach();
  }
}

This code makes the servo do the same (turn 60-120-60 degrees) for x (in this case 10) times, UNLESS the signal from the switch is high. If I turn off the switch, the servo stops. This is almost what I want. When I turn on the switch again (so the signal is high), the servo does nothing. I want the servo to continue working when I turn on the switch. I want the servo to continue where he left off. So for example, when I turn off the switch after 6 cycles, I want the servo to continue where he left off when I turn on the switch again, so he has to run through 4 more cycles and then turn back off.

I hope my explanation is clear, I find it difficult to put it into words… :sweat_smile:

I suggest you remove the line that detaches the servo.

...R

Robin2: I suggest you remove the line that detaches the servo.

...R

Tried that but still doesn't work.. The servo still does nothing when I turn the switch back on.

I was going to add to Reply #12 but you got there first.

You are not approaching the problem in the correct way.

Just use a variable to count the number of repeats and allow loop() to do the iteration. When the variable value gets to the permitted max the servos won’t do anything more. Then when you press the button, set the variable back to 0.

Something like this pseudo code

void loop() {
   if (button is pressed) {
        repeatCounter = 0
   }
   if (repeatCounter < maxRepeats) {
       doOneServoCycle()
       repeatCounter ++
    }
}

void doOnServoCycle() {
    // code to make the servos do a cycle
}

…R

EEFAFCA: Tried that but still doesn't work.. The servo still does nothing when I turn the switch back on.

That's because where you are checking the switch doesn't stop the for loop from going round 10 times. It's just that on some of those loops it doesn't move the servo. So when you "turn the switch back on" it's already done all of its 10 loops, there's nothing left to complete.

You need to stop it counting and let it then start again later. Using your own count instead of relying on a for loop is one way of solving the problem. To stop the count you could get away with something crude like "while (button is pressed);" i.e. do nothing until the button is released.

Steve