Hello all,
i'm doing some experimenting with Arduino uno to adjust the rpm of a dc motor.
I used the above circuitry and code.
I would like to change the speed with two buttons (probably two digital inputs) instead of a potentiometer. Also i would like to make a speed controller, i can set the desired speed (in rpm) and the motor must starts to rotate, with the mentioned speed.
The above code for the digital setting (0-255) is ok?
And how can i set the desire rpm in accordance with motor's speed?
The principle of what you are doing in the function that you posted looks OK but the function will never end because of the while(1) at the start. This may be OK with you but you have not posted the whole of your program.
As to setting the rpm and having the motor run at that speed, to do it accurately you really need feedback from the motor so that you know what speed it is running at. You could do a crude approximation of what you want by mapping an input range of RPM values to a range of analogWrite() values but the speed of a DC motor in relation to input is not likely to be very linear.
For one thing, in the code on that page he should not be setting oldtime until just before attaching the interrupt. Analog read takes like 105 microseconds and his code adds that to the sample time.
With buttons you may run into problems with that code. You could try and put them on pin change interrupts and maybe they won't mess up the rev count.
Over 99% of the runtime of that sketch is spent doing NOTHING by the call to delay(1000).
It doesn't have to be that way.
If you get rid of the delay(), your sketch can watch the buttons and more as it runs.
At the bottom of this post are 3 links. Check out the 1st one, learn it and you can write code that can run multiple things at a time so fast that you have to take that into consideration.
As for the buttons, see about acting on press and release rather than simply pin state. Pin state alone could add or subtract 1000 or more before your finger has finished pushing the button down.