Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 96 lines (78 sloc) 1.477 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
int FWD = 4;
int REV = 5;
int LIGHT = 0;

int CLOSE_SWITCH = 8;
int OPEN_SWITCH = 9;

void setup() {
  pinMode(FWD, OUTPUT);
  pinMode(REV, OUTPUT);
  pinMode(CLOSE_SWITCH, INPUT);
  pinMode(OPEN_SWITCH, INPUT);
  Serial.begin(9600);
  stopMotor();
  openDoor();
}

//void loop() {
  //openDoor();
  //delay(1000);
//}



void loop() {
  static int daylight=true;
  if (daylight) {
    daylight = checkDaylight(250);
    if (!daylight) {
      closeDoor();
    }
  } else {
    daylight = checkDaylight(750);
    if (daylight) {
      openDoor();
    }
  }
  delay(1000);
}

int checkOpen() {
  return digitalRead(OPEN_SWITCH);
}

int checkClosed(){
  return digitalRead(CLOSE_SWITCH);
}

int checkDaylight(int threshold) {
  int light = analogRead(LIGHT);
  if (light>threshold) {
    return true;
  } else {
    return false;
  }
}

void stopMotor() {
  Serial.println("stopMotor");
  digitalWrite(FWD,LOW);
  digitalWrite(REV,LOW);
}

void closeDoor() {
  Serial.println("closeDoor");
  int doorClosed = checkClosed();

  if (!doorClosed) motorForward();
  
  while(!doorClosed) {
    doorClosed = checkClosed();
  }
  stopMotor();
}

void openDoor() {
  int doorOpen = checkOpen();
  Serial.println("openDoor");
  
  if (!doorOpen) motorReverse();
  
  while(!doorOpen) {
    doorOpen = checkOpen();
  }
  stopMotor();
}

void motorReverse() {
  digitalWrite(FWD,LOW);
  digitalWrite(REV,HIGH);
}

void motorForward() {
  digitalWrite(FWD,HIGH);
  digitalWrite(REV,LOW);
}
Something went wrong with that request. Please try again.