Created
June 10, 2019 18:51
-
-
Save MindstormFan/fcf46332652763cbca27f0692323a901 to your computer and use it in GitHub Desktop.
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
#include <AFMotor.h> | |
AF_DCMotor motor3(3, MOTOR34_64KHZ); // Define motor #3, 64KHz pwm | |
AF_DCMotor motor4(4, MOTOR34_64KHZ); // Define motor #4, 64KHz pwm | |
// Arduino pin numbers | |
const int X_pin = 0; // analog pin connected to X output | |
const int Y_pin = 1; // analog pin connected to Y output | |
const int SW_pin = 2; // digital pin connected to switch output | |
/* AF_DCMotor motor(2);*/ //Another way to define | |
void setup() { | |
Serial.begin(9600); // Open Serial communication at baud rate 9600 bps | |
Serial.println("Testing the Motor"); | |
motor3.setSpeed(200); // Set the motor speed at 200 to 255 PWM | |
motor4.setSpeed(200); // Set the motor speed at 200 to 255 PWM | |
pinMode(SW_pin, INPUT); | |
digitalWrite(SW_pin, HIGH); | |
Serial.begin(9600); | |
} | |
void loop() { | |
while(analogRead(X_pin) == 0) { | |
Serial.print("Turning Forward.... "); | |
motor3.run(FORWARD); // turn it on going forward | |
motor4.run(FORWARD); // turn it on going forward | |
delay(1000); | |
} | |
while(analogRead(X_pin) == 1023) { | |
Serial.print("Turning Backward"); | |
motor3.run(BACKWARD); // the other way | |
motor4.run(BACKWARD); // the other way | |
delay(1000); | |
} | |
while(analogRead(Y_pin) == 0) { | |
Serial.print("Turning Right...."); | |
motor3.run(FORWARD); // turn it on going forward | |
motor4.run(BACKWARD); // turn it on going forward | |
delay(1000); | |
} | |
while(analogRead(Y_pin) == 1023) { | |
Serial.print("Turning Left..."); | |
motor3.run(BACKWARD); // the other way | |
motor4.run(FORWARD); // the other way | |
delay(1000); | |
} | |
if(digitalRead(SW_pin)) { | |
Serial.print("Release or Stop"); | |
motor3.setSpeed(200); // Set the motor speed at 200 to 255 PWM | |
motor4.setSpeed(200); // Set the motor speed at 200 to 255 PWM | |
motor3.run(RELEASE); // Motor will stop | |
motor4.run(RELEASE); // Motor will stop | |
delay(1000); // delay at 1 second | |
} | |
if(!digitalRead(SW_pin)) { | |
motor3.setSpeed(250); // Set the motor speed at 200 to 255 PWM | |
motor4.setSpeed(250); // Set the motor speed at 200 to 255 PWM | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment