Friday, 30 March 2018

Control different speed of a small DC motor with push button

Hi, in this post I would like to share with the sketch (down below) of a pwm speed controller and the diagram of it. By uploading the skecth to your Arduino board you will be able to control speed of a small dc motor without a potentiometer and without pressing the reset button. Thanks to Jerico ViƱas who shared the original sketch.

The sketch
const int motor = 4;
const int btn1 = 5;
const int btn2 = 6;
const int btn3 = 7;

int button1 = 0;
int button2 = 0;
int button3 = 0;
int var1 = 0;
int var2 = 0;
int var3 = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(btn1,INPUT);
pinMode(btn2,INPUT);
pinMode(btn3,INPUT);
pinMode(motor,OUTPUT);

}

void loop() {
//Unlimited Execution
// Eco-Programmer
AST();
}

void AST(){
//This is the customized void
button1 = digitalRead(btn1);
button2 = digitalRead(btn2);
button3 = digitalRead(btn3);

if(button1 == HIGH){
var1=1;
var2=0;
var3=0;
Serial.write(var1);
}

if(button2 == HIGH){
var1=0;
var2=1;
var3=0;
Serial.write(var2);
}

if(button3 == HIGH){
var1=0;
var2=0;
var3=1;
Serial.write(var3);
}

if(var1 == 1){
digitalWrite(motor, HIGH);
delay(15);
digitalWrite(motor, LOW);
delay(40);
}

if(var2 == 1){
digitalWrite(motor, HIGH);
delay(15);
digitalWrite(motor, LOW);
delay(20);
}

if(var3 == 1){
digitalWrite(motor, HIGH);
delay(50);
digitalWrite(motor, LOW);
delay(10);
} else{
digitalWrite (motor, LOW);
 }
}

No comments:

Post a Comment