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//! for now .. use the PTStepper.h
18//#include "PTStepper.h"
19
20// from Dispense it calls getFeederType_mainModule()
21#include "StepperModule.h"
22
23/******************stepper declarations******************************/
28
30{
31 SerialDebug.printf("PTStepperClass init %s\n", config);
32}
33
34//
35
36#ifdef BOARD
37////Put all the pins in an array to make them easy to work with
39 12, //BLUE end of the Blue/Yellow motor coil RED is ground
40 14, //PINK end of the Pink/Orange motor coil
41 27, //YELLOW end of the Blue/Yellow motor coil
42 26 //ORANGE end of the Pink/Orange motor coil
43};
44
45#else
46
47
48#define NEW_M5_STEPPER
49#ifdef NEW_M5_STEPPER
50// M5Atom GPIO pins connected to ULN2003 IN1–IN4
51#define IN1 22
52#define IN2 19
53#define IN3 23
54#define IN4 33
55////Put all the pins in an array to make them easy to work with
57 IN1, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
58 IN2, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
59 IN3, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
60 IN4 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
61};
62#else
63////Put all the pins in an array to make them easy to work with
65 13, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
66 12, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
67 14, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
68 27 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
69};
70#endif
71
72#endif //BOARD
73
74//Define the full step sequence.
75//With the pin (coil) states as an array of arrays
78 {HIGH,HIGH,LOW,LOW},
79 {LOW,HIGH,HIGH,LOW},
80 {LOW,LOW,HIGH,HIGH},
81 {HIGH,LOW,LOW,HIGH}
82};
83
84//Keeps track of the current step.
85//We'll use a zero based index.
87//int _cycleCnt = 0;
89
90//Keeps track of the current direction
91//Relative to the face of the motor.
92//Clockwise (true) or Counterclockwise(false)
93//We'll default to clockwise
95
96
97void step_PTStepperClass(int steps[][4], int stepCount);
98/**************** end stepper declarations *******************************************************************/
99
100/***************** begin stepper actions *******************************************************************/
101
102/************* Set all motor pins off which turns off the motor ************************************************/
104 for (int pin = 0; pin < 4; pin++) {
105 pinMode(_pins_PTStepperClass[pin], OUTPUT);
106 digitalWrite(_pins_PTStepperClass[pin], LOW);
107 }
108
109}
110
111//! calculate the steps etc
113{
115 //!converts degrees to motor cycles
117
118 // Define number of steps per rotation:
119 // Calculate how many micro-steps to complete one feed sb 130 for Uno or 512 for Mini
120
121#define USE_STEPPER_ANGLE_FORMULA
122#ifdef USE_STEPPER_ANGLE_FORMULA
123#else
124 // Uno makes 16 steps per revolution
126 {
128 }
129 // Mini makes 4 steps per revolution but reverses
132 // Tumbler accepts a variable number of degrees
134 {
135 //!converts degrees to motor cycles
137 }
138 // Fall thru handling *** new information from https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/
139 else
141#endif
142 SerialDebug.printf("PTStepperClass type=%d, angle = %f, STEPS = %d\n",getFeederType_mainModule(), stepperAngle, _targetSteps_PTStepperClass);
143
144 //! 6.14.2024 put this back as the if-else was goofed up.
145 /**************** Fudge Factor code to correct registration *********************
146 ** This code is added to make the stepper keep registration in the long run. **
147 ** it ran for 20 hours with a stepper angle of 15 and registarion was perfect.**
148 ********************************************************************************/
150 case(0):
153 break;
154 case(1):
157 break;
158 case(2):
161 break;
162 }
163
164 _lastType_PTStepperClass = getFeederType_mainModule(); // let's not do this again until needed
165
166 //!see if the motor direction is clockwise == true
168 SerialDebug.printf("MOTOR_DIRECTION_clockwise_PTStepperClass = %d\n", _clockwise_PTStepperClass);
169
170}
171
172//Prepare motor controller
174{
175 clearPins_PTStepperClass(); // Go turn all motor pins off
176 //SerialDebug.print("ourSteps after ");
177 //SerialDebug.print(ourSteps);
178 //SerialDebug.println(" ");
179
180
181 //! 5.3.25 trying to figure out the PIN use
182 registerPinUse_mainModule(IN1, "IN1", "PTStepperClass", false);
183 registerPinUse_mainModule(IN2, "IN2", "PTStepperClass", false);
184 registerPinUse_mainModule(IN3, "IN3", "PTStepperClass", false);
185 registerPinUse_mainModule(IN4, "IN4", "PTStepperClass", false);
186}
187
188void step_PTStepperClass(int steps[][4], int stepCount)
189{
190 //Then we can figure out what our current step within the sequence from the overall current step
191 //and the number of steps in the sequence
192 //SerialDebug.print("current step ");
193 //SerialDebug.println(currentStep);
194 int currentStepInSequence = _currentStep_PTStepperClass % stepCount;
195
196 //Figure out which step to use. If clock wise, it is the same is the current step
197 //if not clockwise, we fire them in the reverse order...
198 int directionStep = _clockwise_PTStepperClass ? currentStepInSequence : (stepCount - 1) - currentStepInSequence;
199
200 //Set the four pins to their proper state for the current step in the sequence,
201 //and for the current direction
202 for (int pin = 0; pin < 4; pin++) {
203 digitalWrite(_pins_PTStepperClass[pin], steps[directionStep][pin]);
204 }
205}
206
207//! cycle
209{
210 //Get a local reference to the number of steps in the sequence
211 //And call the step method to advance the motor in the proper direction
212
213 //Full Step
214 int stepCount = _fullStepCount_PTStepperClass;
216
217 // Increment the program field tracking the current step we are on
219
220 // If targetSteps has been specified, and we have reached
221 // that number of steps, reset the currentStep, and reverse directions
224 clearPins_PTStepperClass(); // Turn off motor
225 }
226 else if (_targetSteps_PTStepperClass == 0 && _currentStep_PTStepperClass == stepCount) {
227 // don't reverse direction, just reset the currentStep to 0
228 // resetting this will prevent currentStep from
229 // eventually overflowing the int variable it is stored in.
232 }
233
234 //2 milliseconds seems to be about the shortest delay that is usable. Anything
235 //lower and the motor starts to freeze.
236 delay(2);
237}
238
239//! 9.4.25
240//! delay amount after a start_MotorStepper
241//! defaults = 0
243{
244 return 0;
245}
246
247//This will advance the stepper clockwise once by the angle specified in SetupStepper. Example 16 pockets in UNO is 22.5 degrees
250 { // Not assigned yet, is zero or is different type
251 whoAreWe_PTStepperClass(); // go figure who we are, Mini or Uno and calculate steps needed
252 SerialDebug.print("Feeder type currently is ");
253 SerialDebug.println(getFeederType_mainModule());
254 }
255 SerialDebug.println("**************** PTStepperClass::Starting Stepper *******************");
256 //! 8.30.25 LA Warm, Zuma Beach - end scene of Planet of Apes
257#ifdef USE_FAST_LED
258 fillpix(L_RED);
259#endif
260 //! 5.15.25 try the async CLICK
261 //! click call
262 //! click call 5.26.25 SYNC version
264
266 // Step one unit in forward
269 }
270 _cycleCounter_PTStepperClass = 0; // Reset when done
271 SerialDebug.println("**************** PTStepperClass::Ending Stepper *************");
273
274
276 SerialDebug.println("**************** PTStepperClass::Starting Return *******************");
277 _clockwise_PTStepperClass = !_clockwise_PTStepperClass; // Time to go backwards
278 while (_cycleCounter_PTStepperClass < _targetSteps_PTStepperClass - 5) { //Don't quite go all the way back
279 // Step one unit backwards
282 }
284 _cycleCounter_PTStepperClass = 0; // Reset when done
285 _clockwise_PTStepperClass = !_clockwise_PTStepperClass; // next time go forward
286 SerialDebug.println("**************** PTStepperClass::Ending Return ***********");
287
288 }
289 //! 8.30.25 LA Warm, Zuma Beach - end scene of Planet of Apes
290#ifdef USE_FAST_LED
292#endif
293}
294//! stops motor
296{
297
298}
299//!loop the PTStepper (so timer can run)
301{
302
303}
void fillpix(CRGB Color)
color the button light
Definition: M5Display.cpp:67
#define L_RED
colors
Definition: M5Display.h:36
#define L_BLUE
Definition: M5Display.h:38
void main_dispatchSyncCommand(int syncCallCommand)
the main sync command (no parameters yet)
void registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
int getFeederType_mainModule()
get the feeder type (Sepper 1,2,3 ...)
Definition: MainModule.cpp:649
#define SYNC_CLICK_SOUND
Definition: MainModule.h:262
void cycle_PTStepperClass()
cycle
void step_PTStepperClass(int steps[][4], int stepCount)
#define IN4
int _ourSteps_PTStepperClass(0)
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
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