const int TasterAuf = 32; //zum Aufmachen
const int TasterAb = 33; //Taster zum Zumachen
const int OEndschalter = 25; //oberer Endschalter
const int UEndschalter = 26; //unterer Endschalter
const int Lichtschranke = 27; //Lichtschranke
//Motor anschlüsse
const int EN1 = 19;
const int IN1 = 18;
const int IN2 = 5;
//Motor Zustände
const int MOTOR_STOP = 0;
const int MOTOR_AUF = 1;
const int MOTOR_AB = 2;
//Zwischenspeicher für Zustand
int MOTOR_STATUS = MOTOR_STOP;
void setup() {
Serial.begin(115200);
//Motor anschlüsse zu Outputs
pinMode(EN1, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
//Motor Stoppen
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
//Pin modes
pinMode(TasterAuf, INPUT_PULLUP);
pinMode(TasterAb, INPUT_PULLUP);
pinMode(OEndschalter, INPUT_PULLUP);
pinMode(UEndschalter, INPUT_PULLUP);
pinMode(Lichtschranke, INPUT_PULLUP);
}
void loop() {
//Taster kontrolle
if (digitalRead(TasterAuf) == LOW && MOTOR_STATUS != MOTOR_AUF) {
motorauf();
}
if (digitalRead(TasterAb) == LOW && MOTOR_STATUS != MOTOR_AB) {
motorab();
}
//Endschalter kontrolle
if (digitalRead(OEndschalter) == LOW && MOTOR_STATUS == MOTOR_AUF) {
motorstop();
}
if (digitalRead(UEndschalter) == LOW && MOTOR_STATUS == MOTOR_AB) {
motorstop();
}
//Lichtschranken kontrolle
if (digitalRead(Lichtschranke) == LOW && MOTOR_STATUS == MOTOR_AB) {
motorauf();
}
}
//Funktion um Garagentor aufzumachen
void motorauf() {
MOTOR_STATUS = MOTOR_AUF;
analogWrite(EN1, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
Serial.println("Garagentor auf");
}
//Funktion um Garagentor zuzumachen
void motorab() {
MOTOR_STATUS = MOTOR_AB;
analogWrite(EN1, 255);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
Serial.println("Garagentor zu");
}
//Funktion um Garagentor zustopen
void motorstop() {
MOTOR_STATUS = MOTOR_STOP;
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
Serial.println("Garagentor stop");
}