Created
February 24, 2017 12:04
-
-
Save peat-psuwit/37077f24bb47197fd54d9f2b31a7d3e0 to your computer and use it in GitHub Desktop.
Speed up and slow down motors, then reverse.
This file contains 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
/* | |
* Author: Ratchanan Srirattanamet | |
*/ | |
int Menable = 6; //PWM capable | |
int MdriveA = 5; | |
int MdriveB = 4; | |
int delaytime = 300; | |
void setup() { | |
pinMode(Menable, OUTPUT); | |
pinMode(MdriveA, OUTPUT); | |
pinMode(MdriveB, OUTPUT); | |
Serial.begin(9600); | |
} | |
void DriveMotCW(int Mspeed) { | |
analogWrite(Menable, Mspeed); | |
digitalWrite(MdriveA, 0); | |
digitalWrite(MdriveB, 1); | |
} | |
void DriveMotCCW(int Mspeed) { | |
analogWrite(Menable, Mspeed); | |
digitalWrite(MdriveA, 1); | |
digitalWrite(MdriveB, 0); | |
} | |
void loop() { | |
int Mspeed = 0; | |
for (Mspeed = 5; Mspeed <= 250; Mspeed += 5) { | |
DriveMotCW(Mspeed); | |
Serial.println(Mspeed); | |
delay(delaytime); | |
} | |
for (Mspeed = 245; Mspeed >= 5; Mspeed -= 5) { | |
DriveMotCW(Mspeed); | |
Serial.println(Mspeed); | |
delay(delaytime); | |
} | |
for (Mspeed = 5; Mspeed <= 250; Mspeed += 5) { | |
DriveMotCCW(Mspeed); | |
Serial.println(-Mspeed); | |
delay(delaytime); | |
} | |
for (Mspeed = 245; Mspeed >= 5; Mspeed -= 5) { | |
DriveMotCCW(Mspeed); | |
Serial.println(-Mspeed); | |
delay(delaytime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment