Go Down

Topic: Controlling servo without needing to use delay (Read 901 times) previous topic - next topic

Dane

This is an excerpt from my loop. I would prefer to have as few as possible uses of delay in my sketch. I would prefer to "detach" the servo to cause it to return to its original position (the servo arm is connected to a spring so the power of the (now) unresisted spring should enable a return). However, this doesn't seem to work.

Code: [Select]
if (bedroomVoltsRead < 100)
  {
    if (currentMillis-bedroomTriggeredTime > debounce) 
    {
      bedroomTriggeredTime = currentMillis;

      if (bedroomState == OFF)
      {
        bedroomState = ON;
        if (bedroomLEDState == OFF)
        {
          bedroomLEDState = ON;
          if(value3) 
          {
            BellControlServo.attach(6);
            BellControlServo.writeMicroseconds(2300);
            delay(200);              // wait for a bit
          }
          bedroomLEDTimeStamp1 = currentMillis;
          Serial.println();
        }
        else if (bedroomLEDState == ON)
        {

          bedroomLEDState = FLASH;
          if(value3) 
          {
            BellControlServo.attach(6);
            BellControlServo.writeMicroseconds(2300);
            delay(200);              // wait for a bit
          }
          bedroomLEDTimeStamp1 = currentMillis;
        }
      }
    }
    else
    {
      bedroomState = OFF;
      BellControlServo.attach(6);
      BellControlServo.writeMicroseconds(600);
      delay(200);              // wait for a bit
      BellControlServo.detach();
    }
  }
  if (bedroomLEDTimeStamp1 + LEDOnDuration < currentMillis)
  {
    bedroomLEDState = OFF;
  }
 


In particular I understand each time the arduino goes through the loop this bit is executed. So I have a delay of 1.2seconds every time the loop is executed even if nothing is happening because I have six of these in my loop. If something is happening the delay is potentially 7.2 seconds!

Code: [Select]
    else
    {
      bedroomState = OFF;
      BellControlServo.attach(6);
      BellControlServo.writeMicroseconds(600);
      delay(200);              // wait for a bit
      BellControlServo.detach();
    }


Any ideas?

Thanks!

Grumpy_Mike

Quote
(the servo arm is connected to a spring so the power of the (now) unresisted spring should enable a return).

Not a good idea. The servo normally goes through a worm gear and it is not a good idea to try and drive those backwards.

Quote
Any ideas?

Use the blink without delay method to write your program.

Dane


Quote
(the servo arm is connected to a spring so the power of the (now) unresisted spring should enable a return).

Not a good idea. The servo normally goes through a worm gear and it is not a good idea to try and drive those backwards.

Oh? I thought it was possible to move a servo manually through its range of rotation (for example to test its limits)? Didn't realise it was damaging...

Quote
Any ideas?

Use the blink without delay method to write your program.

OK...was hoping to avoid that as it's going to be quite complicated for my sketch.

zoomkat

Quote
Any ideas?


Not really, as you talk of code that doesn't work for you without explaining what you are actually trying to do. If you are using "hobby" servos, I've never seen one containing worm gears. Having a servo strain against a spring might not be a good idea unless the spring is used as a counter balance for some type of a load. You should also describe what function a delay in your code serves.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Dane


Quote
Any ideas?


Not really, as you talk of code that doesn't work for you without explaining what you are actually trying to do. If you are using "hobby" servos, I've never seen one containing worm gears. Having a servo strain against a spring might not be a good idea unless the spring is used as a counter balance for some type of a load. You should also describe what function a delay in your code serves.


Sorry.

If I don't include a delay after each instruction to the servo, the servo doesn't move. It doesn't seem to have time to complete the task required of it and the arduino seems to continue onin the loop and forget the command to the servo. If I include a delay the servo moves.

The delay after a command to move to Microseconds(2300) is manageable. It's the delay after Microseconds(600) that I'm keen to eliminate because this is encountered ALL the time in my sketch.

I had hoped to be able to use "detach" to return the servo to a default position given that the bell cord is lightly sprung.

Basically my  servo pulls a string which is attached to and thus rings a bell. It's important that for the bell to ring just once per request that the back and forth movement of the servo arm is conflated into one as far as the bell is concerned.

I can attach a YouTube video if this is still unclear.

Go Up