ESP_IOT v2.5
IOT ESP Coding
PTStepperClass.cpp
Go to the documentation of this file.
1//! \link PTStepperClass
2// PTStepperClass.cpp
3//
4//
5// Created by Scott Moody on 3/16/25.
6//
7
8#include "PTStepperClass.h"
9
10
11//! 8.30.25 LA Warm, Zuma Beach - end scene of Planet of Apes
12#ifdef USE_FAST_LED
13#include "../ATOM_LED_Module/M5Display.h"
14#endif
15
16
17//! 7.13.26 Nice day, Field cut .. not hay yet. Ballon in morning..
19
20//! for now .. use the PTStepper.h
21//#include "PTStepper.h"
22
23// from Dispense it calls getFeederType_mainModule()
24#include "StepperModule.h"
25
26/******************stepper declarations******************************/
31
33{
34 SerialDebug.printf("PTStepperClass init %s\n", config);
35}
36
37//
38
39#ifdef BOARD
40////Put all the pins in an array to make them easy to work with
42 12, //BLUE end of the Blue/Yellow motor coil RED is ground
43 14, //PINK end of the Pink/Orange motor coil
44 27, //YELLOW end of the Blue/Yellow motor coil
45 26 //ORANGE end of the Pink/Orange motor coil
46};
47
48#else
49
50
51#define NEW_M5_STEPPER
52#ifdef NEW_M5_STEPPER
53// M5Atom GPIO pins connected to ULN2003 IN1–IN4
54#ifdef ESP_M5_ATOM_S3
55//! 7.12.26 new pins of Atom zs3
56#define IN1 5
57#define IN2 6
58#define IN3 7
59#define IN4 8
60#else
61#define IN1 22
62#define IN2 19
63#define IN3 23
64#define IN4 33
65#endif
66////Put all the pins in an array to make them easy to work with
68 IN1, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
69 IN2, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
70 IN3, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
71 IN4 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
72};
73#else
74////Put all the pins in an array to make them easy to work with
76 13, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
77 12, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
78 14, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
79 27 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
80};
81#endif
82
83#endif //BOARD
84
85//Define the full step sequence.
86//With the pin (coil) states as an array of arrays
89 {HIGH,HIGH,LOW,LOW},
90 {LOW,HIGH,HIGH,LOW},
91 {LOW,LOW,HIGH,HIGH},
92 {HIGH,LOW,LOW,HIGH}
93};
94
95//Keeps track of the current step.
96//We'll use a zero based index.
98//int _cycleCnt = 0;
100
101//Keeps track of the current direction
102//Relative to the face of the motor.
103//Clockwise (true) or Counterclockwise(false)
104//We'll default to clockwise
106
107
108void step_PTStepperClass(int steps[][4], int stepCount);
109/**************** end stepper declarations *******************************************************************/
110
111/***************** begin stepper actions *******************************************************************/
112
113/************* Set all motor pins off which turns off the motor ************************************************/
115 //! check for valid
117 {
118 return;
119 }
120 for (int pin = 0; pin < 4; pin++) {
121 pinMode(_pins_PTStepperClass[pin], OUTPUT);
122 digitalWrite(_pins_PTStepperClass[pin], LOW);
123 }
124
125}
126
127//! calculate the steps etc
129{
131 //!converts degrees to motor cycles
133
134 // Define number of steps per rotation:
135 // Calculate how many micro-steps to complete one feed sb 130 for Uno or 512 for Mini
136
137#define USE_STEPPER_ANGLE_FORMULA
138#ifdef USE_STEPPER_ANGLE_FORMULA
139#else
140 // Uno makes 16 steps per revolution
142 {
144 }
145 // Mini makes 4 steps per revolution but reverses
148 // Tumbler accepts a variable number of degrees
150 {
151 //!converts degrees to motor cycles
153 }
154 // Fall thru handling *** new information from https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/
155 else
157#endif
158 SerialDebug.printf("PTStepperClass type=%d, angle = %f, STEPS = %d\n",getFeederType_mainModule(), stepperAngle, _targetSteps_PTStepperClass);
159
160 //! 6.14.2024 put this back as the if-else was goofed up.
161 /**************** Fudge Factor code to correct registration *********************
162 ** This code is added to make the stepper keep registration in the long run. **
163 ** it ran for 20 hours with a stepper angle of 15 and registarion was perfect.**
164 ********************************************************************************/
166 case(0):
169 break;
170 case(1):
173 break;
174 case(2):
177 break;
178 }
179
180 _lastType_PTStepperClass = getFeederType_mainModule(); // let's not do this again until needed
181
182 //!see if the motor direction is clockwise == true
184 SerialDebug.printf("MOTOR_DIRECTION_clockwise_PTStepperClass = %d\n", _clockwise_PTStepperClass);
185
186}
187
188//Prepare motor controller
190{
191
192 //! 5.3.25 trying to figure out the PIN use
193 _validPins_PTStepperClass = registerPinUse_mainModule(IN1, "IN1", "PTStepperClass", false);
197
198 //! check for valid
200 {
201 SerialError.println("**** invalid pins PTStepperClass");
202 return;
203 }
204
205 //! uses the IN1 pins ..
206 clearPins_PTStepperClass(); // Go turn all motor pins off
207
208}
209
210void step_PTStepperClass(int steps[][4], int stepCount)
211{
212 //Then we can figure out what our current step within the sequence from the overall current step
213 //and the number of steps in the sequence
214 //SerialDebug.print("current step ");
215 //SerialDebug.println(currentStep);
216 int currentStepInSequence = _currentStep_PTStepperClass % stepCount;
217
218 //Figure out which step to use. If clock wise, it is the same is the current step
219 //if not clockwise, we fire them in the reverse order...
220 int directionStep = _clockwise_PTStepperClass ? currentStepInSequence : (stepCount - 1) - currentStepInSequence;
221
222 //Set the four pins to their proper state for the current step in the sequence,
223 //and for the current direction
224 for (int pin = 0; pin < 4; pin++) {
225 digitalWrite(_pins_PTStepperClass[pin], steps[directionStep][pin]);
226 }
227}
228
229//! cycle
231{
232 //Get a local reference to the number of steps in the sequence
233 //And call the step method to advance the motor in the proper direction
234
235 //Full Step
236 int stepCount = _fullStepCount_PTStepperClass;
238
239 // Increment the program field tracking the current step we are on
241
242 // If targetSteps has been specified, and we have reached
243 // that number of steps, reset the currentStep, and reverse directions
246 clearPins_PTStepperClass(); // Turn off motor
247 }
248 else if (_targetSteps_PTStepperClass == 0 && _currentStep_PTStepperClass == stepCount) {
249 // don't reverse direction, just reset the currentStep to 0
250 // resetting this will prevent currentStep from
251 // eventually overflowing the int variable it is stored in.
254 }
255
256 //2 milliseconds seems to be about the shortest delay that is usable. Anything
257 //lower and the motor starts to freeze.
258 delay(2);
259}
260
261//! 9.4.25
262//! delay amount after a start_MotorStepper
263//! defaults = 0
265{
266 return 0;
267}
268
269//This will advance the stepper clockwise once by the angle specified in SetupStepper. Example 16 pockets in UNO is 22.5 degrees
271{
272 //! check for valid
274 {
275 return;
276 }
278 { // Not assigned yet, is zero or is different type
279 whoAreWe_PTStepperClass(); // go figure who we are, Mini or Uno and calculate steps needed
280 SerialDebug.print("Feeder type currently is ");
281 SerialDebug.println(getFeederType_mainModule());
282 }
283 SerialDebug.println("**************** PTStepperClass::Starting Stepper *******************");
284 //! 8.30.25 LA Warm, Zuma Beach - end scene of Planet of Apes
285#ifdef USE_FAST_LED
286 fillpix(L_RED);
287#else
288 //! 8.2.26 LED working
290#endif
291 //! 5.15.25 try the async CLICK
292 //! click call
293 //! click call 5.26.25 SYNC version
295
297 // Step one unit in forward
300 }
301 _cycleCounter_PTStepperClass = 0; // Reset when done
302 SerialDebug.println("**************** PTStepperClass::Ending Stepper *************");
304
305
307 SerialDebug.println("**************** PTStepperClass::Starting Return *******************");
308 _clockwise_PTStepperClass = !_clockwise_PTStepperClass; // Time to go backwards
309 while (_cycleCounter_PTStepperClass < _targetSteps_PTStepperClass - 5) { //Don't quite go all the way back
310 // Step one unit backwards
313 }
315 _cycleCounter_PTStepperClass = 0; // Reset when done
316 _clockwise_PTStepperClass = !_clockwise_PTStepperClass; // next time go forward
317 SerialDebug.println("**************** PTStepperClass::Ending Return ***********");
318
319 }
320 //! 8.30.25 LA Warm, Zuma Beach - end scene of Planet of Apes
321#ifdef USE_FAST_LED
322 fillpix(L_BLUE);
323#else
324 //! 8.2.26 LED working
326#endif
327}
328//! stops motor
330{
331 //! check for valid
333 {
334 return;
335 }
336}
337//!loop the PTStepper (so timer can run)
339{
340 //! check for valid
342 {
343 return;
344 }
345}
boolean registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
void main_dispatchSyncCommand(int syncCallCommand)
the main sync command (no parameters yet)
int getFeederType_mainModule()
get the feeder type (Sepper 1,2,3 ...)
Definition: MainModule.cpp:730
void setLEDColor(ColorsUnified color)
uses colors.. _GREEN to be typed values
#define SYNC_CLICK_SOUND
Definition: MainModule.h:268
@ BLUE_LED
Definition: MainModule.h:537
@ RED_LED
Definition: MainModule.h:536
void cycle_PTStepperClass()
cycle
void step_PTStepperClass(int steps[][4], int stepCount)
#define IN4
int _ourSteps_PTStepperClass(0)
for now .. use the PTStepper.h
boolean _validPins_PTStepperClass
PTStepperClass
int _currentStep_PTStepperClass
int _fudgeFactor_PTStepperClass
int _pins_PTStepperClass[]
int _fullStepCount_PTStepperClass
int _fullSteps_PTStepperClass[][4]
void whoAreWe_PTStepperClass()
calculate the steps etc
int _targetSteps_PTStepperClass
#define IN1
7.12.26 new pins of Atom zs3
int _cycleCounter_PTStepperClass
bool _clockwise_PTStepperClass
void clearPins_PTStepperClass()
#define IN2
int _lastType_PTStepperClass
int delayAmount_MotorStepper()
#define IN3
float getPreferenceFloat_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define STEPPER_IS_TUMBLER
#define STEPPER_IS_UNO
#define STEPPER_IS_MINI
#define PREFERENCE_STEPPER_ANGLE_FLOAT_SETTING
#define PREFERENCE_STEPPER_CLOCKWISE_MOTOR_DIRECTION_SETTING
#define MINI
Definition: StepperModule.h:48
An mostly virtual class.
void start_MotorStepper()
starts the PTStepper
PTStepperClass(char *config)
constructor
void loop_MotorStepper()
setup the PTStepper
void setup_MotorStepper()
setup the PTStepper
void stop_MotorStepper()
stops motor