#define MOTOR_DATAPIN 0 // 3 pins from ATtiny85
#define MOTOR_LATCHPIN 1 // to the 74HC595
#define MOTOR_CLOCKPIN 2 // shift register
// These positions correspond to motorOutput variable
#define MOTOR_A_PUL 0
#define MOTOR_A_DIR 1
#define MOTOR_B_PUL 2
#define MOTOR_B_DIR 3
#define MOTOR_C_PUL 4
#define MOTOR_C_DIR 5
#define MOTOR_D_PUL 6
#define MOTOR_D_DIR 7
#define MOTOR_E_PUL 8
#define MOTOR_E_DIR 9
#define MOTOR_F_PUL 10
#define MOTOR_F_DIR 11
#define MOTOR_G_PUL 12
#define MOTOR_G_DIR 13
#define MOTOR_H_PUL 14
#define MOTOR_H_DIR 15
#define MOTOR_I_PUL 16
#define MOTOR_I_DIR 17
#define MOTOR_J_PUL 18
#define MOTOR_J_DIR 19
#define MOTOR_K_PUL 20
#define MOTOR_K_DIR 21
#define MOTOR_L_PUL 22
#define MOTOR_L_DIR 23
#define MOTOR_M_PUL 24
#define MOTOR_M_DIR 25
#define MOTOR_N_PUL 26
#define MOTOR_N_DIR 27
#define MOTOR_O_PUL 28
#define MOTOR_O_DIR 29
#define MOTOR_P_PUL 30
#define MOTOR_P_DIR 31
// Variable that stores what is sent to the 74HC595 Shift Register
unsigned long motorOutput;
class stepperMotor{
public:
void stop(void){
enable = 0;
}
void start(void){
enable = 1;
}
unsigned long steps(void){
return stepCount;
}
void init(int _pulsePin, int _dirPin, unsigned long _delayTime, bool _direction){
pulsePin = _pulsePin;
dirPin = _dirPin;
delayTime = _delayTime;
direction = _direction;
togglePulse = LOW;
enable = 0;
changeDirection(direction);
}
void control(void){
currentTime = micros();
if(enable == 1){
if( (currentTime - deltaTime) > delayTime ){
togglePulse = togglePulse == LOW ? HIGH : LOW;
pulseCount++;
if(pulseCount % 2 == 0){
stepCount++;
}
bitWrite(motorOutput,pulsePin,togglePulse);
deltaTime = currentTime;
}
}
}
void changeDirection(bool _direction){
direction = _direction;
bitWrite(motorOutput,dirPin,direction);
}
void changeSpeed(unsigned long _speed){
delayTime = _speed;
}
private:
unsigned long delayTime, currentTime;
unsigned long deltaTime = 0;
unsigned long pulseCount = 0;
unsigned long stepCount = 0;
int pulsePin, dirPin;
bool direction, togglePulse, enable;
};
stepperMotor stepperA, stepperB, stepperC, stepperD, stepperE, stepperF, stepperG, stepperH;
stepperMotor stepperI, stepperJ, stepperK, stepperL, stepperM, stepperN, stepperO, stepperP;
bool stepperAdirection = LOW;
unsigned long int lastStepCount;
void setup() {
pinMode(MOTOR_DATAPIN, OUTPUT);
pinMode(MOTOR_LATCHPIN, OUTPUT);
pinMode(MOTOR_CLOCKPIN, OUTPUT);
digitalWrite(MOTOR_LATCHPIN, HIGH);
// Arguments: PULSE PIN, DIRECTION PIN, SPEED, DIRECTION
stepperA.init(MOTOR_A_PUL, MOTOR_A_DIR, 100, stepperAdirection);
stepperB.init(MOTOR_B_PUL, MOTOR_B_DIR, 200, stepperAdirection);
stepperC.init(MOTOR_C_PUL, MOTOR_C_DIR, 300, stepperAdirection);
stepperD.init(MOTOR_D_PUL, MOTOR_D_DIR, 400, stepperAdirection);
stepperE.init(MOTOR_E_PUL, MOTOR_E_DIR, 500, stepperAdirection);
stepperF.init(MOTOR_F_PUL, MOTOR_F_DIR, 600, stepperAdirection);
stepperG.init(MOTOR_G_PUL, MOTOR_G_DIR, 700, stepperAdirection);
stepperH.init(MOTOR_H_PUL, MOTOR_H_DIR, 800, stepperAdirection);
stepperI.init(MOTOR_I_PUL, MOTOR_I_DIR, 900, stepperAdirection);
stepperJ.init(MOTOR_J_PUL, MOTOR_J_DIR, 1000, stepperAdirection);
stepperK.init(MOTOR_K_PUL, MOTOR_K_DIR, 1100, stepperAdirection);
stepperL.init(MOTOR_L_PUL, MOTOR_L_DIR, 1200, stepperAdirection);
stepperM.init(MOTOR_M_PUL, MOTOR_M_DIR, 1300, stepperAdirection);
stepperN.init(MOTOR_N_PUL, MOTOR_N_DIR, 1400, stepperAdirection);
stepperO.init(MOTOR_O_PUL, MOTOR_O_DIR, 1500, stepperAdirection);
stepperP.init(MOTOR_P_PUL, MOTOR_P_DIR, 1600, stepperAdirection);
delay(2000);
stepperA.start();
stepperB.start();
stepperC.start();
stepperD.start();
stepperE.start();
stepperF.start();
stepperG.start();
stepperH.start();
stepperI.start();
stepperJ.start();
stepperK.start();
stepperL.start();
stepperM.start();
stepperN.start();
stepperO.start();
stepperP.start();
lastStepCount = stepperA.steps();
}
void loop() {
stepperA.control();
stepperB.control();
stepperC.control();
stepperD.control();
stepperE.control();
stepperF.control();
stepperG.control();
stepperH.control();
stepperI.control();
stepperJ.control();
stepperK.control();
stepperL.control();
stepperM.control();
stepperN.control();
stepperO.control();
stepperP.control();
if(stepperA.steps() - lastStepCount >= 500){
stepperAdirection = stepperAdirection == LOW ? HIGH : LOW;
stepperA.changeDirection(stepperAdirection);
stepperB.changeDirection(stepperAdirection);
stepperC.changeDirection(stepperAdirection);
stepperD.changeDirection(stepperAdirection);
stepperE.changeDirection(stepperAdirection);
stepperF.changeDirection(stepperAdirection);
stepperG.changeDirection(stepperAdirection);
stepperH.changeDirection(stepperAdirection);
stepperI.changeDirection(stepperAdirection);
stepperJ.changeDirection(stepperAdirection);
stepperK.changeDirection(stepperAdirection);
stepperL.changeDirection(stepperAdirection);
stepperM.changeDirection(stepperAdirection);
stepperN.changeDirection(stepperAdirection);
stepperO.changeDirection(stepperAdirection);
stepperP.changeDirection(stepperAdirection);
lastStepCount = stepperA.steps();
}
// Your code goes here.
// This updates the Shift Register Values in each loop
digitalWrite(MOTOR_LATCHPIN, LOW);
shiftOut(MOTOR_DATAPIN, MOTOR_CLOCKPIN, MSBFIRST, (motorOutput >> 24));
shiftOut(MOTOR_DATAPIN, MOTOR_CLOCKPIN, MSBFIRST, (motorOutput >> 16));
shiftOut(MOTOR_DATAPIN, MOTOR_CLOCKPIN, MSBFIRST, (motorOutput >> 8));
shiftOut(MOTOR_DATAPIN, MOTOR_CLOCKPIN, MSBFIRST, (motorOutput >> 0));
digitalWrite(MOTOR_LATCHPIN, HIGH);
}