int stop_up = 5;
int stop_down = 4;
int motor_up = 2;
int motor_down = 3;
int up = 6;
int down = 7;
// This function determines the current status of the roof
enum {OPEN, CLOSED, OPENING, CLOSING, UNKNOWN};
byte get_roof_status()
{
if (digitalRead(stop_down) == LOW) return CLOSED;
else if (digitalRead(stop_up) == LOW) return OPEN; // Fixed typo
else if (digitalRead(motor_up) == HIGH) return OPENING;
else if (digitalRead(motor_down) == HIGH) return CLOSING;
return UNKNOWN;
}
// Helper functions
void open_roof() {
digitalWrite(motor_up, LOW);
digitalWrite(motor_down, HIGH);
// Loop until roof is open
while (OPEN != get_roof_status()) /* NULL */;
stop();
}
void close_roof() {
digitalWrite(motor_down, LOW);
digitalWrite(motor_up, HIGH);
// Loop until roof is closed
while (CLOSED != get_roof_status()) /* NULL */;
stop();
}
void stop() {
digitalWrite(motor_down, LOW);
digitalWrite(motor_up, LOW);
}
void setup() {
// Set the pinmodes
pinMode(motor_up, OUTPUT);
pinMode(motor_down, OUTPUT);
pinMode(stop_up, INPUT_PULLUP);
pinMode(stop_down, INPUT_PULLUP);
pinMode(up, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
digitalWrite(motor_up, LOW);
digitalWrite(motor_down, LOW);
}
void loop() {
// This function is much simpler now
if (digitalRead(up) == LOW) {
close_roof();
}
else if (digitalRead(down) == LOW){
open_roof();
}
}