Hello
I have a few states where things happen in a sequence. The states work fine, but the problem I have is that the stepper will not run for 400 steps.
The sequence is:
hold before shot
SHOT FIRED
hold after shot
STEPPER SHOULD RUN
Is interval: 6000
hold before shot
SHOT FIRED
hold after shot
STEPPER SHOULD RUN
Is interval: 6000
etc
I thought I did it correct to have the stepper run for 400 steps when it hits that part, but I'm wrong. I hear the stepper step 1 step, once every second (total duration of hold time is 6 seconds, so it steps 6 steps).
I need the stepper to run for 400 steps when it hits that, regardless of hold time or interval.
What am I doing wrong?
Oh, this is part of a much larger sketch but the problem area is: 'case S_MOVE: // MOVE THE STEPPE n STEPS at n SPEED'
the code:
#include <AccelStepper.h>
long PreviousDelayMillis = 0;
int currentType = 0;
int value = 0;
int numIncr = 0;
int Min = 0;
int Max = 0;
int LinEndleft = 0; // ENDSTOP LEFT ---> use as: if (LinEndleft == HIGH) { blaaah }
int LinEndRight = 0; // ENDSTOP RIGHT
int Confirm = 0; // confirm of init gezet is
int InitMode = 0; // zijn we in initmodes? Zo ja welke dan?
int InitStatus = 0; // init gedaan na opstarten? 0=nee
const int Step1End = 0; // eindpunt is 0, stepper telt af!
long Step1Start = 2666; // startpunt is de waarde uit INIT
const int Step2End = 0; // eindpunt is 0, stepper telt af!
long Step2Start = 666; // startpunt is de waarde uit INIT
const int Step2Max = 360; // mximaal aantal graden rotatie
int Srev = 200; // number of steps per rev
char Fm = 2; // MicroStepping Factor
char Pitch = 2; // pitch in mm
int Nt = 20; // Number of teeth
float Pstep = (Srev * Fm) / (Pitch * Nt); // aantal steps per mm
float Pdist = 1 / Pstep; // aantal mm per steps
int TimeBetwenShots = 10; // USER INPUT tussen 4 - 1000 secondes between each shot
int TrackLength = 10000; //stappen kan ook maar lengte is nu length of track in mm
int RunTime = 800; //1 hour to complete a 1000mm track komt van userinput vlaues [1] in seconden
int HoldTime = 4; //total holding time /s
float NumShots = RunTime / TimeBetwenShots; // total time devided by # shots
float MMBetweenShots = TrackLength / NumShots; // tracklength devided by shots
int Speed = 500; // StepperSpeed = 400 steps p seconde
AccelStepper stepper1(1, 5, 4); // stepper 1 pin setup 14 step - 15 dir
void setup() {
Serial.begin(9600);
}
void loop() {
ShootIt();
}
unsigned long previousMillis = 0; // store last time
const long interval = (TimeBetwenShots - HoldTime) * 1000; // seconds to ms minus de holdtijd
const long intervalNorm = TimeBetwenShots * 1000; // seconds to ms for shots fired in NORM
unsigned long previousHold = 0; // store last time a hold ws done
unsigned long previousRelease = 0; // store last time a hold ws done
const long ReleaseTime = 3000; // the holdtime before and after the shot, has to be subtracted from the TimeBetwenShots lateron!
//States
const int S_HOLD = 0;
const int S_SHOOT = 1;
const int S_RELEASE = 2;
const int S_MOVE = 3;
int StepperHasRun = 0;
int ShotCounterStatus = 0;
void ShootIt() {
static int state = S_HOLD ;
switch (state)
{
case S_HOLD: // hold the camera steady BEFORE taking a shot for n seconds
if (previousHold == 0) {
previousHold = millis();
}
if (millis() - previousHold >= (HoldTime * 1000) / 2)
{
Serial.println(F("hold before shot"));
previousHold = 0;
state = S_SHOOT;
}
break;
case S_SHOOT:
// fire the remote focus and shutter on PIN 11 + 12
shoot();
state = S_RELEASE;
break;
case S_RELEASE: // hold the camera steady BEFORE taking a shot for 2 seconds
if (previousRelease == 0) {
previousRelease = millis();
}
if (millis() - previousRelease >= (HoldTime * 1000) / 2)
{
Serial.println(F("hold after shot"));
previousRelease = 0;
state = S_MOVE;
}
break;
// ## PROBLEM AREA #############################################
case S_MOVE: // MOVE THE STEPPE n STEPS at n SPEED
stepper1.setSpeed(800);
stepper1.moveTo(400);
if (StepperHasRun == 0 && stepper1.distanceToGo() != 0) // StepperHasRun is 0
{
stepper1.run();
// Serial.print(stepper1.distanceToGo());
}
if (previousMillis == 0) {
previousMillis = millis();
}
if (millis() - previousMillis >= interval)
{
Serial.print(F("Is interval: ")); Serial.println(interval);
previousMillis = 0;
state = S_HOLD;
StepperHasRun = 0;
}
break;
} // end switch
} // end ShootIt()
void shoot()
{
digitalWrite(11, HIGH); // focus up
digitalWrite(12, HIGH); // shutter up
digitalWrite(12, LOW); // shutter down
digitalWrite(11, LOW); // focus diwn
Serial.println(F("SHOT FIRED"));
ShotCounterStatus = ShotCounterStatus + 1;
}