I need help with my dc motor

Hi!
I need help programming my dc motor.
I need it to start when I press the pushbutton and to accelerate it with potentiometer.
And lastly I want it to shut down when I press the pushbutton again. :slight_smile:

Here's my code

void button();
void potentiometer();
int buttonPin = 2;
int motorPin =  3;
int buttonState = 0;
int ledState = HIGH;
int lastButtonState = LOW;
int sensorValue = 0;
int analogInPin = A0;
long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
  pinMode(motorPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
  Serial.begin(9600);
}

void loop()
{
  button();
}
  
  
//******************BUTTON*******************************
void button()
{
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState)
  {
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  
  
  lastButtonState = reading;
  digitalWrite (motorPin, ledState);
  Serial.println(buttonState);
}


//*********************POTENTIOMETER*********************
void potentiometer()
{

     sensorValue = analogRead(analogInPin);
    sensorValue=sensorValue/4; 
    analogWrite(motorPin, sensorValue);           
    Serial.println(sensorValue);
    button();
 }

What sort of motor is it and how is it connected to the Arduino ?

The Arduino pins have very limited ability to supply current without the chip being damaged, so it is normal to use an external power supply for motors and a means of controlling them with a small current input such as an FET of H Bridge circuit.

What does your program do at the moment ?

When I upload it to Arduino the motor starts and I can control it's speed with potentiometer.
But the button isn't working.

The only place that you read the value of the analogInPin in your code is in the potentiometer() function but that function is not called in your program so I don't understand how the potentiometer controls the speed of the motor.

Oh my bad. I had wrong program uploaded to my Arduino.
So, I can start and stop my motor from the button, but I can't control motor's speed with my potentiometer.

So, post the code.

I already posted it, I only had wrong code uploaded to my Arduino.

By what mechanism do you expect the potentiometer to affect the motor?

I don't know. Please enlighten me :slight_smile:

Well, you've written code to read a pot, but, as has already been pointed out, you don't call it.
I'd start there.

I called the potentiometer() function and now I can adjust motors speed with my potentiometer but the button stopped working.

void button();
void potentiometer();
int buttonPin = 2;
int motorPin =  3;
int buttonState = 0;
int ledState = HIGH;
int lastButtonState = LOW;
int sensorValue = 0;
int analogInPin = A0;
long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
  pinMode(motorPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
  Serial.begin(9600);
}

void loop()
{
  button();
}
  
  
//******************BUTTON*******************************
void button()
{
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState)
  {
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  
  
  lastButtonState = reading;
  potentiometer();
  Serial.println(buttonState);
}


//*********************POTENTIOMETER*********************
void potentiometer()
{

     sensorValue = analogRead(analogInPin);
    sensorValue=sensorValue/4; 
    analogWrite(motorPin, sensorValue);           
    Serial.println(sensorValue);
    button();
 }

With all that recursion going on, I'm surprised it works at all.

I've deleted your last comment.
Post another one and you're banned.

If "button" calls "potentiometer" and "potentiometer" calls "button", you're going to run out of stack very quickly.
That's recursion.

So, what should I do? I'm new at Arduino world.

I'd remove the reference to "button" from "potentiometer", and the reference to "potentiometer" from "button", then call "button" and "potentiometer" from "loop".

I'm new at Arduino world.

That's no excuse for your earlier comments.
How are we supposed to know that?

It did nothing, I can adjust the speed of motor from potentiometer, but the button isn't working.

void button();
void potentiometer();
int buttonPin = 2;
int motorPin =  3;
int buttonState = 0;
int ledState = HIGH;
int lastButtonState = LOW;
int sensorValue = 0;
int analogInPin = A0;
long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
  pinMode(motorPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
  Serial.begin(9600);
}

void loop()
{
  button();
  potentiometer();
}
  
  
//******************BUTTON*******************************
void button()
{
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState)
  {
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  
  
  lastButtonState = reading;
  Serial.println(buttonState);
}


//*********************POTENTIOMETER*********************
void potentiometer()
{

     sensorValue = analogRead(analogInPin);
    sensorValue=sensorValue/4; 
    analogWrite(motorPin, sensorValue);           
    Serial.println(sensorValue);
 }

What do you mean by " the button isn't working." ?

When I upload the code to my Arduino, the motor starts running and I can adjust it's speed with my potentiometer, but the button does nothing when I press it.

What do you expect the button to do?
What does it actually do?
What does the serial monitor show the button is doing?

I already said in the beginning of this topic that what I want the button to do.
The button does absolutely nothing.
Serial monitor says that the button is changing only "reading" value from "0" to "1".