From 279a5cf539054f3d36e3c3727173f9c859228a6b Mon Sep 17 00:00:00 2001 From: rsformoso Date: Tue, 7 Jul 2026 12:46:31 +0800 Subject: [PATCH] Use one array to manage step sequence --- .../rotating-display-controller.ino | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/rotating-display-controller/rotating-display-controller.ino b/rotating-display-controller/rotating-display-controller.ino index 6086e50..99df830 100644 --- a/rotating-display-controller/rotating-display-controller.ino +++ b/rotating-display-controller/rotating-display-controller.ino @@ -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 + } } }