/*
Forum: https://forum.arduino.cc/t/timer-problem-accelstepper-library/1341227
Wokwi: https://wokwi.com/projects/419796124630104065
Originalsketch aus Post 1 Wokwi: https://wokwi.com/projects/419795832434931713
2025/01/11
Angepasst durch ec2021
*/
/* Sketch für eine Rotationsentwicklungs-Maschine für Jobo Dosen
15xx und 25xx/28xx mit Nema 17 Stepper-Motor und DRV8825
8 Umdrechungen im Uhrzeigersinn und zurück. Ca. 70 U/min */
#include <Arduino.h>
#include "AccelStepper.h"
const int BUZZER = 3;
const int ledPin = 4;
const unsigned long stepperDelay = 300;
const unsigned long ledIntervall = 5000; // 60000;
const unsigned long blinkDuration = 300;
const unsigned long buzzerIntervall = 5000; //60000;
const int stepperRechts = 3200;
const int stepperLinks = 0;
enum StatusTyp {WARTERECHTS, RECHTS, WARTELINKS, LINKS};
StatusTyp status = WARTERECHTS;
unsigned long ledTimer = 0;
unsigned long buzzerTimer = 0;
unsigned long stepperTimer = 0;
int LedStatus = HIGH;
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 0
#define stepPin 1
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(470); //Half-Step
stepper.setAcceleration(400); //Half-Step
ledTimer = millis();
buzzerTimer = millis();
}
void loop() {
stateMachine();
beep();
blink();
}
void stateMachine() {
switch (status) {
case WARTERECHTS:
if (millis() - stepperTimer > stepperDelay) {
status = RECHTS;
stepper.moveTo(stepperRechts);
}
break;
case RECHTS:
stepper.run();
if (stepper.currentPosition() >= stepperRechts) {
status = WARTELINKS;
stepperTimer = millis();
}
break;
case WARTELINKS:
if (millis() - stepperTimer > stepperDelay) {
status = LINKS;
stepper.moveTo(stepperLinks);
}
break;
case LINKS:
stepper.run();
if (stepper.currentPosition() <= stepperLinks) {
status = WARTERECHTS;
stepperTimer = millis();
}
break;
}
}
void beep() {
if (millis() - buzzerTimer >= buzzerIntervall ) {
buzzerTimer = millis();
tone(BUZZER, 500, 1000); //tone of 500Hz for 1 second/1000 milliseconds
}
}
void blink() {
static byte ledStatus = HIGH;
if (millis() - ledTimer >= ledIntervall && ledStatus == HIGH) {
ledTimer = millis();
ledStatus = LOW;
digitalWrite(ledPin, ledStatus);
}
if (millis() - ledTimer >= blinkDuration && ledStatus == LOW) {
ledStatus = HIGH;
digitalWrite(ledPin, ledStatus);
}
}
// ACHTUNG:
// Hilfslösung, da der Attiny85 bei Wokwi tone() auf Pin PB3 nicht unterstützt
//
void tone(byte Pin, int f, int d) {
pinMode(Pin, OUTPUT);
for (int i = 0; i < 5; i++) {
digitalWrite(Pin, HIGH);
delay(1);
digitalWrite(Pin, LOW);
}
}