Embedded Programming‎ > ‎Examples‎ > ‎

Run Motor

Code:


//motor 1 pins defined by motor driver
#define PWM1_PIN 3
#define DIR1 12
//motor 2 pins
#define PWM2_PIN 11
#define DIR2 13
#define FWD 1
#define REV 0

void setup(){

pinMode(PWM1_PIN, OUTPUT); //initialize all pins being used as output pins
pinMode(PWM2_PIN, OUTPUT);
pinMode(DIR1, OUTPUT);
pinMode(DIR2, OUTPUT);

digitalWrite(DIR1,FWD); //initialize the directions forward
digitalWrite(DIR2,FWD);

analogWrite(PWM1_PIN,0); //turn both motors off
analogWrite(PWM2_PIN,0);

}

void loop(){

digitalWrite (DIR1,FWD); //set motor 1 to move forward
analogWrite (PWM1_PIN,255); //send 255 (100%) duty cycle to motor 1
delay(1000); //pause 1000ms or 1 second

analogWrite(PWM1_PIN,0); //turn motor 1 off
delay(1000);

digitalWrite (DIR1,REV); //set motor 1 to move in reverse
analogWrite (PWM1_PIN,255); //send 255 (100%) duty cycle to motor 1
delay(1000); //pause 1000ms or 1 second

analogWrite(PWM1_PIN,0); //turn motor 1 off
delay(1000);

digitalWrite (DIR2,FWD); //set motor 2 to move forward
analogWrite (PWM2_PIN,255); //send 255 (100%) duty cycle to motor 2
delay(1000); //pause 1000ms or 1 second

analogWrite(PWM2_PIN,0); //turn motor 2 off
delay(1000);

digitalWrite (DIR2,REV); //set motor 2 to move in reverse
analogWrite (PWM2_PIN,255); //send 255 (100%) duty cycle to motor 2
delay(1000); //pause 1000ms or 1 second

analogWrite(PWM2_PIN,0); //turn motor 2 off
delay(1000);
}

Comments