Hey guys, I am trying to get a dc motor to start/stop with the press of button while also being able to change the speed of the motor with a potentiometer. I have used the following code and had some success. At first, I was able to get the start/stop button to work and even the potentiometer worked somewhat. However, after repeated use that motor would either only run when I held the button down, only run for a brief second or two and then stop, start when I pressed the button but then not stop, or just run as soon as I uploaded the code and not respond to any button or potentiometer. This all happened when I didn't change the physical circuit at all and not altering the code. I am really confused as to why the motor seemingly does what it wants and how to fix it.
const int controlPin1 = 2, controlPin2 = 3, onOffPin=9;
int speed;
int start = 0;
int direc ;
void setup() {
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(onOffPin, OUTPUT);
pinMode(A1, INPUT);
// put your setup code here, to run once:
Then, tidy up your code (in the IDE - use Ctrl-T) and put it inside code tags so people can easily read it and cut&paste the code. It helps others help you.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
You might want to try ButtonToSwitch library, and use the SldrDALtchMPBttn class that gives you the chance to set a button that turns On/Off a device, and when held down acts as a potentiometer. You might try the 11_SldrDALtchMPBttn_1a example. I think it's documented enough, but if you have any dobut PM me.