// Define pin connections & motor's steps per revolution
const int dirPin = 5;
const int stepPin = 6;
const int dirPinA = 7;
const int stepPinA = 8;
const int dirPinB = 9;
const int stepPinB = 10;
const int dc_enable1 = 11;
const int dc_enable2 = 12;
const int stepsPerRevolution = 200;
char Incoming_value ;
void setup()
{
Serial.begin(9600);
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepPinA, OUTPUT);
pinMode(dirPinA, OUTPUT);
}
void loop()
{
if(Serial.available() > 0)
{
Incoming_value = Serial.read();
// Set motor direction clockwise
if(Incoming_value == '0') {
Incoming_value = Serial.read();
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(dirPin, HIGH);
// digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
}
// Set motor direction counterclockwise
if(Incoming_value == '1') {
// Spin motor quickly
Incoming_value = Serial.read();
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
} }
if(Incoming_value == '2') {
Incoming_value = Serial.read();
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(dirPinA, HIGH);
// digitalWrite(dirPin, HIGH);
digitalWrite(stepPinA, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPinA, LOW);
delayMicroseconds(100);
}
}
// Set motor direction counterclockwise
if(Incoming_value == '3') {
// Spin motor quickly
Incoming_value = Serial.read();
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(dirPinA, LOW);
digitalWrite(stepPinA, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPinA, LOW);
delayMicroseconds(100);
} }
if(Incoming_value == '4') {
Incoming_value = Serial.read();
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(dirPinB, HIGH);
// digitalWrite(dirPin, HIGH);
digitalWrite(stepPinB, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPinB, LOW);
delayMicroseconds(100);
}
}
// Set motor direction counterclockwise
if(Incoming_value == '5') {
// Spin motor quickly
Incoming_value = Serial.read();
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(dirPinB, LOW);
digitalWrite(stepPinB, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPinB, LOW);
delayMicroseconds(100);
} }
if(Incoming_value == '6') {
Incoming_value = Serial.read();
digitalWrite(dc_enable1, LOW);
digitalWrite(dc_enable2, HIGH);
delay(5000);
}
if(Incoming_value == '7') {
Incoming_value = Serial.read();
digitalWrite(dc_enable1, LOW);
digitalWrite(dc_enable2, LOW);
}
}}
Files to Download
Arduino IDE Code