Automatic door opener problem

Hi! This is my first Arduino project, so sorry if this is posted in the wrong thread/place. I've built a automatic door opener for my chickens using an arduino controlling a DC motor, 2 reed switches and a light sensor. It opens at sunrise and closes at sundown perfectly for 2-4 days, and then the trouble starts. Either i find the door not closed after dark, or it never opens during the morning/day. If i pull the power to the arduino and restart it, it will then work fine for a few days, before it malfunctions again. I will paste the code i've used. It has been a modified version of another project i found on Youtube. I have read somewhere that using "delay" is generally not advised, and I thought maybe that could be the problem, but I would appreciate any input.

Some more info : The BH1750 is the light sensor im using. Im using a TB6612FNG to control the motor.

// Newest
#include <Arduino.h>
#include <hp_BH1750.h>
hp_BH1750 BH1750;

int BUTTON_PIN1 = 7;
int BUTTON_PIN2 = 8;

int buttonState1 = 0;
int buttonState2 = 0;

//
int pwm = 12;
int in1 = 11;
int in2 = 10;
int MotorSpeed1 = 255;

//

void setup() {
  // put your setup code here, to run once:
  bool avail = BH1750.begin(BH1750_TO_GROUND);

  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);

  pinMode(pwm, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  Serial.begin(9600);

  buttonState1 = digitalRead(BUTTON_PIN1);
  buttonState2 = digitalRead(BUTTON_PIN2);

  if (buttonState2 != HIGH) {
    // this function will run the motor down as long as switch 1 has not been triggered
    while (buttonState2 != HIGH) {
      // turn on motor and set speed to 255
      analogWrite(pwm, 255);
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      //read the state of the switch again to make sure that it has not been triggered
      buttonState1 = digitalRead(BUTTON_PIN1);
      buttonState2 = digitalRead(BUTTON_PIN2);
    }
    // once buttonState2 has been triggered turn off the motor
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {

  buttonState1 = digitalRead(BUTTON_PIN1);
  buttonState2 = digitalRead(BUTTON_PIN2);

  BH1750.start();
  float lux = BH1750.getLux();

  if (buttonState2 = HIGH && lux < 25)

  {
    delay(90000);
    motordown();
  }

  else if (buttonState1 = HIGH && lux > 190) {
    delay(90000);
    motorup();
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

void motordown() {

  BH1750.start();
  float lux = BH1750.getLux();
  //Read the state of switch 1 (the Bottom one) and place it in buttonState1
  buttonState1 = digitalRead(BUTTON_PIN1);
  //read the state of switch 2 (the top one) and place it in buttonState2
  buttonState2 = digitalRead(BUTTON_PIN2);

  //If switchState2 is high and the light is dark then continue
  if (buttonState2 = HIGH && lux < 25)
    //wait 2 seconds
    delay(3000);
  //read the state of switch 1 (the bottom one) and place it in buttonState1
  buttonState1 = digitalRead(BUTTON_PIN1);
  //read the state of switch 2 (the top one) and place it in buttonState2
  buttonState2 = digitalRead(BUTTON_PIN2);

  while (buttonState1 != HIGH) {
    // turn on motor and set speed to 255
    analogWrite(pwm, 255);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    //read the state of switch 2 (the top one) and place it in buttonState2
    buttonState1 = digitalRead(BUTTON_PIN1);
    //read the state of switch 2 (the top one) and place it in buttonState2
    buttonState2 = digitalRead(BUTTON_PIN2);
  }


  // now turn off motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

//////////////////////////////////////////////////////////////////////////////////////////////////


void motorup() {

  BH1750.start();
  float lux = BH1750.getLux();
  //read the state of switch 1 (the bottom one) and place it in buttonState1
  buttonState1 = digitalRead(BUTTON_PIN1);
  //read the state of switch 2 (the top one) and place it in buttonState2
  buttonState2 = digitalRead(BUTTON_PIN2);

  //if switchState1 is high and the light is bright then continue
  if (buttonState1 = HIGH && lux > 190) {

    //read the state of switch 1 (the bottom one) and place it in buttonState1
    buttonState1 = digitalRead(BUTTON_PIN1);
    //read the state of switch 2 (the top one) and place it in buttonState2
    buttonState2 = digitalRead(BUTTON_PIN2);
    //delay 2 seconds
    delay(3000);
    //while buttonState2 is not high turn on the motor up
    while (buttonState2 != HIGH) {
      // this function will run the motor as long as switch 2 has not been triggered
      // turn on motor and set speed to 255
      analogWrite(pwm, 255);
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      //read the state of switch 1 (the bottom one) and place it in buttonState1
      buttonState1 = digitalRead(BUTTON_PIN1);
      //read the state of switch 2 (the top one) and place it in buttonState2
      buttonState2 = digitalRead(BUTTON_PIN2);
    }
    // now turn off motor
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }
}

I'm very confused by your sketch. You are restarting the BH1750 every time you want to take a measurement? That isn't necessary. If you want the BH1750 to only take a measurement when you want it to, you can put it into one-shot mode. I would look at this library as I found it easier to use with my BH1750 project. You can start the sensor once, put it into one-time mode and then trigger a reading when you want. I suspect you are running out of memory by starting the sensor every time you take a reading.

  1. use millis() to measure time passage
  2. does the board have wifi? if so, then you could log information about the state to some kind of web service or local web server –> for example unexpected restarts

Even using millis() would not work for too long if using unsinged long, which is able to hold bigger numbers compared to, for example, int.


One solution is to have the Arduino software-reset itself every day or two.

@ryancasler has the theory I'd test first.

But if that doesn't pan, there is nothing wrong in your sketch that using millis() would fix, and both delay() and millis() can handle time forever.

Here's where the more info we'd like may come in handy.

A schematic, showing all the parts and how they are wired up with attention to include how they are wired for power, and where that power comes from.

A picture of your physical device, so we can see the wires and how tangled up or not they are, whether they are the right colour and so forth.

But first the restarting thing.

Most times, Arduinos can run forever. I can move my eyes just a bit and see three devices that have been in service here in the lab for years, only discontinuously so on account of power failures.

a7

Hi,
I presume your buttons are the limit switches, upper and lower, it might be worth calling them that to make you code easier to read.
upperLimitPin and lowerLimitPin.
Call pwm, motorSpeedPin.
Call in1 and in2, in1Pin and in2Pin, etc to identify them as fixed variables and pins.

There is some other code tidying you can do, but lets get your code working first.

Tom... :smiley: :+1: :coffee: :australia:

So I've changed to the library you linked to now. I just used its default settings (continuous mode). So far it works fine, so I guess I'll just have to wait a few days to see if the problem occurs again. If it does I'll give the "one-time mode" a try. I didnt completely understand how to use the one-time mode, so thats why I try the continuous mode first. I have also cleaned up the code a bit and I will include it below. Thanks for all the suggestions so far! Really appreciate it!

#include <BH1750.h>
#include <Wire.h>
#include <Arduino.h>

BH1750 lightMeter;

int lowerLimitPin = 7;
int upperLimitPin = 8;

int lowerLimitState = 0;
int upperLimitState = 0;

//
int motorSpeedPin = 12;
int in1 = 11;
int in2 = 10;
int MotorSpeed1 = 255;

//

void setup() {

  Serial.begin(9600);
  Wire.begin();

  lightMeter.begin();

  pinMode(lowerLimitPin, INPUT_PULLUP);
  pinMode(upperLimitPin, INPUT_PULLUP);

  pinMode(motorSpeedPin, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);


  lowerLimitState = digitalRead(lowerLimitPin);
  upperLimitState = digitalRead(upperLimitPin);

  if (upperLimitState != HIGH) {
    // this function will run the motor down as long as upperLimitState has not been triggered
    while (upperLimitState != HIGH) {
      // turn on motor and set speed to 255
      analogWrite(motorSpeedPin, 255);
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      //read the state of the switch again to make sure that it has not been triggered
      lowerLimitState = digitalRead(lowerLimitPin);
      upperLimitState = digitalRead(upperLimitPin);
    }
    // once upperLimitState has been triggered turn off the motor
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {

  lowerLimitState = digitalRead(lowerLimitPin);
  upperLimitState = digitalRead(upperLimitPin);

  float lux = lightMeter.readLightLevel();

  if (upperLimitState == HIGH && lux < 25)

  {
    delay(90000);
    motordown();
  }

  else if (lowerLimitState == HIGH && lux > 190) {
    delay(90000);
    motorup();
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

void motordown() {

  float lux = lightMeter.readLightLevel();
  //Read the state of switch 1 (the Bottom one) and place it in lowerLimitState
  lowerLimitState = digitalRead(lowerLimitPin);
  //read the state of switch 2 (the top one) and place it in upperLimitState
  upperLimitState = digitalRead(upperLimitPin);

  //If switchState2 is high and the light is dark then continue
  if (upperLimitState == HIGH && lux < 25)
    //wait 2 seconds
    delay(3000);
  //read the state of switch 1 (the bottom one) and place it in lowerLimitState
  lowerLimitState = digitalRead(lowerLimitPin);
  //read the state of switch 2 (the top one) and place it in upperLimitState
  upperLimitState = digitalRead(upperLimitPin);

  while (lowerLimitState != HIGH) {
    // turn on motor and set speed to 255
    analogWrite(motorSpeedPin, 255);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    //read the state of switch 2 (the top one) and place it in upperLimitState
    lowerLimitState = digitalRead(lowerLimitPin);
    //read the state of switch 2 (the top one) and place it in upperLimitState
    upperLimitState = digitalRead(upperLimitPin);
  }


  // now turn off motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

//////////////////////////////////////////////////////////////////////////////////////////////////


void motorup() {

  float lux = lightMeter.readLightLevel();
  //read the state of switch 1 (the bottom one) and place it in lowerLimitState
  lowerLimitState = digitalRead(lowerLimitPin);
  //read the state of switch 2 (the top one) and place it in upperLimitState
  upperLimitState = digitalRead(upperLimitPin);

  //if lowerLimitState is high and the light is bright then continue
  if (lowerLimitState == HIGH && lux > 190) {

    //read the state of switch 1 (the bottom one) and place it in lowerLimitState
    lowerLimitState = digitalRead(lowerLimitPin);
    //read the state of switch 2 (the top one) and place it in upperLimitState
    upperLimitState = digitalRead(upperLimitPin);
    //delay 2 seconds
    delay(3000);
    //while upperLimitState is not high turn on the motor up
    while (upperLimitState != HIGH) {
      // this function will run the motor as long as switch 2 has not been triggered
      // turn on motor and set speed to 255
      analogWrite(motorSpeedPin, 255);
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      //read the state of switch 1 (the bottom one) and place it in lowerLimitState
      lowerLimitState = digitalRead(lowerLimitPin);
      //read the state of switch 2 (the top one) and place it in upperLimitState
      upperLimitState = digitalRead(upperLimitPin);
    }
    // now turn off motor
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }
}

I'll keep my fingers crossed for you! Let us know if it works past the 3 day mark.

Nicely Edited... :+1: :+1: :+1: :+1: :+1:

Tom.. :smiley: :+1: :coffee: :australia:

So the problem still persists unfortunately. I guess I’ll have to try the one-shot mode then. And maybe find a way to soft reset it once a day.

I could certainly try to take some pictures and draw up a wire diagram. I just don’t understand how (if I hade wired it wrong) it would work for a couple of days after turning the power off and on again. I would have thought if there was something wrong with my wiring the door would not open/close at all

Without knowing what error is occurring you can't say for sure that it's not a mechanical problem. But I agree, it feels like a software problem. I have not used that BH1750 library. I use the Adafruit one. So I can't speak to whether that might be part of your problem. If you do decide to try the one-shot method, their library has an example of that included so it should be easy to switch.

You could also just write into your program to reset the board every day say, an hour after the door closes, therefore resetting the memory leak if there is one. Just add:
void(* resetFunc) (void) = 0;
before your setup function. Then when you want to reset the board, just call:
resetFunc();
But I would only use that as a last resort. I think there is still value in trying to nail down what the problem is and trying to fix it.