Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. /*
  2. BH1750
  3. Connection:
  4.  VCC-5v
  5.  GND-GND
  6.  SCL-SCL(analog pin 5)
  7.  SDA-SDA(analog pin 4)
  8.  ADD-NC or GND
  9.  
  10. */
  11.  
  12. #include <Wire.h>
  13. #include <BH1750.h>
  14.  
  15. // LUX
  16. BH1750 lightMeter;
  17. uint16_t lastlux;
  18.  
  19. // L298N
  20. const int enableCoopDoorMotorB = 6;          // enable motor b - pin 6
  21. const int directionCloseCoopDoorMotorB = 7;  // direction close motor b - pin 7
  22. const int directionOpenCoopDoorMotorB = 8;  // direction open motor b - pin 8
  23.  
  24. // Switches and contacts
  25. const int UpSwitchPin = 2;
  26. const int DownSwitchPin = 3;
  27. const int TopContactPin = 3;
  28. const int BottomContactPin = 4;
  29. bool manual = 0;
  30. bool error = 0;
  31. int MAX_RUN_TIME = 45;
  32.  
  33.  
  34. void setup() {
  35.   Serial.begin(9600);
  36.   pinMode(13, OUTPUT);
  37.   lightMeter.begin();
  38.   Serial.println("Running...");
  39.  
  40.   // coop door motor
  41.   pinMode (enableCoopDoorMotorB, OUTPUT);           // enable motor pin = output
  42.   pinMode (directionCloseCoopDoorMotorB, OUTPUT);   // motor close direction pin = output
  43.   pinMode (directionOpenCoopDoorMotorB, OUTPUT);    // motor open direction pin = output
  44.  
  45.   // Manual control switches
  46.   // Up switch
  47.   pinMode(UpSwitchPin, INPUT);
  48.   digitalWrite(UpSwitchPin, HIGH);
  49.   // Down switch
  50.   pinMode(DownSwitchPin, INPUT);
  51.   digitalWrite(DownSwitchPin, HIGH);
  52.   // Contacts
  53.   // Top contact
  54.   pinMode(TopContactPin, INPUT);
  55.   digitalWrite(TopContactPin, HIGH);
  56.   // Bottom contact
  57.   pinMode(BottomContactPin, INPUT);
  58.   digitalWrite(BottomContactPin, HIGH);
  59. }
  60.  
  61.  
  62. void loop() {
  63.   // LUX
  64.   uint16_t lux = lightMeter.readLightLevel();// Get Lux value
  65.   //if (lux != lastlux) {
  66.   Serial.println(lux);
  67.   //  lastlux = lux;
  68.   //}
  69.  
  70.   // DOOR OPEN OR CLOSE
  71.   if (!error && lux <= 5 && !manual && (digitalRead(BottomContactPin) == 1 )) {
  72.     Serial.println(lux);
  73.     Serial.print("Status:");
  74.     Serial.println("Donker");
  75.     closeCoopDoorMotorB();
  76.   }
  77.   if (!error && lux > 5 && !manual && (digitalRead(TopContactPin) == 1) ) {
  78.     Serial.println(lux);
  79.     Serial.print("Status:");
  80.     Serial.println("Licht");
  81.     openCoopDoorMotorB();
  82.   }
  83.   if (error) {
  84.     Serial.println("ERROR");
  85.   }
  86.  
  87. }
  88.  
  89. // close the coop door motor (motor dir close = clockwise)
  90. void closeCoopDoorMotorB() {
  91.   int RUN_TIME = 0;
  92.   digitalWrite (directionCloseCoopDoorMotorB, HIGH);     // turn on motor close direction
  93.   digitalWrite (directionOpenCoopDoorMotorB, LOW);       // turn off motor open direction
  94.   analogWrite (enableCoopDoorMotorB, 255);               // enable motor, full speed
  95.   while (!error && digitalRead(BottomContactPin) == 1) {
  96.     RUN_TIME++;
  97.     //Serial.println(RUN_TIME);
  98.     delay(1000);
  99.     if (RUN_TIME > MAX_RUN_TIME) {
  100.       stopCoopDoorMotorB();
  101.       error = 1;
  102.       Serial.println("Max runtime exceeded!");
  103.     }
  104.  
  105.   }
  106.   if (digitalRead(BottomContactPin) == 0 ) {                         // if bottom reed switch circuit is closed
  107.     stopCoopDoorMotorB();
  108.     Serial.println("Coop Door Closed");
  109.   }
  110. }
  111.  
  112. // open the coop door (motor dir open = counter-clockwise)
  113. void openCoopDoorMotorB() {
  114.   int RUN_TIME = 0;
  115.   digitalWrite(directionCloseCoopDoorMotorB, LOW);       // turn off motor close direction
  116.   digitalWrite(directionOpenCoopDoorMotorB, HIGH);       // turn on motor open direction
  117.   analogWrite(enableCoopDoorMotorB, 255);                // enable motor, full speed
  118.   while (!error && digitalRead(TopContactPin) == 1  ) {
  119.     RUN_TIME++;
  120.     delay(1000);
  121.     //Serial.println(RUN_TIME);
  122.     if (RUN_TIME > MAX_RUN_TIME) {
  123.       stopCoopDoorMotorB();
  124.       error = 1;
  125.       Serial.println("Max runtime exceeded!");
  126.     }
  127.   }
  128.   if (digitalRead(TopContactPin) == 0) {                         // if bottom reed switch circuit is closed
  129.     stopCoopDoorMotorB();
  130.     Serial.println("Coop Door Opened");
  131.   }
  132. }
  133. //  stop the coop door motor
  134. void stopCoopDoorMotorB() {
  135.   digitalWrite (directionCloseCoopDoorMotorB, LOW);      // turn off motor close direction
  136.   digitalWrite (directionOpenCoopDoorMotorB, LOW);       // turn on motor open direction
  137.   analogWrite (enableCoopDoorMotorB, 0);                 // enable motor, 0 speed
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement