servo controll

hi very new to this. but can anyone give me a idea how i can stop a servo at any point as it sweeps from end to end in reaction to a sensor input
thanks in advance

Just stop updating the position.

thats easy for you to say?????
servo command 0 - 70 70 - 160
its for a turret was thinking of a while and break command ???????

You gave a set of desired parameters, a correct answer was given by AWOL, and now you express dissatisfaction with the answer because the answer does not fit with your double secret parameter set. Perhaps sharing with us those double secret desired operational parameters may get to a solution faster instead of doling out a list of ever changing double secret parameters?

Here is some code that I use to control a servo based upon sensor readings:

void fLIDAR_ServoAspectChange( void * parameter )
{
  int ServoMax = 1537;
  int ServoMin = 1413;
  int ServoIncrement = 2;
  int ServoCurrent = 0;
  while (1)
  {
    xEventGroupWaitBits (eg, evtLIDAR_ServoAspectChange, pdTRUE, pdTRUE, portMAX_DELAY) ;
    if ( ServoCurrent == 0 )
    {
      xSemaphoreTake ( sema_LIDAR, xSemaphoreTicksToWait );
      ledcWrite( Channel_LIDAR, usToTicks ( ServoMin ) );
      xSemaphoreGive ( sema_LIDAR );
      ServoCurrent = ServoMin;
      x_LIDAR_INFO.CurrentCell = 1;
      x_LIDAR_INFO.ServoSweepUp = true;
      vTaskDelay( pdMS_TO_TICKS( ServoDelay40mS ) );
    }
    else
    {
      if ( x_LIDAR_INFO.ServoSweepUp )
      {
        ServoCurrent += ServoIncrement; // set next servo position to torque to
        x_LIDAR_INFO.CurrentCell += 1;
        xSemaphoreTake ( sema_LIDAR, xSemaphoreTicksToWait );
        ledcWrite( Channel_LIDAR, usToTicks ( ServoCurrent ) );
        xSemaphoreGive ( sema_LIDAR );
        if ( ServoCurrent >= ServoMax )
        {
          x_LIDAR_INFO.ServoSweepUp = false; // toggle servo direction
          xSemaphoreTake( sema_LIDAR_INFO, xSemaphoreTicksToWait );
          xQueueOverwrite( xQ_LIDAR_INFO, (void *) &x_LIDAR_INFO );
          xSemaphoreGive ( sema_LIDAR_INFO );
          xSemaphoreTake( sema_LIDAR_FOR_ALARM, xSemaphoreTicksToWait );
          xQueueOverwrite ( xQ_LIDAR_FOR_ALARM, (void *) &x_LIDAR_INFO );
          xSemaphoreGive( sema_LIDAR_FOR_ALARM );
          xEventGroupSetBits( eg, evtSendSerialToBrain );
        }
      } // if ( ServoSweepUp )
      else // servo sweep down
      {
        ServoCurrent -= ServoIncrement;
        x_LIDAR_INFO.CurrentCell -= 1; // set next cell position
        xSemaphoreTake ( sema_LIDAR, xSemaphoreTicksToWait );
        ledcWrite( Channel_LIDAR, usToTicks ( ServoCurrent ) );
        xSemaphoreGive ( sema_LIDAR );
        // has sweep completed
        if ( ServoCurrent <=  ServoMin )
        {
          x_LIDAR_INFO.ServoSweepUp = true; // toggle servo direction
          xSemaphoreTake( sema_LIDAR_INFO, xSemaphoreTicksToWait );
          xQueueOverwrite( xQ_LIDAR_INFO, (void *) &x_LIDAR_INFO );
          xSemaphoreGive ( sema_LIDAR_INFO );
          xSemaphoreTake( sema_LIDAR_FOR_ALARM, xSemaphoreTicksToWait );
          xQueueOverwrite ( xQ_LIDAR_FOR_ALARM, (void *) &x_LIDAR_INFO );
          xSemaphoreGive( sema_LIDAR_FOR_ALARM );
          xEventGroupSetBits( eg, evtSendSerialToBrain );
        } // if ( ServoCurrent <=  ServoMin )
      } // else // servo sweep down
    } //
    vTaskDelay( ServoDelay12mS );
    xEventGroupSetBits( eg, evtDoLIDAR );
  }
  vTaskDelete( NULL );
}

He is some other code that I use to control a servo based upon sensor readings:

void fTweakServoY( void * parameter )
{
  float temp;
  TickType_t ServoDelay =  pdMS_TO_TICKS( 30 ); // limits degrees per second
  int Max = usToTicks( 1747 );
  int Min = usToTicks( 1349 );
  while (1)
  {
    xEventGroupWaitBits (eg, evtTweakServoY, pdTRUE, pdTRUE, portMAX_DELAY) ;
    xQueueReceive ( xQ_Y_INFO, &temp, QueueReceiveDelayTime );
      temp *= 5.54f; // pitch to servo torque uSeconds
      temp = iY_Posit90 + temp;
      xSemaphoreTake ( sema_LIDAR, xSemaphoreTicksToWait );
      ledcWrite (Channel_Y, usToTicks( (int)temp) );
    xSemaphoreGive ( sema_LIDAR );
    vTaskDelay( ServoDelay );
    xSemaphoreGive( sema_Y );
  }
  vTaskDelete( NULL );
}

Look at File->Examples->Servo->Sweep. Inside the 'for' loops, after the "delay(15);", put:

  if (digitalRead(SensorPin))
    break;

That will exit that sweep when the input on your sensor pin goes HIGH.

neilmac1:
thats easy for you to say?????
servo command 0 - 70 70 - 160
its for a turret was thinking of a while and break command ???????

Show us the code you have now and we can help you change it. But there's no point us trying to guess what you're currently doing. I can't immediately think of any reason why you might need while or break...but then I can't see your code

Oh and you might want to give a few more details than just "a sensor".

Steve

Somebody needs top step away from the moderator button LOL.

Bob

thanks for all the replies i will take all the answers as a learning curve as soon as i get new parts i will be back on it and post some code what i am looking to do is to stop a servo and hold that place in response to a input signal as yet only can run a + - 180 deg turn but i am learning thanks all v much