My daughters are working on a school STEM project using an RFID tag to control access to a cat bowl feeder. The door is controlled by two stepper motors moving simultaneously in opposite directions at the same speed. The issue is that one motor operates correctly but the second only vibrates without moving. Output displayed on the serial monitor shows that the Arduino thinks the second motor is arriving at the target destination, but the motor itself isn't turning. This occurs whether the motor is moving clockwise or counterclockwise. We have eliminated hardware issues by testing a simpler code with both motors (both function perfectly) and changing out the stepper driver. We also get the same problem when we swap the motor pins on the Arduino. The steppers are model 28BYJ-48, and use ULN2003 driver boards. The code is below: (the problem motor is Stepper2 referenced below)
//Cat bowl Feeder STEM project
//RFID activated automatic cat bowl feeder
#include <SPI.h>
#include <MFRC522.h>
#include <AccelStepper.h>
#define SS_PIN 10 //RFID pin
#define RST_PIN 9 //RFID pin
#define LED_G A2 //define green LED pin
#define LED_R A1 //define red LED pin
#define FULLSTEP 4 //used for Stepper motors
#define HALFSTEP 8 //used for Stepper motors
// motor pins defined
#define motorPin1 4 // 28BYJ48 pin 1 Stepper 1
#define motorPin2 5 // 28BYJ48 pin 2
#define motorPin3 6 // 28BYJ48 pin 3
#define motorPin4 7 // 28BYJ48 pin 4
#define motorPin5 0 // 28BYJ48 pin 1 Stepper 2
#define motorPin6 1 // 28BYJ48 pin 2
#define motorPin7 2 // 28BYJ48 pin 3
#define motorPin8 3 // 28BYJ48 pin 4
MFRC522 mfrc522(SS_PIN, RST_PIN); // Initializes the RFID
AccelStepper stepper1(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4); //initalize stepper motor1
AccelStepper stepper2(FULLSTEP, motorPin5, motorPin7, motorPin6, motorPin8); //initalizes stepper motor2
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
pinMode(LED_G, OUTPUT); //setup for Green LED light
pinMode(LED_R, OUTPUT); //setup for Red LED light
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(50.0);
stepper1.setSpeed(200);
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(50.0);
stepper2.setSpeed(200);
Serial.println("Put your card to the reader...");
Serial.println();
Serial.println (stepper1.currentPosition());
Serial.println (stepper2.currentPosition());
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.print("UID tag :"); //Show UID on serial monitor
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "2B F5 A4 59") //This is the unique ID tag for our cat collar
{
opendoor(); //runs open door sequence
}
else
{
closedoor(); //runs close door sequence
}
}
void opendoor() //routine to open the door
{
Serial.println("Hello kitty cat");
Serial.println();
analogWrite(LED_G, 255); //Turns on Green LED
delay(300);
if (stepper1.currentPosition() == 2048) // this is to test if the door is already open, exit the loop if is
{
Serial.println("door already open");
return;
}
else {
stepper1.moveTo(2048);
stepper2.moveTo(-2048);
while (stepper2.distanceToGo() != 0)
{
stepper1.run();
stepper2.run();
}
if (stepper1.distanceToGo() == 0)
{
Serial.println("stepper1 reached target");
}
if (stepper2.distanceToGo() == 0)
{
Serial.println("stepper2 reached target");
}
delay(5000);
analogWrite(LED_G, 0);
}
}
void closedoor() { //routine to close the door
Serial.println("Nope, not the cat");
analogWrite(LED_R, 255); //turns on Red LED light
delay(1000);
analogWrite(LED_R, 0);
if (stepper1.currentPosition() == 0) //to test if the door is already closed, if yes, exit the loop
{
Serial.println("door already closed");
return;
}
else {
stepper1.moveTo(0);
stepper2.moveTo(0);
while (stepper2.distanceToGo() !=0)
{
stepper1.run();
stepper2.run();
if (stepper1.distanceToGo() == 0)
{
Serial.println("stepper1 reached target");
}
if (stepper2.distanceToGo() == 0)
{
Serial.println("stepper2 reached target");
}
}
}
}