Control speed of a small DC motor with push button

Long time ago I made video about PWM where I used an Arduino to control the power. In that video I used 3 sketches, one for slow speed, one for medium and one for fast speed. I had to upload different sketch to make the motor spin in different speed.

Although I tried my best to find out how I can use one sketch and set different speed, but could not find it at that time. And here I've found it and it works fine, but the problem is you have to reset before you send another command. I think it's still way better than the other one which I made earlier.

Why do you have to click the reset button every time?
Well, that because (if you use the sketch below) then when you send a command to the Arduino board to spin the motor in one speed, it goes into loop by turning the motor on and off for certain amount of time. And it works so fast that it doesn't even have time to get another command, so it's ignores it as if nothing really happened. That's why you have to click the reset button to bring it back from the loop.

The sketch
const int motor =  5;      // the number of the motor pin

const int button1Pin = 2;     // the number of the pushbutton pin
int buttonState1 = 0;         // variable for reading the pushbutton status

const int button2Pin = 3;     //
int buttonState2 = 0;         //

const int button3Pin = 4;     //
int buttonState3 = 0;         //

void setup() {
  // initialize the motor pin as an output:
  pinMode(motor, OUTPUT);
  // initialize the pushbutton pins as an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
}

void slow(){
  digitalWrite(motor, HIGH);
  delay(15); // Change the numbers if you need to.
  digitalWrite(motor, LOW);
  delay(40); // Change the numbers if you need to.
  slow();
}

void medium(){
  digitalWrite(motor, HIGH);
  delay(15); // Change the numbers if you need to.
  digitalWrite(motor, LOW);
  delay(20); // Change the numbers if you need to.
  medium();
}

void fast(){
  digitalWrite(motor, HIGH);
  delay(30); // Change the numbers if you need to.
  digitalWrite(motor, LOW);
  delay(10); // Change the numbers if you need to.
  fast();
}


void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(button1Pin);
  buttonState2 = digitalRead(button2Pin);
  buttonState3 = digitalRead(button3Pin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn Motor on:
    slow();
  } else {
    // Nothing
  }

   if (buttonState2 == HIGH) {
    // turn Motor on:
    medium();
  } else {
    // Nothing
  }

  if (buttonState3 == HIGH) {
    // turn Motor on:
    fast();
  } else {
    // Nothing
  }
}

The sketch is in action below
I hope it helped you.



1 comment:

  1. hello friend, can you change the speed without pressing the reset button

    ReplyDelete



Newly posted:
Reason why rich getting richer and poor getting poorer