I am trying to control two motors simultaneously with both moving in opposite directions. Unfortunately, I am not able to control RPM or step size with this. It runs at the same speed no matter what I input. Can someone help me with this?
#include<Stepper.h>
//definitions for each command to be received through serial port
#define COMMAND_STRETCH 's'
#define COMMAND_UNSTRETCH 'u'
//enter the steps per revolution for the motor here
int stepsInRev = 21600;
//this sets the value for the for loops and therefore sets the amount of steps in each call
int num_of_steps = 1;
//setup pins for each driver motor motor1 = IN1. IN2, IN3, IN4; motor2 = IN1, IN2, IN3, IN4
Stepper myStepper1(stepsInRev, 4, 5, 6, 7);
Stepper myStepper2(stepsInRev, 8, 9, 10, 11);
//variable to store the last call to the serial port
char lastCall = ' ';
// void stretch();
// void unstretch();
// void allStop();
void stretch(int steps)
{
Serial.println("stretch");
//motor 1 steps clockwise and motor 2 steps anticlockwise
myStepper1.step(-1);
myStepper2.step(1);
delay(10);
}
void unstretch(int steps){
Serial.println("unstretch");
//motor 1 moves anticlockwise and motor 2 steps clockwise
myStepper1.step(1);
myStepper2.step(-1);
delay(10);
}
//to power down the motor drivers and stop the motors
void allStop(){
Serial.println("stop");
//steppers stop
PORTD = B00000000; //sets all pins from 0 to 7 as LOW to power off stepper1
PORTB = B00000000; //sets all pins from 8 to 13 as LOW to power off stepper2
}
void setup() {
//start the bluetooth serial port = send and receive at 9600 baud rate
//set the speed at 60 rpm;
myStepper1.setSpeed(60);
myStepper2.setSpeed(60);
Serial.begin(9600);
//initialise the serial port;
}
void loop() {
// check if there is serial communication and if so read the data
if(Serial.available()){
char data = (char)Serial.read();
// switch to set the char via serial to a command
switch(data) {
case COMMAND_STRETCH:
stretch(num_of_steps);
break;
case COMMAND_UNSTRETCH:
unstretch(num_of_steps);
break;
}
//set the lastCall variable to the last call from the serial
lastCall = data;
}
else{
char data = lastCall;
switch(data)
{
case COMMAND_STRETCH:
stretch(num_of_steps);
break;
case COMMAND_UNSTRETCH:
unstretch(num_of_steps);
break;
}
lastCall = data;
}
}