#include <AccelStepper.h>
int light = 1000000;
int lightthresh = 700;
AccelStepper stepper(1, PB2, PB4); // (Typeof driver: with 2 pins, STEP, DIR)
float maxSpeed = 2500; // # of steps per second to speeed up to
float Stop = 0;
unsigned long time;
unsigned long previousAccel = 0;
int interval = 50; // # of milliseconds between speed increases
int pot,PotVal,S;
void setup() {
stepper.setSpeed(20);
stepper.setMaxSpeed(maxSpeed);
}
void loop() {
light = analogRead(3);
PotVal = analogRead(0);
S = map(PotVal, 0,1023, 100 ,maxSpeed);
if (light < lightthresh){
while (stepper.speed() < S) {
time = millis();
if (time > previousAccel + interval) {
previousAccel = time;
stepper.setSpeed(stepper.speed() + 50);
}
stepper.runSpeed();
}
stepper.setSpeed(S);
stepper.runSpeed();
}
if (light > lightthresh){
while (stepper.speed() > Stop) {
time = millis();
if (time > previousAccel + interval) {
previousAccel = time;
stepper.setSpeed(stepper.speed() - 50);
}
stepper.runSpeed();
}
stepper.setSpeed(Stop);
stepper.runSpeed();
}
}