#include <AccelStepper.h>
#define stp 7
#define dir 8
AccelStepper carr = AccelStepper(1, stp, dir);
void calculation(float Gl, float Trl, float Nh, float Hhd);
void motorRunning(int steps, int speed);
float *distances;
float *rotations;
int *pulses;
float e,x;
int trayColmn;
void setup() {
carr.setMaxSpeed(2000);
Serial.begin(9600);
Serial.println("rotations");
delay(2000);
calculation(1115, 522, 18, 35);
}
void loop() {
for (int i = 0; i < trayColmn; i++) {
motorRunning(pulses[i],300);
}
}
void motorRunning(int steps, int speed) {
carr.setCurrentPosition(0);
while (carr.currentPosition() != steps) {
carr.setSpeed(speed);
carr.runSpeed();
}
delay(2000);
carr.setCurrentPosition(0);
while (carr.currentPosition() != -steps) {
carr.setSpeed(-speed);
carr.runSpeed();
}
delay(2000);
}
void calculation(float Gl, float Trl, float Nh, float Hhd) {
x = (Trl - (Nh * Hhd)) / 2;
e = (Gl - 200 - Trl) / 2;
int p=Nh/2;
trayColmn=p;
distances = new float[p+1];
rotations = new float[p+1];
pulses = new int[p+1];
distances[0] = e + x;
rotations[0] = distances[0] / 125.66;
pulses[0] = rotations[0] * 200;
for (int i = 1; i < p; i++) {
distances[i] = distances[i - 1] + Hhd;
rotations[i] = distances[i] / 125.66;
pulses[i] = rotations[i] * 200;
}
}