Created
March 14, 2020 07:09
-
-
Save winhtut/059e481cee22815e5a1050d1cc2da553 to your computer and use it in GitHub Desktop.
dcmotorSpeedControlWinHtut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int IN1 = 7; | |
const int IN2 = 6; | |
const int ENA = 9; | |
const int ENB = 3; | |
void setup() { | |
pinMode (IN1, OUTPUT); | |
pinMode (IN2, OUTPUT); | |
pinMode (ENA, OUTPUT); | |
pinMode (ENB, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("Press F for fast speed"); | |
Serial.println("Press M for Normal speed"); | |
Serial.println("Press S for Slow speed"); | |
Serial.println("Press x for Stop Motor"); | |
} | |
void loop() { | |
if(Serial.available()){ | |
int data=Serial.read(); | |
switch(data){ | |
case 102: | |
fast(); | |
break; | |
case 109: | |
medium(); | |
break; | |
case 115: | |
slow(); | |
break; | |
case 120: | |
xstop(); | |
break; | |
} | |
} | |
} | |
void fast(){ | |
analogWrite(ENA, 255); | |
analogWrite(ENB, 255); | |
digitalWrite(IN1, HIGH); | |
digitalWrite(IN2, LOW); | |
delay(3000); | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, HIGH); | |
delay(3000); | |
} | |
void medium(){ | |
analogWrite(ENA, 125); | |
analogWrite(ENB, 125); | |
digitalWrite(IN1, HIGH); | |
digitalWrite(IN2, LOW); | |
delay(3000); | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, HIGH); | |
delay(3000); | |
} | |
void slow(){ | |
analogWrite(ENA, 40); | |
analogWrite(ENB, 40); | |
digitalWrite(IN1, HIGH); | |
digitalWrite(IN2, LOW); | |
delay(3000); | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, HIGH); | |
delay(3000); | |
} | |
void xstop(){ | |
analogWrite(ENA, 40); | |
analogWrite(ENB, 40); | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, LOW); | |
delay(3000); | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, LOW); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment