You’ve seen the video(s), a vast sea of shiny balls suspended from a ceiling panel, moving up and down to make interesting shapes.
I’m looking to make something similar. Bought a couple of 28BYJ-48 geared motors with ULN2003 “driver” that buffers 4 Arduino outputs to sink current from 5V thru 4 motor phases to make it turn.
Used a Stepper.h example, got one to spin and move a holiday ornament up and down.
www.youtube.com/watch?v=hXEccqlsE6M
Added a 2nd, got one to move, than the other.
Now, the interesting part. How to make both move at the same time? Wrote this program to make them rotate at different speeds and for different distances, using arrays to hold values.
Arrays are sized for 17 motors on a Mega eventually, I have 2 wired up on a '1284P and am running the code to simulate having 6 connected. #2 seems to run pretty smooth, #1 seems a little jerky in comparison.
Is this just a play-with-settings-until-things-smooth-out kind of thing? Is 16 or 17 with a Mega going to be possible? Or is there a better way to achieve concurrent movement with different speeds and distance (# of steps)?
#include <Stepper.h> //include the function library, standard Arduino library
#define STEPS 64 // 64 steps per rev // hardware dependent
int speed[] = {
300,300,300,300,300,300,0,0,0,0,0,0,0,0,0,0,0,}; // speed to move motor
int numSteps[] = {
6000,5200,4000,3000,2000,1000,0,0,0,0,0,0,0,0,0,0,0,}; // steps to move in total
int countSteps[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; // track how much moved
byte stepsDir[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; // up or down, 0 or 1
int stepSize[] = {
9,3,4,5,2,6,0,0,0,0,0,0,0,0,0,0,0,}; // how big of a step
Stepper stepper0(STEPS, 5, 3, 4, 2); //create the stepper0 <<< Order here is important
Stepper stepper1(STEPS, 9, 7, 8, 6); //create the stepper1 <<< Order here is important
Stepper stepper2(STEPS, 13, 11, 12, 10); //create the stepper2 <<< Order here is important
Stepper stepper3(STEPS, 17, 15, 16, 14); //create the stepper3 <<< Order here is important
Stepper stepper4(STEPS, 21, 19, 20, 18); //create the stepper4 <<< Order here is important
Stepper stepper5(STEPS, 25, 23, 24, 22); //create the stepper5 <<< Order here is important
/*
Stepper stepper6(STEPS, 29, 27, 28, 26); //create the stepper6 <<< Order here is important
Stepper stepper7(STEPS, 33, 31, 32, 20); //create the stepper7 <<< Order here is important
Stepper stepper8(STEPS, 37, 35, 36, 34); //create the stepper8 <<< Order here is important
Stepper stepper9(STEPS, 41, 39, 40, 38); //create the stepper9 <<< Order here is important
Stepper stepper10(STEPS, 45, 43, 44, 42); //create the stepper10 <<< Order here is important
Stepper stepper11(STEPS, 49, 47, 48, 46); //create the stepper11 <<< Order here is important
Stepper stepper12(STEPS, 53, 51, 52, 50); //create the stepper12 <<< Order here is important
Stepper stepper13(STEPS, 57, 55, 56, 54); //create the stepper13 <<< Order here is important
Stepper stepper14(STEPS, 61, 59, 60, 58); //create the stepper14 <<< Order here is important
Stepper stepper15(STEPS, 65, 63, 64, 62); //create the stepper15 <<< Order here is important
Stepper stepper16(STEPS, 69, 67, 68, 66); //create the stepper16 <<< Order here is important
*/
void setup()
{
Serial.begin(9600); // initialize serial communication:
stepper0.setSpeed(speed[0]); //set speed
stepper1.setSpeed(speed[1]); //set speed
stepper2.setSpeed(speed[2]); //set speed
stepper3.setSpeed(speed[3]); //set speed
stepper4.setSpeed(speed[4]); //set speed
stepper5.setSpeed(speed[5]); //set speed
}
void loop()
{
// Motor 0
if (stepsDir[0] == 0){
countSteps[0] = countSteps[0] + stepSize[0];
if (countSteps[0] >= numSteps[0]){
stepsDir[0] = 1;
}
}
if (stepsDir[0] == 1){
countSteps[0] = countSteps[0] - stepSize[0];
if (countSteps[0] <= 0){
stepsDir[0] = 0;
}
}
if (stepsDir[0] == 0){
stepper0.step(stepSize[0]); //move one direction
}
if (stepsDir[0] == 1){
stepper0.step(-stepSize[0]); //move the other direction
}
// Motor 1
// *****************************
if (stepsDir[1] == 0){
countSteps[1] = countSteps[1] + stepSize[1];
if (countSteps[1] >= numSteps[1]){
stepsDir[1] = 1;
}
}
if (stepsDir[1] == 1){
countSteps[1] = countSteps[1] - stepSize[1];
if (countSteps[1] <= 0){
stepsDir[1] = 0;
}
}
if (stepsDir[1] == 0){
stepper1.step(stepSize[1]); //move one direction
}
if (stepsDir[1] == 1){
stepper1.step(-stepSize[1]); //move the other direction
}
// Motor 2
// *****************************
if (stepsDir[2] == 0){
countSteps[2] = countSteps[2] + stepSize[2];
if (countSteps[2] >= numSteps[2]){
stepsDir[2] = 1;
}
}
if (stepsDir[2] == 1){
countSteps[2] = countSteps[2] - stepSize[2];
if (countSteps[2] <= 0){
stepsDir[2] = 0;
}
}
if (stepsDir[2] == 0){
stepper2.step(stepSize[2]); //move one direction
}
if (stepsDir[2] == 1){
stepper2.step(-stepSize[2]); //move the other direction
}
// Motor 3
// *****************************
if (stepsDir[3] == 0){
countSteps[3] = countSteps[3] + stepSize[3];
if (countSteps[3] >= numSteps[3]){
stepsDir[3] = 1;
}
}
if (stepsDir[3] == 1){
countSteps[3] = countSteps[3] - stepSize[3];
if (countSteps[3] <= 0){
stepsDir[3] = 0;
}
}
if (stepsDir[3] == 0){
stepper3.step(stepSize[3]); //move one direction
}
if (stepsDir[3] == 1){
stepper3.step(-stepSize[3]); //move the other direction
}
// Motor 4
// *****************************
if (stepsDir[4] == 0){
countSteps[4] = countSteps[4] + stepSize[4];
if (countSteps[4] >= numSteps[4]){
stepsDir[4] = 1;
}
}
if (stepsDir[4] == 1){
countSteps[4] = countSteps[4] - stepSize[4];
if (countSteps[4] <= 0){
stepsDir[4] = 0;
}
}
if (stepsDir[4] == 0){
stepper4.step(stepSize[4]); //move one direction
}
if (stepsDir[4] == 1){
stepper4.step(-stepSize[4]); //move the other direction
}
// Motor 5
// *****************************
if (stepsDir[5] == 0){
countSteps[5] = countSteps[5] + stepSize[5];
if (countSteps[5] >= numSteps[5]){
stepsDir[5] = 1;
}
}
if (stepsDir[5] == 1){
countSteps[5] = countSteps[5] - stepSize[5];
if (countSteps[5] <= 0){
stepsDir[5] = 0;
}
}
if (stepsDir[5] == 0){
stepper5.step(stepSize[5]); //move one direction
}
if (stepsDir[5] == 1){
stepper5.step(-stepSize[5]); //move the other direction
}
}