Use one array to manage step sequence
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// --- Hardware Configuration ---
|
||||
// Pins connected to the ULN2003 driver (or similar stepper driver)
|
||||
#define IN1 9
|
||||
#define IN2 10
|
||||
#define IN3 11
|
||||
@@ -6,21 +7,13 @@
|
||||
|
||||
// Step sequences for 28BYJ-48
|
||||
// Clockwise sequence
|
||||
int stepSequenceCW[4][4] = {
|
||||
int stepSequence[4][4] = {
|
||||
{ 1, 0, 0, 0 },
|
||||
{ 0, 1, 0, 0 },
|
||||
{ 0, 0, 1, 0 },
|
||||
{ 0, 0, 0, 1 }
|
||||
};
|
||||
|
||||
// Counter-Clockwise sequence (reverse of CW)
|
||||
int stepSequenceCCW[4][4] = {
|
||||
{ 0, 0, 0, 1 },
|
||||
{ 0, 0, 1, 0 },
|
||||
{ 0, 1, 0, 0 },
|
||||
{ 1, 0, 0, 0 }
|
||||
};
|
||||
|
||||
// --- Configuration Constants ---
|
||||
const int STEPS_PER_REVOLUTION = 2048; // 28BYJ-48 standard (512 steps * 4 gear reduction)
|
||||
const float DEGREES_PER_STEP = 360.0 / STEPS_PER_REVOLUTION;
|
||||
@@ -66,15 +59,15 @@ void updateStepInterval() {
|
||||
}
|
||||
|
||||
void calculateStepsForAngle(float angle) {
|
||||
// Determine direction
|
||||
// Determine direction based on angle sign
|
||||
if (angle >= 0) {
|
||||
_directionCW = true;
|
||||
_directionCW = true; // Clockwise
|
||||
} else {
|
||||
_directionCW = false;
|
||||
angle = -angle; // Work with absolute value
|
||||
_directionCW = false; // Counter-Clockwise
|
||||
angle = -angle; // Work with absolute value for calculation
|
||||
}
|
||||
|
||||
// Calculate steps
|
||||
// Calculate total steps needed
|
||||
_stepCount = (int)(angle / DEGREES_PER_STEP);
|
||||
|
||||
// Update speed interval immediately
|
||||
@@ -84,26 +77,30 @@ void calculateStepsForAngle(float angle) {
|
||||
_currState = MOVE;
|
||||
_stepsRemaining = _stepCount;
|
||||
_stepTime = millis();
|
||||
_currentStepIndex = 0;
|
||||
_currentStepIndex = 0; // Start from the beginning of the sequence
|
||||
|
||||
// Reset wait timer just in case
|
||||
_waitTime = 0;
|
||||
}
|
||||
|
||||
void runStep() {
|
||||
int* sequence = _directionCW ? stepSequenceCW[0] : stepSequenceCCW[0]; // Placeholder logic
|
||||
|
||||
// Actually, we need to access the 2D array correctly
|
||||
int (*seq)[4] = _directionCW ? stepSequenceCW : stepSequenceCCW;
|
||||
// Get the current step pattern from the single array
|
||||
const int* step = stepSequence[_currentStepIndex];
|
||||
|
||||
setStep(seq[_currentStepIndex][0],
|
||||
seq[_currentStepIndex][1],
|
||||
seq[_currentStepIndex][2],
|
||||
seq[_currentStepIndex][3]);
|
||||
|
||||
_currentStepIndex++;
|
||||
if (_currentStepIndex >= 4) {
|
||||
_currentStepIndex = 0;
|
||||
// Set the output pins
|
||||
setStep(step[0], step[1], step[2], step[3]);
|
||||
|
||||
// Update index based on direction
|
||||
if (_directionCW) {
|
||||
_currentStepIndex++;
|
||||
if (_currentStepIndex >= 4) {
|
||||
_currentStepIndex = 0; // Wrap around to start
|
||||
}
|
||||
} else {
|
||||
_currentStepIndex--;
|
||||
if (_currentStepIndex < 0) {
|
||||
_currentStepIndex = 3; // Wrap around to end for CCW
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user