Thanks CJ Southern
My project is for model railroad. (I am an Arduino beginner btw) There is a thing called a transfer table that moves 1 loco at a time backward and forward to different tracks.(a turntable goes rotationally around to each track) Using stepper motor.
Ideas is to tell the steppr motor direction and then number of turns. Distance between each track is exactly the same. Stepper is at set speed.
There are variables for
current position (which is obtained from new postion (when its reached its destination.)(held in an Eprom (which is obtained from button press)
motor direction (worked out from diff between variable new pos and current pos)
movement (in # of motor turns) (obtained by diff between each selected position.
Code below. There will be some errors which I hope to learn and fix, at the moment just trying to clarify how the button press works and how to make it refresh each loop so after a button press on 1 loop the next loop does not see it anymore. and the variable has been updated. Need something like -- button pressed, digital read, change variable, and then back to state before the was button pressed all in 1 loop. Do I need to go as far as using the button states etc
Is there a way to use 'Create to check the code.'
Bear in mind I am aged deficient or is that over supplied I am 77 and a bit slow on the uptake.
Thanks and go easy.
Charles Harris
Downunder NZ
/* Sketch using UNO R3 ??? with stepper motor and driver, with 5 buttons. For use on model railroad transfer table 5 loco track positions. Use of buttons for track selection allows user to go between any 2 tracks in either direction. Retains current position(from previous session) on startup.
*/
#include <EEPROM.h>
#define ENABLE_PIN A0 // LOW = driver enabled – stepper motor
#define M1_PIN A1 // M1 microstepping mode// is this necessay??
#define M2_PIN A2 // M2 microstepping mode?? ditto
#define DIR_PIN A3 // to DIR pin of driver
#define STEP_PIN A4 // to DIR pin of driver
#define MTRSPEED_PIN A5 // set speed
#define ONOFF_LED_PIN 13 // Motor running LED
#define DIR_LED_PIN 4 // Motor direction LED
#define PULSES_PER_REV 200 // Pulses per revolution
#define BUTTON1_PIN = ; //pushbutton to select track #1
#define BUTTON2_PIN = ; //pushbutton to select track #2
#define BUTTON3_PIN = ; //pushbutton to select track #3 NEED PINs
#define BUTTON4_PIN = ; //pushbutton to select track #4
#define BUTTON5_PIN = ; //pushbutton to select track #5
// Define number of positions, and number of each position
// NUM_POSITIONS = 5
unsigned long positions = {0,800,1600,2400,3200}; // this needs fixing to pos 1 + step
#define step = 800etc
byte dir, current_pos, new_pos, totalmovement;
void motor_enable() {
set speed(200)
digitalWrite(ENABLE_PIN, LOW); // LOW = driver enabled
digitalWrite(ONOFF_LED_PIN, HIGH);
digitalWrite(DIR_PIN, dir);
}
void motor_idle() {
digitalWrite(ENABLE_PIN, HIGH); // HIGH = driver disabled
digitalWrite(ONOFF_LED_PIN, LOW);
}
void setup() {
pinMode(BUTTON1_PIN, INPUT); pullup? And == positions ?
pinMode(BUTTON2_PIN, INPUT);
pinMode(BUTTON3_PIN, INPUT);
pinMode(BUTTON4_PIN, INPUT);
pinMode(BUTTON5_PIN, INPUT);
pinMode(M1_PIN, OUTPUT);
pinMode(M2_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
pinMode(ONOFF_LED_PIN, OUTPUT);
pinMode(DIR_LED_PIN, OUTPUT);
digitalWrite(M1_PIN, LOW); // set M1,M2 to 0,0 for no microstepping ??
digitalWrite(M2_PIN, LOW);
motor_idle();
current_pos = EEPROM.read(0);
if(current_pos > NUM_POSITIONS) {
current_pos = 1;
EEPROM.write(0,1);
}
new_pos = current_pos;
}
void loop() {
if(digitalRead(BUTTON1_PIN) == 0) new_pos = 1, (BUTTON1_PIN) == 1; This ??????
if(digitalRead(BUTTON2_PIN) == 0) new_pos = 2;
if(digitalRead(BUTTON3_PIN) == 0) new_pos = 3;
if(digitalRead(BUTTON4_PIN) == 0) new_pos = 4;
if(digitalRead(BUTTON5_PIN) == 0) new_pos = 5;
if(new_pos != current_pos) {
if(new_pos > current_pos) {
dir = 1;
movement step
//totalmovement(tt_position[new_pos] – tt_position[current_pos]);
}
else {
dir = 0;
totalmovement(tt_position[current_pos] - tt_position[new_pos]);
}
current_pos = new_pos;
EEPROM.write(0, current_pos);
}
}
//b