Controlling a DC motor using PWM and a potentiometer

I concur, the code doesn't really make any sense.

The value function just looks like a leftover from the getpot() function. OP, appears to be trying to constrain the ADC values (normally 0-1023) to 90->255. Arduino actually has a constrain function which does this, btw.

Your motor forward and backward are doing exactly the same thing. There is no way you are getting a different action, other than a slower FORWARD speed and only because you are turning the pot to a lower value.

So, let's start at the beginning...

You are trying to control the speed of the motor using a pot in only one direction. Then why do you have motorForward and motorBackward routines?

What does the relay do?

The code is a mess and the only reason it is doing anything at all is because you are writing an uncontrolled 'something' to the motor line. It kind of looks like you were copying code from some other project and not understanding how to use it.

What you are trying to do is extremely simple (and the code you posted far too complex for that), so let us know what the relay is for and we can help.

1 Like