/************************************************** *****/
/*PIN Defines, je nach Pin-Belegung bitte anpassen*/
#define PIN_STEP 3 //Step Pin für Schrittmotor
#define PIN_DIR 6 //Direction Pin für Schrittmotor
#define PIN_EN 8 //Enable Pin für Schrittmotor
#define PIN_MIC 9 //Sound Sensor Input
#define PIN_SW_HOME 10 //Home Button (Taster), Pullup ist intern gesetzt, Taster gegen Masse verkabeln
#define PIN_SW_EN 11 //Freigabe (Schalter), Pullup ist intern gesetzt, Schalter gegen Masse verkabeln
/************************************************** *****/
/*USER Defines, nach belieben anpassen*/
#define PRE_DELAY 2500 //Zeit in ms, die nach Triggerung gewartet wird bevor der Tisch sich dreht
#define ANGLE_INCREMENT 5.0 //Winkel in Grad, der pro Trigger verfahren wird
#define SPEED 1000 //Zeit in ms zum Verfahren des oben angegebenen Winkels (Eingabe nur Ganzzahlig)
#define DRIVE_DIR 0 //Richtung in der der Tisch automatisch weiterfährt (0 oder 1)
#define MIC_TRIGGER 50 //Ansprechschwelle, um den Trigger für die Drehung zu setzten, 10 bedeutet 10 Schwingungen am Mikrofon damit Trigger erkannt wird
#define STEPS_PER_DEGREE 60.50000 //Nötige Schritte für 1° (mind. 5 Nachkommastellen eingeben)
/************************************************** *****/
/*Sontige Variablen, bedarfen keiner Anpassung*/
int micState = 0;
int oldMicState = 0;
int swHomeState = 1;
int oldSwHomeState = 1;
int micStateChanged = 0;
int enableWritten = 0;
float stepIncrement = ANGLE_INCREMENT*STEPS_PER_DEGREE;
float totalStepsF = 0;
uint16_t totalStepsU = 0;
unsigned int stepDelay = (unsigned int)round(((float)SPEED)*50/stepIncrement);
/************************************************** *****/
/*Sources wirtten by Luca Zechner 23.01.2020*/
void setup()
{
Serial.begin(115200);
Serial.print("Automatischer Drehtisch\n");
Serial.print("Steps pro Trigger: ");
Serial.print(stepIncrement);
Serial.print("\n");
inits();
Serial.print("***********************\n");
Serial.print("Warte auf Aktivierung per Schalter...\n");
}
void loop()
{
while(digitalRead(PIN_SW_EN) == 0)
{
if(digitalRead(PIN_SW_EN) == 0 && enableWritten == 0)
{
digitalWrite(PIN_EN, LOW);
Serial.print("Drehtisch starr, Akustische Triggerung aktiviert\n");
enableWritten = 1;
}
swHomeState = digitalRead(PIN_SW_HOME);
if(swHomeState != oldSwHomeState)
{
Serial.print("Coming Home, Drehtisch wird auf 0 Position gedreht...");
stepper_drive_back();
Serial.print("abgeschlossen\n");
delay(100);
swHomeState = digitalRead(PIN_SW_HOME);
}
oldSwHomeState = swHomeState;
micState = digitalRead(PIN_MIC);
if(micState != oldMicState)
{
micStateChanged++;
}
oldMicState = micState;
if(micStateChanged >= 50)
{
Serial.print("Trigger, Warte...");
delay(PRE_DELAY);
Serial.print("Drehung...");
stepper_drive_auto();
Serial.print("abgeschlossen, ges. Winkel: ");
Serial.print((float)(totalStepsU / STEPS_PER_DEGREE));
Serial.print("°\n");
micStateChanged = 0;
}
else
delay(1);
}
if(digitalRead(PIN_SW_EN) == 1 && enableWritten == 1)
{
Serial.print("Drehtisch frei, Akustische Triggerung deaktiviert\n");
}
digitalWrite(PIN_EN, HIGH);
micState = 0;
enableWritten = 0;
oldMicState = 0;
swHomeState = 1;
oldSwHomeState = 1;
micStateChanged = 0;
totalStepsF = 0;
totalStepsU = 0;
}
void stepper_drive_auto()
{
digitalWrite(PIN_DIR, DRIVE_DIR);
uint16_t steps = (uint16_t)round(stepIncrement + totalStepsF - ((float)totalStepsU));
for(uint16_t i = 0; i < steps; i++)
{
digitalWrite(PIN_STEP, HIGH);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
digitalWrite(PIN_STEP, LOW);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
}
totalStepsF += stepIncrement;
totalStepsU += steps;
}
void stepper_drive_back()
{
if(DRIVE_DIR == 1)
{
digitalWrite(PIN_DIR, 0);
}
else
{
digitalWrite(PIN_DIR, 1);
}
for(uint16_t i = 0; i < totalStepsU; i++)
{
digitalWrite(PIN_STEP, HIGH);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
digitalWrite(PIN_STEP, LOW);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
delayMicroseconds(stepDelay);
}
totalStepsF = 0;
totalStepsU = 0;
}
void inits()
{
pinMode(PIN_STEP, OUTPUT);
digitalWrite(PIN_STEP, LOW);
pinMode(PIN_DIR, OUTPUT);
digitalWrite(PIN_DIR, LOW);
pinMode(PIN_EN, OUTPUT);
digitalWrite(PIN_EN, HIGH);
pinMode(PIN_MIC, INPUT);
pinMode(PIN_SW_HOME, INPUT_PULLUP);
pinMode(PIN_SW_EN, INPUT_PULLUP);
}