Auto Brake motor with Ultrasonic sensor

It seems to be working when sensor isn’t connected however when it is connected and detecting object it won’t write powerAngle to what i set as neutral it will make itself go to 91 which isn’t the actual neutral position?

How can I stop this happening? Thanks for the help

Yes when object is detected you go into the first part of the if and there is no print,

 if (distance != 0) { // it means it's less than STOP_DISTANCE
    servoMotor.write(neutral);    //brake the motor if sensor picks up object
  }

so we keep sending the order to stay at neutral

Maybe the servo is not good enough and see 90 and 91 as similar

Thanks I’ve sort of managed to get it working, however sometimes it will just go out of control and go full forward or reverse, which I’m not sure about, I’ll have to do some more investigation.

Thank you so much, couldn’t have done it without your help

may be form time to time pulseIn() behaves weirdly - may be you want to kinda smooth the variations

one way to do this is keep x% of the previous value and (100-x)% of the new value. for example 80% and 20%long powerPulse = 0.8* powerPulse  + 0.2*pulseIn(remoteControlPin, HIGH);

If the pulse is stable, then the value stays the same, but if you get an erroneous pulseIn() read then it will only be worth 20% of the change (if you used 0.2).

The higher the x the slower the reaction to change - so you need to play a bit with x to see what feels right

Thanks I'll try adding that in and see how it goes.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.