How can I add a push button to my servo sweep code?

Hi,
I'm new to Arduino. I want to have a servo operate a mechanism every 30 seconds. I have achieved this already by modifying the example Servo Sweep code, but my servo was too weak. I've bought a slightly more powerful servo. When I run my code the servo just rotates constantly. The movement is too fast The new one is a continuous servo, the previous one was a 180-degree job. I'd be happy for the servo to rotate but I want to slow the movement down to get a rotation every 30 seconds. I've been banging my head on the keyboard trying to modify that code without success so far. Can you help me, please?

I've arranged a discreet 5v power supply for my servo in case that was the problem. My board is an Elegoo Uno R3

Many thanks,

Neil Ives

Do You think it could help us helping You if You attach the code? Read the topics like "How to use Forum", "How to attach...."

You should be able to control the speed of the continuous servo.

You are right to use an external supply even though so many tutorials use the
Arduino supply.

NeilIves:
I’ve been banging my head on the keyboard trying to modify that code without success

Possibly because “that code” is for a standard servo not a motor gearbox.

The only speed control you have for a continuous servo is the write() command. The Sweep technique does nothing at all. Basically write(90) is stop, 100 is slow, 150 is faster, 180 is fastest. But be aware that many continuous rotation servos have a very poor range of speed control so you may not be able to get yours to run very slowly.

Steve

Thank you for the replies. This is my first Arduino project. I think I will need to buy a 180 degree servo and a book on programming the board.

PS., Can you see that those replies were not really very useful to a complete beginner?

PS., Can you see that those replies were not really very useful to a complete beginner?

It is up to you to say what, in the reply, you are not able to understand.

If you need clarification, you must ask for it.

You always need to supply the information requested of you as we are trying to understand the situation.

We need to see your documentation, a schematic, your sketch, and an 'exact' explanation of what you are having trouble with.

All the replies given so far are appropriate.

We helpers are often getting a bit entusiastic and the replies soon covers more then the question asks for. Read and filter out what looks like useful. I've also been adking questions.....

larryd:
It is up to you to say what, in the reply, you are not able to understand.

If you need clarification, you must ask for it.

You always need to supply the information requested of you as we are trying to understand the situation.

We need to see your documentation, a schematic, your sketch, and an 'exact' explanation of what you are having trouble with.

All the replies given so far are appropriate.

You're perfectly right.

Connect your continuous servo to pin 9 and try this simple test sketch, I wouldn’t expect the speed to be very accurate or stable.

/*
 Try this test sketch with the Servo library to see how your
 servo responds to different settings, type a position
 (0 to 180) or if you type a number greater than 180 it will be
 interpreted as microseconds(544 to 2400), in the top of serial
 monitor and hit [ENTER], start at 90 (or 1472) and work your
 way toward zero (544) 5 degrees (or 50 micros) at a time, then
 toward 180 (2400). 
*/
#include <Servo.h>
Servo servo;

void setup() {
  // initialize serial:
  Serial.begin(9600); //set serial monitor baud rate to match
  servo.write(90);
  servo.attach(9);
  prntIt();
}

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 pos = Serial.parseInt();
    pos = constrain(pos, 0, 2400);
    servo.write(pos);
    prntIt();
  }
}
void prntIt()
{
  Serial.print("  degrees = "); 
  Serial.print(servo.read());
  Serial.print("\t");
  Serial.print("microseconds =  ");
  Serial.println(servo.readMicroseconds());
}

Thank you JCA34F,

I don't fully understand what's happening but a value of 93 has the servo quietly turning at about 4 seconds per revolution.

The speed is not really the problem. I want the servo to operate, (a full rotation would be ok) every 30 seconds. Can you tell me a way to have the servo wait for a period and then resume?

I've read that these boards do not have a very accurate timing circuit and I may need to get a clock module for the board.

Neil

Look at the blink without delay example. Take a copy and make it flash the LED every thirty seconds rather than the 0.5Hz it does now.

Then instead of blinking the led, at that point in the code, set your servo moving, delay a bit and stop it. For quick results use the delay function for this bit. Then fix it to use millis.

After experimenting with JCA34F’s code I found that this is all that’s needed to get the servo making a rotation every 4 seconds

#include <Servo.h>
Servo servo;

void setup() {
servo.attach(9);

}
void loop() {
}

Where can I insert a pause/delay? I’ve tried putting delay(1000); in the loop as an experiment, but with no success.

I'm giving this idea up!
I had what I wanted with a 9g 180° servo using a modified version of the Sweep example. I just needed a bit more oomph. I did not realise that things would get so tricky with a 360° servo. I've ordered a more butch 180° servo. If that doesn't work, "I'll be back".

Many thanks to all who tried to help me.

Neil Ives

It's not that difficult. If you want the servo to move for a while and then stop you need to use JCA34F's code to find the value that causes it to stop moving. It should be somewhere close to 90. Let's guess it might be 88.

Then you want your loop() to contain

void loop() {

  servo.write(93);  // start servo moving slowly
  delay(4000);       // this is how long you want to move for
  servo.write(88);  // the 88 is whatever value stops the servo
  delay(30000);    // wait for 30 seconds then loop round again
}

Steve

Steve, your code combined with JCA34F’s code gets me close to what I want. Thank you. The only thing now is getting the servo to stop at the same place each turn or at least a jump back to a restart position. Is it possible to send a servo position command?
Neil

NeilIves:
Steve, your code combined with JCA34F's code gets me close to what I want. Thank you. The only thing now is getting the servo to stop at the same place each turn or at least a jump back to a restart position. Is it possible to send a servo position command?
Neil

No.

No, not with a CONTINUOUS ROTATION servo. I guess it's time to get that 180 degree servo (and some different code).

You see all we can do is help you to get to what you SAY you want. If you keep changing your mind or adding new requirements that you didn't previously mention then it may all have been wasted effort.

Steve

My first post describes what I'm trying to achieve. I haven't changed my requirements.

I've ordered a 180° servo.

Thank you all again.

I'm familiar with programming in Basic. I feel that using C/C++ was not a great choice for beginners using Arduino.

1 Like

I think you need to understand what is being explained here. There are two distinct sorts of "servos" available.

The first is the true servo - you send it a command (pulse width) corresponding to the position you want it to assume. It contains a potentiometer on its output shaft which allows it to determine the position and its control circuit compares that with the position that it is commanded to take and controls the motor to move in whichever direction is necessary to move to the position specified.

The second sort which you have unwittingly obtained is not a "360°" at all. It has no control of the output position at any point. To the extent that it is a "servo", it allows you to determine not the position at all but the speed of the movement. For this reason it is referred to as a "continuous rotation" servo, not a "360°" (because its movement is infinitely more than 360°).

Now a trick which is used when one - for whatever reason - needs a continuous rotation servo is to remove the actuating arm of the internal potentiometer in a common servo, leaving it set to the middle of its range. It "thinks" its arm is always set to the middle position and if commanded to move away from that position, it starts the motor in a futile effort to move the phantom "arm" to the new position, how fast it does so depending in proportion to how far the command is from the apparent centre position.

There are some actual uses for this, one being to directly implement the driving whee(s) of a lightweight vehicle.