Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- BH1750
- Connection:
- VCC-5v
- GND-GND
- SCL-SCL(analog pin 5)
- SDA-SDA(analog pin 4)
- ADD-NC or GND
- */
- #include <Wire.h>
- #include <BH1750.h>
- // LUX
- BH1750 lightMeter;
- uint16_t lastlux;
- // L298N
- const int enableCoopDoorMotorB = 6; // enable motor b - pin 6
- const int directionCloseCoopDoorMotorB = 7; // direction close motor b - pin 7
- const int directionOpenCoopDoorMotorB = 8; // direction open motor b - pin 8
- // Switches and contacts
- const int UpSwitchPin = 2;
- const int DownSwitchPin = 3;
- const int TopContactPin = 3;
- const int BottomContactPin = 4;
- bool manual = 0;
- bool error = 0;
- int MAX_RUN_TIME = 45;
- void setup() {
- Serial.begin(9600);
- pinMode(13, OUTPUT);
- lightMeter.begin();
- Serial.println("Running...");
- // coop door motor
- pinMode (enableCoopDoorMotorB, OUTPUT); // enable motor pin = output
- pinMode (directionCloseCoopDoorMotorB, OUTPUT); // motor close direction pin = output
- pinMode (directionOpenCoopDoorMotorB, OUTPUT); // motor open direction pin = output
- // Manual control switches
- // Up switch
- pinMode(UpSwitchPin, INPUT);
- digitalWrite(UpSwitchPin, HIGH);
- // Down switch
- pinMode(DownSwitchPin, INPUT);
- digitalWrite(DownSwitchPin, HIGH);
- // Contacts
- // Top contact
- pinMode(TopContactPin, INPUT);
- digitalWrite(TopContactPin, HIGH);
- // Bottom contact
- pinMode(BottomContactPin, INPUT);
- digitalWrite(BottomContactPin, HIGH);
- }
- void loop() {
- // LUX
- uint16_t lux = lightMeter.readLightLevel();// Get Lux value
- //if (lux != lastlux) {
- Serial.println(lux);
- // lastlux = lux;
- //}
- // DOOR OPEN OR CLOSE
- if (!error && lux <= 5 && !manual && (digitalRead(BottomContactPin) == 1 )) {
- Serial.println(lux);
- Serial.print("Status:");
- Serial.println("Donker");
- closeCoopDoorMotorB();
- }
- if (!error && lux > 5 && !manual && (digitalRead(TopContactPin) == 1) ) {
- Serial.println(lux);
- Serial.print("Status:");
- Serial.println("Licht");
- openCoopDoorMotorB();
- }
- if (error) {
- Serial.println("ERROR");
- }
- }
- // close the coop door motor (motor dir close = clockwise)
- void closeCoopDoorMotorB() {
- int RUN_TIME = 0;
- digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
- digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
- analogWrite (enableCoopDoorMotorB, 255); // enable motor, full speed
- while (!error && digitalRead(BottomContactPin) == 1) {
- RUN_TIME++;
- //Serial.println(RUN_TIME);
- delay(1000);
- if (RUN_TIME > MAX_RUN_TIME) {
- stopCoopDoorMotorB();
- error = 1;
- Serial.println("Max runtime exceeded!");
- }
- }
- if (digitalRead(BottomContactPin) == 0 ) { // if bottom reed switch circuit is closed
- stopCoopDoorMotorB();
- Serial.println("Coop Door Closed");
- }
- }
- // open the coop door (motor dir open = counter-clockwise)
- void openCoopDoorMotorB() {
- int RUN_TIME = 0;
- digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
- digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
- analogWrite(enableCoopDoorMotorB, 255); // enable motor, full speed
- while (!error && digitalRead(TopContactPin) == 1 ) {
- RUN_TIME++;
- delay(1000);
- //Serial.println(RUN_TIME);
- if (RUN_TIME > MAX_RUN_TIME) {
- stopCoopDoorMotorB();
- error = 1;
- Serial.println("Max runtime exceeded!");
- }
- }
- if (digitalRead(TopContactPin) == 0) { // if bottom reed switch circuit is closed
- stopCoopDoorMotorB();
- Serial.println("Coop Door Opened");
- }
- }
- // stop the coop door motor
- void stopCoopDoorMotorB() {
- digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
- digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn on motor open direction
- analogWrite (enableCoopDoorMotorB, 0); // enable motor, 0 speed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement