I'm working on automating my model railroad turntable with a NEMA17 stepper motor and an Arduino Uno. Over the last couple of weeks, I've built and tested the various subassemblies, and got them working in isolation. However, I've just having them work together, and they're not working at all.
The turntable has a hall effect sensor mounted on the outside, to detect a magnet mounted on the end of the turntable bridge. This is to be used for calibration, and the turntable is supposed to stop when it the magnet is detected. There's also a 4-digit 7-segment LED display on the control panel, driven by a MAX7219 chip, which will display the DCC address of the selected locomotive.
After putting everything together, I tested it with the following code:
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include <LedControl.h>
#define SENSOR_PIN 10 //For hall effect sensor
//LED output pins
#define GR_LED_PIN 4
#define RD_LED_PIN 5
//Motor control pins
#define MT_STEP 2
#define MT_DIR 3
// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200
#define RPM 3 //2 is the lowest at 16 microsteps for smooth movement. 1 is too jerky.
// Since microstepping is set externally, make sure this matches the selected mode
// If it doesn't, the motor will move at a different RPM than chosen
// 1=full step, 2=half step etc.
#define MICROSTEPS 16
//For motor direction, positive is clockwise and negative is anticlockwise
//LED Display Pins
#define RO_CLK 7
#define RO_DIN 8
#define RO_LOAD 9
//Occupancy Sensor
#define OCC_PIN A0
//Below defines and creates the LED controller object
LedControl dispLED = LedControl(RO_DIN,RO_CLK,RO_LOAD,1);
//Create the Stepper Motor object.
BasicStepperDriver step_motor(MOTOR_STEPS, MT_DIR, MT_STEP);
void setup() {
//Set up the LED pins:
pinMode(GR_LED_PIN, OUTPUT);
pinMode(RD_LED_PIN, OUTPUT);
//Set up the hall effect sensor
pinMode(SENSOR_PIN, INPUT);
// put your setup code here, to run once:
//Initialise the LED controller
dispLED.shutdown(0,false);
/* Set the brightness to a medium values */
dispLED.setIntensity(0,12);
/* and clear the display */
dispLED.clearDisplay(0);
disNumLed (1234);
delay(1000);
dispLED.clearDisplay(0);
//Initialise the stepper motor
step_motor.begin(RPM, MICROSTEPS);
step_motor.enable();
}
void loop() {
// put your main code here, to run repeatedly:
}
void calibrateTurntable(){
/*Below boolean acts as a flag as to
* whether the turntable should be
* calibrating or not.
*/
boolean turning = true;
while (turning){
step_motor.move(-1);
delay(10);
if (digitalRead(SENSOR_PIN) == HIGH){
turning = false;
}
}
}
void disNumLed (int disNum){
//Takes an integer and displays it on the 4-digit LED display
int firstDigit;
int secondDigit;
int thirdDigit;
int fourthDigit;
/*The 4-digit single module has digit 1 on left and 4 on the right. Hence,
* it needs to be added in reverse order. firstDigit to position 3, through
* to fourthDigit in position 0.
*/
dispLED.clearDisplay(0);
firstDigit = disNum % 10; //Return remainder of division operation (modulus)
dispLED.setDigit(0,3,firstDigit,false);
if (disNum > 9){
//If int is still greater than 9, then there should be another digit (base 10)
disNum /= 10; //Move to next digit
secondDigit = disNum % 10;
dispLED.setDigit(0,2,secondDigit,false);
}
if (disNum > 9){
disNum /= 10; //Move to next digit
thirdDigit = disNum % 10;
dispLED.setDigit(0,1,thirdDigit,false);
}
if (disNum > 9){
//Remainder would be above 10
disNum /= 10; //Move to next digit
fourthDigit = disNum; //As it's the only digit left
dispLED.setDigit(0,0,fourthDigit,false);
}
}
However, this didn't give the intended results. The turntable bridge only moved a handful of steps the first couple of times. After resetting the arduino 3 times, it started moving in a full circle. However, the LED display on the control panel went haywire while it was doing so. Furthermore, the arduino seemed to ignore the hall effect sensor completely. I know that it works, as I tested it with the turntable bridge and a sketch that lit up an LED just a few minutes before testing this one.
I suspect that the magnetic field of the stepper motor is interfering with the other components and causing false signals. The hall effect sensor is about 4.5" away, and the MAX7219 chip about 1' away. Am I correct? If so, how do I go about shielding the stepper motor so things work the way they're supposed to?