Firstly, my apologies for using incorrect language etc. I am very new to all this and am trying to learn extremely quickly. I am extremely appreciative of any help that can be offered and thank you for reading on.
I have been trying to create a sketch to control a T-Gauge 1/450 scale model railway turntable with simple clockwise and anti clockwise buttons to control direction. I then desire motor travel to the next pre-stored location/step count in said direction.
I have been going around and around in circles for a week or more (pun intended) that im getting dizzy.
Can i please reach out for some guidance/advice, especially as to whether this project is feasible or not.
I have read so many online pages, watched tutorials, followed youtube videos, etc. I have even attempted to use Chat GPT as one video recommended it. All to no success.
I then spent time building and wiring a tester board with sensors, switches, buttons, pots, LEDS etc all built in so as to simplify wiring and ensure it was not the wiring at fault. I am satisfied following extensive testing that these are all now wired correctly.
I have tried learning about .cpp and .h files to try and break the code down but I think im getting more and more confused. I even attempted to follow some tutorials to enable me to create these but im starting to believe my entire approach is wrong. Please help.
I am trying to create the program/sketch to meet my brief as below:
i need it to control a stepper motor 28byj- 48 using an uln2003 driver board to be used as a model rail turntable. - I do not require anything too substantial due to the size if the model.
i need to be able to store 12 saved locations. 6 locations and then the same 6 plus a half turn so the train can be turned around to face the opposite way.
I also require one additional ‘home’ location set at 150 steps clockwise from zero. This should remain locked. This ensures there will always be one saved location from zero.
i require the last known position to be stored to the EEPROM for next power up.
if power is lost before saving the current location then upon power up the zero routine should be automatically initiated.
Upon power up, and if the last position is known, the motor should turn a quarter turn clockwise, followed by a quarter turn counter clockwise, to show the user that the system is ready and calibrated.
If the last position is unknown the find Zero Routine will activate automatically.
the zero routine should also be able to be activated manually by holding both directional buttons down for 5 seconds.
The Zero Routine should allow for the LM393 IR Optocoupler to determine the zero when the beam is broken. - i can use an alternative, a micro switch, a KY-024 hall effect sensor, etc., as per advice and guidance but i am very limited on space due to the size of model.
When active this motor should turn counter clockwise until the beam is broken. This beam break is zero location. Whilst this zero routine is active all 4 leds should be lit constantly.
Once zero is located the motor should turn clockwise to home and then flash all 4 leds twice to show the user the system is calibrated and ready.
The system will then revert back to normal operating mode and await a user input.
Normal operating mode should act as follows.
i would like to be able to control direction of travel with a push button for clockwise and a different push button for counter clockwise travel.
when a button is pressed the motor will move to the next saved position in the desired direction of travel.
whilst travelling the corresponding directional led will light to show which direction the motor is in motion to the user.
if the directional button is held pressed for 3 or more seconds the motor will complete a half turn in the desired direction. the corresponding directional led will flash until the desired location is met.
Whilst a travel order is being carried out a button press in the same direction of travel will be ignored until the destination is reached. If the direction button for the opposite direction is pressed then this will pause all movement. The motor will then await a directional input, and then move to the next saved location in that direction.
i need to be able to activate a ‘customise saved location mode’.
In this mode the zero and home locations will be ignored for the user and only zero will be used to determine the steps required to store the desired locations.
i need to be able to access this ‘customise saved location mode’ by using a rocker switch. Whilst this switch is on the mode is active.
When first turned on the motor will immediately find zero in a clockwise direction and then hold position, awaiting user input. At this point the set position led should light and remain lit whilst in this mode.
By turning the switch off the mode is exited and anything unsaved is lost. The motor will then return to home.
During this mode if a directional button is pressed, then the motor will turn to the next saved location in that direction, if it exists. This will assist with big steps. If no saved position exists then the motor will remain stationary.
In this mode the ky-040 rotary encoder will be used to control precision steps manually to allow for fine tuning.
The KY-040 button will then be pressed for 5 seconds to save the current position.
This will also save a position exactly a half turn from the set position.
To confirm a successfully set saved location both directional leds will light and the set position led will flash 5 times. During this time, the motor will move clockwise 100 steps, then 100 steps counter clockwise.
i need to be able to activate a ‘delete saved location mode’.
In this mode the zero and home locations will be ignored for the user and only zero will be used to determine the steps required to the desired locations.
i need to be able to access this ‘delete saved location mode’ by using a rocker switch. Whilst this switch is on the mode is active.
When first turned on the motor will immediately find zero counter clockwise and then hold position, awaiting user input. At this point the delete led should light and remain lit whilst in this mode.
By turning the switch off the mode is exited and anything unsaved is lost. The motor will then return to home.
During this mode if a directional button is pressed, then the motor will turn to the next saved location in that direction, if it exists. This will assist with big steps. If no saved position exists then the motor will remain stationary.
In this mode the KY-040 button will then be pressed for 5 seconds to delete the current position. The rotary dial will remain inactive throughout this mode.
This will also delete the position exactly a half turn from the selected position.
To confirm a successfully deleted saved location both directional leds will light and the delete led will flash 5 times.
Whilst in this mode the user needs to hold the rotary encoder button and the reset button together for 10 seconds to complete a ‘full reset’, thereby deleting all user defined saved locations. To confirm the full reset all leds will flash 10 times and the system will then exit the mode and begin the find zero routine.
i require an led to show that power is going to the system and that the motor is awaiting its next input. This led should blink when an action is being received, but otherwise should remain in a solid state.
To date i have been using the following pin definitions, however, if need be these can be changed if the Arduino Uno R3 is better allocated another way.
// Pin definitions
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
#define SENSOR_DO_PIN 6
#define POWER_LED 7
#define DIR_0_LED 8
#define DIR_1_LED 9
#define DIR_0_BUTTON 10
#define DIR_1_BUTTON 11
#define CUSTOMISE_SAVED_LOCATION_MODE_SWITCH 12
#define DELETE_SAVED_LOCATION_SWITCH 13
#define ENC_SW A0
#define ENC_DT A1
#define ENC_CLK A2
#define DELETE_LED A3
#define SET_POS_LED A4
Thank you for reading.
Am i asking too much of the Arduino Uno? would a different version work? can anyone recommend where i could locate the code/libraries etc that would work for me?
Many thanks.
Notridge13