rgoo
40
const int SensorPin = 2; //Interrupt Pin (nur 2 oder 3 @ Arduino Uno)
const byte Brake = 8;//Pinausgang Bremse, HIGH = Vollbremsung
const byte PWM = 9;// Pinausgang für Drehzahl
byte power = 150;// Motor-Drehzahl-Normalbetrieb
const byte Drehrichtung = 10;// Pin Drehrichtungswechsel LOW/HIGH Drehrichtung
unsigned long FlagMillis;
unsigned int Counter;
bool setupStep = LOW;
bool Normalbetrieb = LOW;
void motor(bool Stop, bool Richtung, int pwmSignal) {
digitalWrite (Brake, Stop);
digitalWrite (Drehrichtung, Richtung);
analogWrite (PWM, pwmSignal);
}
void M_Stop() {
motor(HIGH, LOW, 0);
}
void M_Hoch() {
motor(LOW, HIGH, power);
}
void M_Runter() {
motor(LOW, LOW, power);
}
void count () {
Counter++;
}
void setup() {
Serial.begin(9600);
pinMode(SensorPin, INPUT); //definiertes Potenzial (HIGH/LOW) von einem Hall-Sensor
pinMode(Brake , OUTPUT);
pinMode(PWM , OUTPUT);
pinMode(Drehrichtung , OUTPUT);//Drehrichung
}
void loop() {
motorSetup();
motorMatik();
Serial.print("\t Counts: ");
Serial.print(Counter);
Serial.print("\t setupStep: ");
Serial.println(setupStep);
}
void motorSetup() {
if (!setupStep) { //Variable um den Setupvorgang vom Normalbetrieb zu trennen
Counter = 0;
attachInterrupt(digitalPinToInterrupt(SensorPin), count, CHANGE);
delay(500);
detachInterrupt(digitalPinToInterrupt(SensorPin));//Stop zur Ermittlung der Drehzahl über 500ms
motor(LOW, LOW, 30); //Kriechgang (30) zum Anfahren der Endposition (Anschlag)
}
static unsigned long SetupMillis = 0;
if ((Counter < 10) && ((millis() - SetupMillis >= 1000))) {// 1s um den Anlaufprozess zu überbrücken
M_Stop (); // Drehzahl sinkt am Anschlag unter (10) , der Motor stopt
setupStep = HIGH;
Counter = 1000;// Counter wir af 1000 gesetzt um ein "Überfahren" in den negativen Bereich zu verhindern
}
// Der Zähler wird auf Positionsmods umgestellt, absolute Werte werden gespeichert
if (setupStep && !Normalbetrieb) {
attachInterrupt(digitalPinToInterrupt(SensorPin), count, CHANGE);
motor (LOW, HIGH, 120); //Richtungswechsel nach Anschlag
if (Counter >= 1100) {//Fahrt auf Ausgangsposition (z.B X + 100)
M_Stop ();//Setup-Phase ist beendet, Motor steht in Ausgangsposition
Normalbetrieb = HIGH;//Umschalten auf Normalbetrieb
}
}
}
void motorMatik () {
if (Normalbetrieb) {
attachInterrupt(digitalPinToInterrupt(SensorPin), count, CHANGE);
}
}