Skip to content

Instantly share code, notes, and snippets.

@peat-psuwit
Created February 24, 2017 12:04
Show Gist options
  • Save peat-psuwit/37077f24bb47197fd54d9f2b31a7d3e0 to your computer and use it in GitHub Desktop.
Save peat-psuwit/37077f24bb47197fd54d9f2b31a7d3e0 to your computer and use it in GitHub Desktop.
Speed up and slow down motors, then reverse.
/*
* 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