Writing code to operate henhouse door - not getting through to L298N motor board

Dear People,
I’ve been trying my darnedest to figure out the code to operate our henhouse door. First, let me admit that I am over 60, and not technically inclined. I am pretty slow on the uptake but have made a sincere effort to learn how to write code and wire the circuits, but there are still things that I don’t understand. Initially, it seemed like what I wanted to do was fairly simple: open the henhouse when it’s light out; and close the door when it’s dark. And I also wanted to add a couple of indicator lights to let me know the status of the henhouse door.

I’ve spent months fabricating the door, motor housing/reel, installing sensors/switches, and putting it all together in the henhouse. Then, I spent a lot (a whole lot) of time reading tutorials and trying to use existing code that other individuals posted on the internet to operate henhouse doors. I am very grateful to those individuals, but I have not been able to get their code to work for me. After weeks of struggling to try to get that code to work, I feel I have learned almost enough to write my own, less complicated code.

I have reached a point where my code almost works, but something is still not right. The lights flash, as the sketch indicates for stuck-door condition, but I don’t seem to be able to get the arduino to check the reed switches or do anything in response to activating the switches. The indicator lights flash constantly, but are supposed to respond to the reed switches. Also, and most importantly, I can’t get the motor to run in any direction.

I suspect the motor problem could be due to a burned out component (looking at you, L298N board), but it might also be the circuit wiring, or a problem with my code. I’ve purchased several arduinos, and several L298N motor controller boards—one of which yielded a neat little column of smoke on an early attempt.

I’m currently using a Sunfounder Mega module, and am on my second L298N, but I have backup Arduino Uno boards and at least one extra motor board that I haven’t compromised. I’m trying to get this system to operate a 24v DC motor, as this was the motor that one of the people whose code I was working with used (I have learned to remove the voltage limiting jumper on the L298N).

The code compiles and uploads, and I can read the photocell on the serial monitor, but other than flashing the lights, nothing else seems to respond when I activate the switches or block the photo sensor.

One thing I have not learned yet is how to draw a circuit diagram (mea culpa). I followed a fritzing diagram posted by an individual whose henhouse door does appear to work. I currently have the reed switches wired to the positive rail on the breadboard and they are connected to the arduino pins on the other end (this may be one of the problems, but I’m following someone else’s wiring diagram). I know the circuit wiring depends on the code, and I’ll try to get a proper wiring diagram together in the near future. In the meantime I was hoping that someone on the forum would be patient enough to send me some advice, and perhaps look at my code to see if there are any obvious problems on that end.
My apologies for this long post.
Thanks,
runciblefish

//Tom's attempt at code to operate Chicken Coop Door and Lights

//Call Out Pins

int photoCellPin = A0;        //read photocell
int photoCellLightVal = 0;    // initial state for photoCellPin Value
int enA_Pin = 7;              // enable motor to pull door up
int doorMotorDownPin = 8;     // door down motor direction
int doorMotorUpPin = 9;       // door up motor direction
int bottomReedSwitchPin = 26; // switch to identify when to stop lowering door
int topReedSwitchPin = 27;    // switch to identify when to stop raising door
int doorOpenLedPin = 40;      // red LED to indicate when top reed switch is active, door is at top
int doorClosedLedPin = 41;    // green LED to indicate when bottom reed switch is active, door is at bottom

void morningDoorUp () {                   // function to read light value (daytime) and raise door to release the hens
  digitalRead (photoCellLightVal);        // check photocell reading
  if (photoCellLightVal >= 150) {         // if light is bright, open the door 
    analogWrite (enA_Pin, 255);           // send power to L298N motor board
    digitalWrite (doorMotorUpPin, HIGH);  // power to motor in up direction
    digitalWrite(doorMotorDownPin, LOW);  // no power to down direction
  }
  {
    digitalRead (topReedSwitchPin);           // check to see if door is open
    if (topReedSwitchPin == HIGH) {           // if upper door reed switch is HIGH, door is open 
      digitalWrite (doorMotorUpPin, LOW);     // stop motor up direction when upper door reed switch is engaged
      digitalWrite (doorMotorDownPin, LOW);   // keep motor down direction in off position
      analogWrite (enA_Pin, 0);               // tell motor board to stop doing anything
    }
  }
}

