So i finally made this program, but was only able to check it through the serial monitor. i have included some strings to print when a particular case is satisfied. i don't have 6 motors now but i can say that they will work when these arguments are passed.
please comment if any problem may occur.
i have given each motor a number like 1, 2,etc and each motor has to rotate in clockwise and anticlockwise direction. so i have chosen 12 alphabets. if you press f motor 1 will rotate clockwise, if you press F, motor 1 will rotate anticlockwise and similarly for all 6 motors. alphabets are f,F,r,R,b,B. I was not able to post the full code here, so i have attached it.
byte directionPin1 = 1;
byte directionPin2 = 3;
byte directionPin3 = 5;
byte directionPin4 = 7;
byte directionPin5 = 9;
byte directionPin6 = 11;
byte stepPin1 = 2;
byte stepPin2 = 4;
byte stepPin3 = 6;
byte stepPin4 = 8;
byte stepPin5 = 10;
byte stepPin6 = 12;
int numberOfSteps = 50;
byte ledPin = 13;
int pulseWidthMicros = 50; // microseconds
int millisbetweenSteps = 1; // milliseconds
void setup()
{
Serial.begin(9600);
for (int pins = 1; pins < 13; pins++)
{
pinMode(pins, OUTPUT);
}
}
void loop()
{
if (Serial.available() > 0)
{
int inByte = Serial.read();
switch (inByte)
{
case 'f': {
Serial.begin(9600);
Serial.println("Starting Stepper Motor 1 CW");
digitalWrite(ledPin, LOW);
delay(1);
pinMode(directionPin1, OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(directionPin1, HIGH);
for(int n = 0; n < numberOfSteps; n++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin1, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(20);
}
break;
case 'F': {
Serial.begin(9600);
Serial.println("Starting Stepper Motor 1 CCW");
digitalWrite(ledPin, LOW);
delay(1);
pinMode(directionPin1, OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(directionPin1, LOW);
for(int n = 0; n < numberOfSteps; n++)
{
digitalWrite(stepPin1, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin1, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(20);
}
break;
case 'r': {
Serial.begin(9600);
Serial.println("Starting Stepper Motor 2 CW");
digitalWrite(ledPin, LOW);
delay(1);
pinMode(directionPin2, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(directionPin2, HIGH);
for(int n = 0; n < numberOfSteps; n++)
{
digitalWrite(stepPin2, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin2, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
}
break;
case 'R': {
Serial.begin(9600);
Serial.println("Starting Stepper Motor 2 CCW");
digitalWrite(ledPin, LOW);
delay(1);
pinMode(directionPin2, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(directionPin2, LOW);
for(int n = 0; n < numberOfSteps; n++)
{
digitalWrite(stepPin2, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin2, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
}
break;
case 'b': {
Serial.begin(9600);
Serial.println("Starting Stepper Motor 3 CW");
digitalWrite(ledPin, LOW);
delay(1);
pinMode(directionPin3, OUTPUT);
pinMode(stepPin3, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(directionPin3, HIGH);
for(int n = 0; n < numberOfSteps; n++)
{
digitalWrite(stepPin3, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin3, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
}
break;
case 'B': {
Serial.begin(9600);
Serial.println("Starting Stepper Motor 3 CCW");
digitalWrite(ledPin, LOW);
delay(1);
pinMode(directionPin3, OUTPUT);
pinMode(stepPin3, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(directionPin3, LOW);
for(int n = 0; n < numberOfSteps; n++)
{
digitalWrite(stepPin3, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin3, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
}
break;
default: {
}
break;
}
}
}
Stepper_Motor_Switch_Case_Full.ino (11.7 KB)