Setting speed proportional to distance using a motor shield

I am trying to set my DC motor speed to vary as a function of distance from a wall. I’m using an ultrasonic sensor to see the distance, and a motor shield to control the speed of the motor. I’ve been using motor.setspeed to set a constant value of speed, but I need to be able to set a function with the form v(x)=ax+b. Is there a way to do this without just using a bunch of if statements?

Without seeing your program, how can we help.


You just need to scale (ax+b) to your speed units and set the speed accordingly. e.g.