/* Example for MobaTools
Moving a stepper back and forth
*/
#include <MobaTools.h>
// Adjust pins, steps and time as needed
const byte dirPin = 8;
const byte stepPin = 9;
const int stepsPerRev = 800; // Steps per Revolution ( example with 1/4 microsteps )
const long targetPos = 6400; // stepper moves between 0 and targetpos
long nextPos;
const byte Buzzer = 10;
const byte Led = 11;
MoToTimebase Buzzerzeit;
MoToTimebase LedBlitz;
MoToStepper myStepper ( stepsPerRev, STEPDIR );
MoToTimer stepperPause; // Pause between stepper moves
bool stepperRunning;
const long BuzzerInterval = 10000;
const long BlitzInterval = 500;
const long stepperPauseInterval = 2000;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
Buzzerzeit.setBasetime( BuzzerInterval );
LedBlitz.setBasetime ( BlitzInterval );
myStepper.attach( stepPin, dirPin );
myStepper.setSpeed( 700 ); // 70 Rev/Min ( if stepsPerRev is set correctly )
myStepper.setRampLen( 1200 );
stepperRunning = true;
//pinMode(Buzzer, OUTPUT);
pinMode(Led, OUTPUT);
digitalWrite(Led, HIGH);
}
void loop() {
if ( stepperRunning ) {
// Wait till stepper has reached target, then set pause time
if ( !myStepper.moving() ) {
Serial.print("targetpos reached myStepper.readSteps()=");
Serial.println(myStepper.readSteps());
Serial.println();
// stepper has reached target, start pause
stepperPause.setTime( stepperPauseInterval );
stepperRunning = false;
}
}
else { // stepper doesn't move, wait till stepperPause time expires
if ( stepperPause.expired() ) {
// stepperPause time expired. Start stepper in opposite direction
if ( nextPos == 0 ) {
nextPos = targetPos;
}
else {
nextPos = 0;
}
Serial.print("pausing finished ");
Serial.print("now moving to pos=");
myStepper.moveTo( nextPos );
Serial.println(nextPos);
stepperRunning = true;
}
}
if ( Buzzerzeit.tick() ) { // Buzzerzeit abgelaufen
tone(Buzzer, 440, 500);
digitalWrite( Led, HIGH ); // LED ein
LedBlitz.setBasetime( BlitzInterval ); // Blitztimer starten
}
// Led ist an und Led On-Zeit vorbei
if ( digitalRead(Led) == HIGH && LedBlitz.tick() ) {
digitalWrite( Led, LOW ); // Led aus
LedBlitz.setBasetime( -BlitzInterval ); // timer stoppen
}
}