void eveningDoorDown () {                     // function to read light value (evening) and lower door to keep hens safe  
  digitalRead (photoCellLightVal);            // check photocell reading
  if (photoCellLightVal <= 100) {             // if light is dim, close the door
    analogWrite (enA_Pin, 255);               // send power to L298N motor board
    digitalWrite (doorMotorUpPin, LOW);       // leave power off for motor up direction 
    digitalWrite(doorMotorDownPin, HIGH);     // power on for motor down direction
  }
  {
    digitalRead (bottomReedSwitchPin);        // check to see if door is closed
    if (bottomReedSwitchPin == HIGH) {        // if lower door reed switch is HIGH, door is closed
      digitalWrite (doorMotorUpPin, LOW);     // keep motor up direction in off position
      digitalWrite (doorMotorDownPin, LOW);   // stop motor down direction when lower door reed switch is engaged
      analogWrite (enA_Pin, 0);               // tell motor board to stop doing anything
    }

  }
}
void doorStuckFlashLeds () {

  if ((topReedSwitchPin != HIGH) && (bottomReedSwitchPin !=HIGH)){  // check to see if door is between upper and lower reed switches
    digitalWrite (doorOpenLedPin, HIGH);                            // if neither door switch is activated, flash the red LED on
    delay (500);                                                    // set blink rate for flashing red LED
    digitalWrite (doorOpenLedPin, LOW);}                            // turn off red LED after pause
    {
    digitalWrite (doorClosedLedPin, HIGH);                          // flash the green LED
        delay (500);                                                // set blink rate for flashing green LED
    digitalWrite (doorClosedLedPin, LOW);}                          // turn off green LED after pause
 }


void setup() {
  // put your setup code here, to run once:

  pinMode (photoCellPin, INPUT);            // set photocell pin as input
  pinMode (enA_Pin, OUTPUT);                // set L298N activation pin as output
  pinMode (doorMotorDownPin, OUTPUT);       // set motor down direction pin as output
  pinMode (doorMotorUpPin, OUTPUT);         // set motor up direction pin as output
  pinMode (bottomReedSwitchPin, INPUT);     // set door closed reed switch pin as input
  pinMode (topReedSwitchPin, INPUT);        // set door open reed switch pin as input
  pinMode (doorOpenLedPin, OUTPUT);         // set door open (red) LED pin as output
  pinMode (doorClosedLedPin, OUTPUT);       // set door closed (green) LED pin as output

  Serial.begin (9600);                      // activate serial print monitor
}

void loop() {
  // put your main code here, to run repeatedly:

  photoCellLightVal = analogRead (photoCellPin); // start by checking photocell for light level
  Serial.println (photoCellLightVal);            // print photocell light value reading 
  delay (200);                                   // delay a moment so I can read the scrolling numbers

  morningDoorUp ();                              // run the morning (door up) routine              

  eveningDoorDown ();                            // run the evening (door down) routine

  doorStuckFlashLeds ();                         // run the stuck-door routine
}

The best approach is to make sure each part of the project works to your complete satisfaction before putting everything together.

That is true in spades if you are having problems with the motor and mechanism. The ancient L298 driver is for toys and probable totally inadequate for the door motor.

Determine the stall current for the motor (from the motor product page, the motor data sheet, or divide the motor supply voltage by the motor winding resistance) and buy an appropriate motor driver.

Otherwise, please review the "How to use this forum" post for more hints on getting the help you need. A clear hand drawn circuit diagram is required.

That's pretty much the response I expected. All input is appreciated. Thanks, for taking the time to reply.

60, Wish I was that young again, full of p*ss and vinegar and all.

Being older, I would use a relay.

2017-08-19_19-16-01.jpg