Hello, I need some advice. I am trying to code a setup of 4 stepper motors that are connected to a keypad and 2 DC-motors that run a conveyor belt. My goal is to be able to push a key, the correspondning stepper motor starts and the DC-motors starts as well and run for a couple of seconds after the stepper. However, I want to be able to push multiple keys while the DC-motors are running. Right now I need to wait for the DC-motors to be done before I can push another key. How can I make this happen?
I think there will be issues if I code it with a timer since the keys are waiting for an input. I don't have any sensors.
This is my code:
#include <Keypad.h>
#include <Stepper.h>
const int stepsPerRevolution = 1024;
Stepper myStepper1(stepsPerRevolution, 22, 26, 24, 28);
Stepper myStepper2(stepsPerRevolution, 32, 36, 34, 38);
Stepper myStepper3(stepsPerRevolution, 23, 27, 25, 29);
Stepper myStepper4(stepsPerRevolution, 33, 37, 35, 39);
const int IN1 = 47;
const int IN2 = 45;
const int IN3 = 51;
const int IN4 = 49;
const int ENA = 43;
const int ENB = 53;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'D','C','B','A'},
{'H','G','F','E'},
{'L','K','J','I'},
{'P','O','N','M'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
myStepper1.setSpeed(10);
myStepper2.setSpeed(10);
myStepper3.setSpeed(10);
myStepper4.setSpeed(10);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode (ENB, OUTPUT);
}
void loop(){
// char key = keypad.getKey();
char key = keypad.waitForKey();
if (key){
Serial.println(key);
//control speed
analogWrite(ENA, 200);
analogWrite(ENB, 200);
//control direction
int onTime = 5000;
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
// ----------------------------------Stepper1-----------------------------
// just print the pressed key
if (key == 'A' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper1.step(256);
delay(100);
}
// just print the pressed key
if (key == 'E' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper1.step(512);
delay(100);
}
if (key == 'I' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper1.step(768);
delay(100);
}
if (key == 'M' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper1.step(1024);
delay(100);
}
//-----------------------Stepper2------------------------
if (key == 'B' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper2.step(256);
delay(100);
}
// just print the pressed key
if (key == 'F' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper2.step(512);
delay(100);
}
if (key == 'J' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper2.step(768);
delay(100);
}
if (key == 'N' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper2.step(1024);
delay(100);
}
//------------------------Stepper3--------------------
if (key == 'C' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper3.step(256);
delay(100);
}
// just print the pressed key
if (key == 'G' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper3.step(512);
delay(100);
}
if (key == 'K' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper3.step(768);
delay(100);
}
if (key == 'O' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper3.step(1024);
delay(100);
}
//------------------------Stepper4--------------------
if (key == 'D' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper4.step(256);
delay(100);
}
// just print the pressed key
if (key == 'H' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper4.step(512);
delay(100);
}
if (key == 'L' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper4.step(768);
delay(100);
}
if (key == 'P' ){
Serial.println(key);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper4.step(1024);
delay(100);
}
//---------DC_Motor---------
delay(onTime);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
}