Hello everyone. I am a model railroad fan and have been working on a turntable project. I have followed numerous threads in other forums. I believe I have found the exact scenario I'm trying to create, but I do NOT know how to put these two together.
So here is what I have. I am using a stepperonline Nema 17 stepper motor with a TB6600 Driver.
The project that I have spent the most time following, they are using an Adafruit Stepper/motor hat as part of the equation. Here is the project I've been following. There is a photo of the interface that connects the DCC, and a hall effect sensor to the adafruit board. I want to keep this same wiring diagram, but instead of going to the adafruit board, I need to go directly to the Arduino uno.
Now I also need to send the outputs to the TB6600 stepper Driver.
So basically what I want to do is EXACTLY what this code is doing:
////////////////////////////////////////////////////////////////////////////////
//
// DCC Turntable Control Test Routines
#include <DCC_Decoder.h>
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Defines and structures
//
#define kDCC_INTERRUPT 0
typedef struct
{
int address; // Address to respond to
} DCCAccessoryAddress;
DCCAccessoryAddress gAddresses[8];
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Adafruit Setup
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
// Connect stepper with 200 steps per revolution (1.8 degree)
// to the M3, M4 terminals (blue,yellow,green,red)
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);
// you can change these to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP!
// wrapper for the motor! (3200 Microsteps/revolution)
void forwardstep2() {
myStepper2->onestep(FORWARD, MICROSTEP);
}
void backwardstep2() {
myStepper2->onestep(BACKWARD, MICROSTEP);
}
// Now we'll wrap the stepper in an AccelStepper object
AccelStepper stepper2(forwardstep2, backwardstep2);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Decoder Init
//
void ConfigureDecoder()
{
gAddresses[0].address = 200;
gAddresses[1].address = 201;
gAddresses[2].address = 202;
gAddresses[3].address = 203;
gAddresses[4].address = 204;
gAddresses[5].address = 205;
gAddresses[6].address = 206;
gAddresses[7].address = 207;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Basic accessory packet handler
//
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data)
{
// Convert NMRA packet address format to human address
address -= 1;
address *= 4;
address += 1;
address += (data & 0x06) >> 1;
boolean enable = (data & 0x01) ? 1 : 0;
for(int i=0; i<(int)(sizeof(gAddresses)/sizeof(gAddresses[0])); i++)
{
if( address == gAddresses[i].address )
{
Serial.print("Basic addr: ");
Serial.print(address,DEC);
Serial.print(" activate: ");
Serial.println(enable,DEC);
if( enable )
{
switch (i) {
case 1:
stepper2.moveTo(200);
break;
case 2:
stepper2.moveTo(400);
break;
case 3:
stepper2.moveTo(600);
break;
case 4:
stepper2.moveTo(800);
break;
case 5:
stepper2.moveTo(1000);
break;
case 6:
stepper2.moveTo(1200);
break;
case 7:
stepper2.moveTo(1400);
break;
}
}else{
switch (i) {
case 1:
stepper2.moveTo(1800);
break;
case 2:
stepper2.moveTo(2000);
break;
case 3:
stepper2.moveTo(2200);
break;
case 4:
stepper2.moveTo(2400);
break;
case 5:
stepper2.moveTo(2600);
break;
case 6:
stepper2.moveTo(2800);
break;
case 7:
stepper2.moveTo(3000);
break;
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Setup
//
void setup()
{
Serial.begin(9600);
AFMStop.begin(); // Start the shield
//configure pin3 as an input and enable the internal pull-up resistor
pinMode(3, INPUT_PULLUP);
//read the sensor (open collector type) value into a variable
int sensorVal = digitalRead(3);
//set stepper motor speed and acceleration
stepper2.setMaxSpeed(30.0);
stepper2.setAcceleration(20.0);
// stepper2.moveTo(800);
// if near reference point move away
sensorVal = digitalRead(3);
while (sensorVal == LOW) {
sensorVal = digitalRead(3);
forwardstep2();
delay(50);
}
// step forward to sensor index point
while (sensorVal == HIGH) {
sensorVal = digitalRead(3);
forwardstep2();
delay(50);
}
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
ConfigureDecoder();
DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main loop
//
void loop()
{
static int addr = 0;
////////////////////////////////////////////////////////////////
// Loop DCC library
DCC.loop();
////////////////////////////////////////////////////////////////
// Bump to next address to test
if( ++addr >= (int)(sizeof(gAddresses)/sizeof(gAddresses[0])) )
{
addr = 0;
}
stepper2.run();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The difference is that I do NOT want to use the adafruit stepper hat, I want to use my external TB6600 Driver instead.
I really hope this makes sense.
I don't know how to fix the code to do this.
