adding a dead band to my motor and gearbox I have a motor and gearbox that has limits and a pot built in.

I have some code what drives the motor one way to when it hits its limits takes a value and drives the other way and takes a value, then map the range from 0 to 100, that all works fine.

I also have a analog input map to 0 to 100 what I would like to do is to drive the motor to where ever the analog input is at, which is what I have done with the code below but when the motor gets close it starts hunting backwards and forwards.

I tried to add some sort of dead band (where + 0.5 / where - 0.5) it just seems to just move the problem.

I did try -+ but this still did not help, so what I need to do is aim for the target value and stop then if I'm within 0.5 ether way then that is good enough.

How can I do that?



sensor       = pot on motor,
where        = 0 to 5 input,
vent1UPlim   = limits,
vent1DOWNlim = limits 


PS. I know the map of 0 to 100 could do with being bigger but it's just easier to get my head around at the moment. 



        //            pot           0-10v     ONE WAY
          if (sensorValue &gt; (where + 0.5)) {
          if (digitalRead(vent1UPlim)==    HIGH) {
            DoneInitializationA=false;                    
            digitalWrite(vent1enable,      HIGH);
            delay(50);
            digitalWrite(vent1PWMA ,       HIGH);
            mydisp.drawStr(0, 2, "-close-|");          
          } 
          else {
            digitalWrite(vent1PWMA ,       LOW);
            delay(50);
            digitalWrite(vent1enable,      LOW);
            mydisp.drawStr(0, 2, "       |");
          }
        }
        else {
          if (!DoneInitializationA) 
          {
            digitalWrite(vent1PWMA ,       LOW);
            delay(50);
            digitalWrite(vent1enable,      LOW);
            mydisp.drawStr(0, 2, "       |");
            DoneInitializationA=true;                
          }
        }

           //                                   OTHER WAY
          if (sensorValue &lt; (where - 0.5)) {
          if (digitalRead(vent1DOWNlim)==  HIGH) {
            DoneInitializationB=false;                       
            digitalWrite(vent1enable,      HIGH);
            delay(50);
            digitalWrite(vent1PWMB ,       HIGH);
            mydisp.drawStr(0, 2, "-open -|");
          } 
          else {
            digitalWrite(vent1PWMB ,       LOW);
            delay(50);
            digitalWrite(vent1enable,      LOW);
            mydisp.drawStr(0, 2, "       |"); 
          }
        }
        else {
          if (!DoneInitializationB) 
          {
            digitalWrite(vent1PWMB ,       LOW);
            delay(50);
            digitalWrite(vent1enable,      LOW);
            mydisp.drawStr(0, 2, "       |");  
            DoneInitializationB=true;                           
          }
        }
The simple way is to just subtract one from the other, take the absolute value and see if it exceeds your hysteresis range.

eg.



if (abs (sensorValue - where) &gt;= 0.5) {
  {
  // do something

  if (sensorValue &gt; where)
    {
    // one way
    }
  else
    {
    // other way
    }
  } // end of large enough to worry about




In response to this (since deleted):


  would this not have the same problem as mine as instead of hunting round its target it will just do it 0.5 away from the target? 


By subtracting and taking the absolute value, we create a band of +0.5 and -0.5 where, if the sensor is in that range, we do nothing. It hasn't moved the problem because it can't move in two directions at once (both up and down).