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