Controling motor with potentiometer and buttom

Hi all

This is my first post here, im a newbie In programming, and im stuck In this code, and i hope that one of you experienced programmers Can help me out:)

The board is an Uno, and i will like to activate dc motor with a potentiometer and when the output value from potentiometer is 0, i will like to activate the dc motor with the pushbuttom.
Right now the dc motor is getting activated by the buttom but it’s only buzzing, not turning.

I hope the code is uploaded correctly:)

</>
const int switchPin = 2;
const int motorPin = 9;

const int potPin = A0; // test analog pin potentiometer

int switchState = 0;

int sensorValue = 0; // test sensor value pot
int outputValue = 0; // test analog output to the PWM

void setup() {
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);

Serial.begin(9600); // test initialize serial com at 9600

}

void loop() {
sensorValue = analogRead(potPin); // test read analog in value
outputValue = map(sensorValue, 0, 1023, 0, 255); // test range of analog output
analogWrite(motorPin, outputValue); // test changing output value motorspeed

// test print result to serial monitor
Serial.print(“sensor=”);
Serial.print(sensorValue);
Serial.print("\t output");
Serial.println(outputValue);

delay(2);

switchState = digitalRead(switchPin);

if (switchState == HIGH) {
digitalWrite(motorPin, HIGH);
}

else {
digitalWrite(motorPin, LOW);
}

}
</>

We can not see how you have things wired together, nor can we see how you are powering your project. They are just as important, if not more important than looking at the code you wrote. Please include a schematic of what you have and all the wiring and power connections.
Paul

Your pot input and switch are fighting each other for control of the motor. The pot gets to set the motor speed for about 2 milliseconds before the switch tells the motor FULL SPEED! or STOP!. Then the pot gets a little more time.

From your description you only want the switch to have control when the pot is set to zero. To do that you want something like:

  if (sensorValue == 0}
  {
    // DO THE SWITCH STUFF HERE
  }
  else
  {
    // DO THE POT STUFF HERE
    analogWrite(sensorValue / 4);
  }

Hi Paul and John
Thanks for the reply

@Paul now a hand drawn schematic is attached, hope that it is readable:)

@John would it be possible to write your peace of code In the loop section and then copy paste pot code In the ELSE and switch coding in IF?

BR
Kenneth

Very nice, Kenneth.
But, I wonder why you went the added complexity of having a resistor connecting the switch to +5 and ground, when you could use use input.pullup on the pin and reverse your logic.
Paul

kc1234:
@John would it be possible to write your peace of code In the loop section and then copy paste pot code In the ELSE and switch coding in IF?

Yes, that is exactly where you should put it.
NOTE: You can’t put ALL of the pot code in the ‘else’. You have to leave the “sensorValue = analogRead()” part above the ‘if’ so that the ‘if’ can check ‘sensorValue’.

Hi again!

@John it worked with anologRead instead of analogWrite! Thank you very much.

@Paul i was not awar of the pullup function, so i will try to change code and schematic and try to make it work! Thank you for telling me about that option:)

BR
Kenneth

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