Guest User

Coop Door - Digispark

a guest
Jun 30th, 2014
1,762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. // Global Vars
  2. int UP = 1;
  3. int DOWN = 0;
  4. int STOP = 2;
  5. int UNKNOWN = 2;
  6.  
  7. int LightLevel = 0;
  8. int DoorState = 2;  // assume neither open nor closed
  9.  
  10. unsigned long motorstarttime;
  11.  
  12. // Pin Assignments
  13. int MotorPinA = 0;
  14. int LEDPin = 1;
  15. int MotorPinB = 2;
  16. int TopSwPin = 3;  // has internal 1.5k pull-up
  17. int BottomSwPin = 4;
  18. int PhotoRPin = 0;  // is actually pin 5 (analog fuckery)
  19.  
  20. void setup(){
  21.  
  22.  
  23. //  pinMode(PhotoRPin, INPUT); // dont do this -- i dont' know why
  24.   pinMode(MotorPinA, OUTPUT);
  25.   pinMode(MotorPinB, OUTPUT);
  26.   pinMode(LEDPin, OUTPUT);
  27.   pinMode(TopSwPin, INPUT);
  28.   pinMode(BottomSwPin, INPUT);
  29.  
  30.   digitalWrite(MotorPinA, HIGH); // motor triggers on LOW
  31.   digitalWrite(MotorPinB, HIGH); // motor triggers on LOW
  32.   digitalWrite(LEDPin, LOW);
  33.  
  34.   DoorState = GetDoorState();
  35.   if (DoorState == UNKNOWN) {
  36.       OpenDoor();
  37.   }
  38.  
  39. }
  40.  
  41. void loop() {
  42.  
  43.   // Get current light level
  44.   LightLevel = analogRead(PhotoRPin)/4;  // low is dark, high is light
  45.    
  46.   if (LightLevel < 20) { // It's dark
  47.       CloseDoor();
  48.   }
  49.   else if (LightLevel > 55) { // It's light
  50.       OpenDoor();
  51.   }
  52.  
  53.   delay(2000);
  54.  
  55. }
  56.  
  57. void OpenDoor() {
  58.   motorstarttime = millis();
  59.   DoorState = GetDoorState();
  60.   while (DoorState != UP) {
  61.     MotorControl(UP);
  62.     DoorState = GetDoorState();
  63.     if (millis() - motorstarttime > 15000) { break; } // Stop running the motor if 15 seconds has passed
  64.   }
  65.   MotorControl(STOP);
  66. }
  67.  
  68. void CloseDoor() {
  69.   motorstarttime = millis();
  70.   DoorState = GetDoorState();
  71.   while (DoorState != DOWN) {
  72.     MotorControl(DOWN);
  73.     DoorState = GetDoorState();
  74.     if (millis() - motorstarttime > 15000) { break; } // Stop running the motor if 15 seconds has passed
  75.   }
  76.   delay(50);
  77.   MotorControl(STOP);
  78. }
  79.  
  80.  
  81. void MotorControl(int Direction) {
  82.  
  83.   if (Direction == UP) {
  84.     digitalWrite(MotorPinA, LOW);
  85.     digitalWrite(MotorPinB, HIGH);
  86.   } else if (Direction == DOWN) {
  87.     digitalWrite(MotorPinA, HIGH);
  88.     digitalWrite(MotorPinB, LOW);
  89.   } else if (Direction == STOP) {
  90.     digitalWrite(MotorPinA, HIGH);
  91.     digitalWrite(MotorPinB, HIGH);
  92.   }  
  93. }
  94.  
  95. int GetDoorState() {
  96.  
  97.   // Read switch states
  98.   int topSwStatus = digitalRead(TopSwPin);
  99.   int bottomSwStatus = digitalRead(BottomSwPin);
  100.   int returnValue = UNKNOWN;
  101.  
  102.   // check to see if the door is between states
  103.   if ((topSwStatus == 1) and (bottomSwStatus == 0)) { returnValue = UNKNOWN; }
  104.   // check to see if the door is open
  105.   if (topSwStatus == 0) { returnValue = UP; }
  106.   // check to see if the door is closed
  107.   if (bottomSwStatus == 1) { returnValue = DOWN; }
  108.  
  109.   return(returnValue);
  110. }
  111.  
  112. void BlinkLED(int times) {
  113.   for (int i=1; i <= times; i++) {
  114.       digitalWrite(LEDPin, HIGH);
  115.       delay(500);
  116.       digitalWrite(LEDPin, LOW);
  117.       delay(500);
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment