0

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 > (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 < (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;                           
          }
        }
  • I don't quite understand how your motors work: Do they keep doing the last thing they were told, or do you have to keep sending some signal to them - even to stay at the same speed? – Nick Gammon Aug 29 '15 at 21:40
  • enable the drive pull hight (vent1enble), and to drive the motor pull one pin high and to go the other way pull the other pin high (vent1PWMA or vent1PWMA) – user11998 Aug 30 '15 at 16:04
  • OK, so if you do nothing, the motor keeps turning at the same rate in the same direction as before? So, doing nothing if it is near the desired sensor value is OK? – Nick Gammon Aug 30 '15 at 20:36
  • if i do nothing the motor does nothing. doing nothing near the desired value yes – user11998 Aug 31 '15 at 10:10
1

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) >= 0.5) {
  {
  // do something

  if (sensorValue > 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).

  • would this not have the same problem as mine instead of hunting round its target it will just do it 0.5 away from the target? as what will drive it into the middle (the target value)? i hope you understand what I'm trying to get to thanks Joe – user11998 Aug 30 '15 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.