please help wit coop door

Please for the love of all things holy someone help me with this code. I have been working on this for 2 weeks and have tried using two other people’s code and can get no luck so I tried writing my own with trying to follow their guidelines. I am very very new to this and apparently not so good. I am building an automatic chicken coop door that works off of a photocell. I am using an Arduino Mega 2560 with a L298N driver and a DHT11 temperature sensor because I would like to control a dc fan inside the coop as well.

I am getting the following error:

Arduino: 1.6.10 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

C:\Users\GERAR_~1\AppData\Local\Temp\ccuL4sTS.ltrans0.ltrans.o: In function `main’:

ccuL4sTS.ltrans0.o:(.text.startup+0x5fc): undefined reference to `motordown()’

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Thanks in advance!

#include <DHT.h>
#include <LiquidCrystal.h>


#define DHTPIN 10                     //thermometer on pin 10
#define DHTTYPE DHT11                //DHT type 11


int photocell = A0;                     //photocell pin
int photocellReading;                   //analog reading from photocell
const int topSwitch = 35;               //top reed switch
const int bottomSwitch = 37;            //bottom reed switch
int openDoorLED = 46;                   //red door open LED 
int closedDoorLED = 48;                 //green door closed LED
int enA = 9;                            //door direction
int in1 = 22;                            //door motor up
int in2 = 24;                            //door motor down
int SwitchState1 = 0;
int SwitchState2 = 0;

DHT dht (DHTPIN, DHTTYPE);

void setup() {

Serial.begin(9600);               //start the print screen
dht.begin();                      //start dht for temperature
      
//door switches pin modes
  pinMode(topSwitch, INPUT);            //top reed switch set as input
  digitalWrite(topSwitch, HIGH);        //activate internal pullup resistor
  pinMode(bottomSwitch, INPUT);         //bottom reed swithc set as input
  digitalWrite(bottomSwitch, HIGH);     //activate internal pullup resistor
  pinMode(photocell, INPUT);            //photocell analod input

// set the motor control pins as outputs. This means pins 7,8, 9 are outputs to the l298n motor controller.
  pinMode(enA, OUTPUT);                 //pin 9 from arduino
  pinMode(in1, OUTPUT);                 //pin 8 from arduino
  pinMode(in2, OUTPUT);                 //pin 7 from arduino

//door LED pin modes
  pinMode (openDoorLED, OUTPUT);        //red LED output
  pinMode (closedDoorLED, OUTPUT);      //green LED output

// Switch State decleration
SwitchState1 = digitalRead(topSwitch);
SwitchState2 = digitalRead(bottomSwitch);

     
        }

     


void loop() {
 
  photocellReading = analogRead(photocell);
  float f = dht.readTemperature(true);
  
    Serial.print("Analog Reading = ");
    Serial.println(photocellReading);
    Serial.print("Top Switch =     ");
    Serial.println(digitalRead(topSwitch));     //display top switch current value
    Serial.print("Bottom Swith =     ");
    Serial.println(digitalRead(bottomSwitch));  //display bottom switch current value
    Serial.print("Temperature = ");
    Serial.println(f);
 delay(2000);

//led loop

 void loop();{

    
        if (digitalRead(topSwitch) == LOW){             //if top switch is connected
          digitalWrite(openDoorLED, HIGH);              //turn on red led
          Serial.println("Door Open");                  //print door open
          }
          else{
            digitalWrite(openDoorLED, LOW);}            //but if not turn the red led off

         if (digitalRead(bottomSwitch) == LOW){         //if the bottom switch is connected
          digitalWrite(closedDoorLED, HIGH);            //turn on the green led
          Serial.println("Door Closed");                //print doopr cloosed
          }
          else{
            digitalWrite(closedDoorLED, LOW);}          //but if not turn the green led off

 } 

 void motorup();{
  
    SwitchState1 = digitalRead(bottomSwitch);
    SwitchState2 = digitalRead(topSwitch);

      if (SwitchState1 = HIGH && photocellReading >200){
            SwitchState1 = digitalRead(bottomSwitch);
            SwitchState2 = digitalRead(topSwitch);
              delay (2000);
                while (SwitchState2 !=HIGH){
                  analogWrite(enA, 255);
                  digitalWrite(in1, LOW);
                  digitalWrite(in2, HIGH);
                  SwitchState1 = digitalRead(bottomSwitch);
                  SwitchState2 = digitalRead(topSwitch);
                  }
                      digitalWrite(in1,LOW);
                      digitalWrite(in2,LOW);}
  }
void motordown();{
  
    SwitchState1 = digitalRead(bottomSwitch);
    SwitchState2 = digitalRead(topSwitch);

      if (SwitchState2 = HIGH && photocellReading < 200)
        delay (2000);
          SwitchState1 = digitalRead(bottomSwitch);
          SwitchState2 = digitalRead(topSwitch);
            while (SwitchState1 !=HIGH){
                analogWrite(enA, 255);
                digitalWrite(in1, HIGH);
                digitalWrite(in2, LOW);
                SwitchState1 = digitalRead(bottomSwitch);
                SwitchState2 = digitalRead(topSwitch);
                }
                    digitalWrite(in1, LOW);
                    digitalWrite(in2, LOW);
  }

void loop();{

    photocellReading = analogRead(photocell);
    SwitchState1 = digitalRead(bottomSwitch);
    SwitchState2 = digitalRead(topSwitch);

    if(SwitchState2 = HIGH && photocellReading <200){
      delay(2000);
        motordown();
        }
        else if(SwitchState1 = HIGH && photocellReading >200){
          delay(2000);
        }
}
}

(deleted)

First, you have two loop() functions, which is not good. Get rid of one of them.

Second, all of your functions have extraneous semicolons after the function signature. That is:

  void motordown(); {

should be written as:

  void motordown() {

Note the removal of the semicolon.

okay, I put in the code that "Spycatcher2K" put in and it allowed me to send it to the Arduino. Problem now is that it isn't working properly.

my led lights work properly but not the motor. it seems that the "while" function isn't holding because when the reed switch breaks contact the motor immediately stops instead of running until it hits the opposite one.

SwitchState1 and SwitchState2 are silly names for variables. It makes it difficult to understand what the code is doing. You've given everything else a good name.

I would change these variables to DoorIsFullyOpen and DoorIsFullyClosed. That will help making the program readable. Also, if you're new to C, you won't be used to reading the logic operators. Just remember, every time you see a !, it means "not". So...

while(! DoorIsFullyOpen) {
  //motor drive in the open direction

Now it should be much more obvious which direction the motor is going and which switch it's looking at to decide when to stop.

On top of the good comments above, with recent updates to the IDE,

  pinMode(topSwitch, INPUT);            //top reed switch set as input
  digitalWrite(topSwitch, HIGH);        //activate internal pullup resistor
  pinMode(bottomSwitch, INPUT);         //bottom reed swithc set as input
  digitalWrite(bottomSwitch, HIGH);     //activate internal pullup resistor

is now better written as

  pinMode(topSwitch, INPUT_PULLUP);            //top reed switch set as input with internal pullup resistor
  pinMode(bottomSwitch, INPUT_PULLUP);         //bottom reed swithc set as input with internal pullup resistor

I did as suggested. Now my two led lights stay on continuously and the motor only runs when the bottom switch is engaged and stops when the contact breaks. My serial print works great and I can tell when the switches are making contact and that the light and temperature sensors are working well. I still seem to be having a problem with the sequencing and having the motor work with the photocell and also to stay engaged when the door is traveling.

Again, thanks in advance for any help. I’m at my wits end with this program but NEED to get it up and running.

#include <DHT.h>
#include <LiquidCrystal.h>


#define DHTPIN 10                     //thermometer on pin 10
#define DHTTYPE DHT11                //DHT type 11


int photocell = A0;                     //photocell pin
int photocellReading;                   //analog reading from photocell
const int topSwitch = 35;               //top reed switch
const int bottomSwitch = 37;            //bottom reed switch
const int openDoorLED = 46;                   //red door open LED
const int closedDoorLED = 48;                 //green door closed LED
const int enA = 9;                            //door direction
const int in1 = 22;                            //door motor up
const int in2 = 24;                            //door motor down
int DoorisFullyOpen = 0;
int DoorisFullyClosed = 0;

DHT dht (DHTPIN, DHTTYPE);

void setup() {

  Serial.begin(9600);               //start the print screen
  dht.begin();                      //start dht for temperature

  //door switches pin modes
  pinMode(topSwitch, INPUT_PULLUP);            //top reed switch set as input
 // digitalWrite(topSwitch, HIGH);        //activate internal pullup resistor
  pinMode(bottomSwitch, INPUT_PULLUP);         //bottom reed swithc set as input
 // digitalWrite(bottomSwitch, HIGH);     //activate internal pullup resistor
  pinMode(photocell, INPUT);            //photocell analod input

  // set the motor control pins as outputs. This means pins 7,8, 9 are outputs to the l298n motor controller.
  pinMode(enA, OUTPUT);                 //pin 9 from arduino
  pinMode(in1, OUTPUT);                 //pin 8 from arduino
  pinMode(in2, OUTPUT);                 //pin 7 from arduino

  //door LED pin modes
  pinMode (openDoorLED, OUTPUT);        //red LED output
  pinMode (closedDoorLED, OUTPUT);      //green LED output

  // Switch State decleration
  DoorisFullyOpen = digitalRead(topSwitch);
  DoorisFullyClosed = digitalRead(bottomSwitch);

 if (DoorisFullyClosed !=HIGH){
  while(DoorisFullyClosed !=HIGH)
    analogWrite, (enA, 255);
    digitalWrite, (in1, LOW);
    digitalWrite, (in2, HIGH);
    DoorisFullyOpen=digitalRead(topSwitch);
    DoorisFullyClosed=digitalRead(bottomSwitch);
    }
    digitalWrite(in1,LOW);
    digitalWrite(in2,LOW);
  }







void loop()
{

  photocellReading = analogRead(photocell);
  float f = dht.readTemperature(true);

  Serial.print("Analog Reading = ");
  Serial.println(photocellReading);
  Serial.print("Top Switch =     ");
  Serial.println(digitalRead(topSwitch));     //display top switch current value
  Serial.print("Bottom Swith =     ");
  Serial.println(digitalRead(bottomSwitch));  //display bottom switch current value
  Serial.print("Temperature = ");
  Serial.println(f);
  delay(2000);


  
  photocellReading = analogRead(photocell);
  DoorisFullyOpen = digitalRead(bottomSwitch);
  DoorisFullyClosed = digitalRead(topSwitch);


  if (DoorisFullyClosed = HIGH && photocellReading > 200) {
    DoorisFullyOpen;
    DoorisFullyClosed;
        delay (2000);
    while (DoorisFullyClosed != HIGH) {
      analogWrite(enA, 255);
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      DoorisFullyOpen = digitalRead(bottomSwitch);
      DoorisFullyClosed = digitalRead(topSwitch);
    }
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }


  if (DoorisFullyOpen = HIGH && photocellReading < 200)
    delay (2000);
    while (DoorisFullyOpen != HIGH) {
    analogWrite(enA, 255);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    DoorisFullyOpen = digitalRead(bottomSwitch);
    DoorisFullyClosed = digitalRead(topSwitch);
  }
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  
if (digitalRead(topSwitch), HIGH) {            //if top switch is connected
    digitalWrite(openDoorLED, HIGH);              //turn on red led
    Serial.println("Door Open");                  //print door open
  }
  else {
    digitalWrite(openDoorLED, LOW);
  }            //but if not turn the red led off

if (digitalRead(bottomSwitch), HIGH) {        //if the bottom switch is connected
    digitalWrite(closedDoorLED, HIGH);            //turn on the green led
    Serial.println("Door Closed");                //print doopr cloosed
  }
  else {
    digitalWrite(closedDoorLED, LOW);
  }          //but if not turn the green led off
  
}

You make this type of mistake at several points in the program"

  if (DoorisFullyClosed = HIGH && photocellReading > 200) {

probably should be:

  if (DoorisFullyClosed == HIGH && photocellReading > 200) {

The test for equality uses the “==” operator while assignment is “=”

EDIT:

This seems bogus, too:

if (digitalRead(topSwitch), HIGH) {            //if top switch is connected

I did as suggested and no dice! Should I have the door open and door close commands each in their own loop?

Post your full code, where you are now

your comments are confusing too...

const int enA = 9;   //door direction
const int [color=red]in1 = 22;[/color]  //door motor up
const int [color=orange]in2 = 24[/color];  //door motor down

  // set the motor control pins as outputs. [color=red]This means pins 7,8, 9 are outputs to the l298n motor controller.[/color]
  pinMode(enA, OUTPUT);                 //pin 9 from arduino
  pinMode([color=red]in1[/color], OUTPUT);                 //pin [color=red]8[/color] from arduino
  pinMode([color=orange]in2[/color] , OUTPUT);                //pin [color=orange]7[/color] from arduino

do you get that in the code below

  if (DoorisFullyClosed != HIGH) {
    while (DoorisFullyClosed != HIGH)
      analogWrite, (enA, 255);
    digitalWrite, (in1, LOW);
    digitalWrite, (in2, HIGH);
    DoorisFullyOpen = digitalRead(topSwitch);
    DoorisFullyClosed = digitalRead(bottomSwitch);
  }

you do the test but the loop is only on this part of the code.

while (DoorisFullyClosed != HIGH)  analogWrite, (enA, 255);
    digitalWrite, (in1, LOW);
    digitalWrite, (in2, HIGH);

are only executed once after (and if) the while ends

...

Sorry, I am very new to all of this and some of it may seem simple to some on this site but I am trying and have poured over videos and prior post to try and get this working.

Latest code

#include <DHT.h>
#include <LiquidCrystal.h>


#define DHTPIN 10                     //thermometer on pin 10
#define DHTTYPE DHT11                //DHT type 11


int photocell = A0;                     //photocell pin
int photocellReading;                   //analog reading from photocell
const int topSwitch = 35;               //top reed switch
const int bottomSwitch = 37;            //bottom reed switch
const int openDoorLED = 46;                   //red door open LED
const int closedDoorLED = 48;                 //green door closed LED
const int enA = 9;                            //door direction
const int in1 = 22;                            //door motor up
const int in2 = 24;                            //door motor down
int DoorisFullyOpen = 0;
int DoorisFullyClosed = 0;

DHT dht (DHTPIN, DHTTYPE);

void setup() {

  Serial.begin(9600);               //start the print screen
  dht.begin();                      //start dht for temperature

  //door switches pin modes
  pinMode(topSwitch, INPUT_PULLUP);            //top reed switch set as input
 // digitalWrite(topSwitch, HIGH);        //activate internal pullup resistor
  pinMode(bottomSwitch, INPUT_PULLUP);         //bottom reed swithc set as input
 // digitalWrite(bottomSwitch, HIGH);     //activate internal pullup resistor
  pinMode(photocell, INPUT);            //photocell analod input

  // set the motor control pins as outputs. This means pins 7,8, 9 are outputs to the l298n motor controller.
  pinMode(enA, OUTPUT);                 //pin 9 from arduino
  pinMode(in1, OUTPUT);                 //pin 8 from arduino
  pinMode(in2, OUTPUT);                 //pin 7 from arduino

  //door LED pin modes
  pinMode (openDoorLED, OUTPUT);        //red LED output
  pinMode (closedDoorLED, OUTPUT);      //green LED output

  // Switch State decleration
  DoorisFullyOpen = digitalRead(topSwitch);
  DoorisFullyClosed = digitalRead(bottomSwitch);

 if (! DoorisFullyClosed == HIGH) {
  while(! DoorisFullyClosed == HIGH){
    analogWrite, (enA, 255);
    digitalWrite, (in1, LOW);
    digitalWrite, (in2, HIGH);
    DoorisFullyOpen=digitalRead(topSwitch);
    DoorisFullyClosed=digitalRead(bottomSwitch);
    }
    digitalWrite(in1,LOW);
    digitalWrite(in2,LOW);}
  }







void loop()
{

  photocellReading = analogRead(photocell);
  float f = dht.readTemperature(true);

  Serial.print("Analog Reading = ");
  Serial.println(photocellReading);
  Serial.print("Top Switch =     ");
  Serial.println(digitalRead(topSwitch));     //display top switch current value
  Serial.print("Bottom Swith =     ");
  Serial.println(digitalRead(bottomSwitch));  //display bottom switch current value
  Serial.print("Temperature = ");
  Serial.println(f);
  delay(2000);


  
  photocellReading = analogRead(photocell);
  DoorisFullyOpen = digitalRead(bottomSwitch);
  DoorisFullyClosed = digitalRead(topSwitch);


  if (DoorisFullyClosed = HIGH && photocellReading > 200) {
    DoorisFullyOpen;
    DoorisFullyClosed;
        delay (2000);
    while (!DoorisFullyOpen) {
      analogWrite(enA, 255);
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      DoorisFullyOpen = digitalRead(bottomSwitch);
      DoorisFullyClosed = digitalRead(topSwitch);
    }
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }


  if (DoorisFullyOpen = HIGH && photocellReading < 200)
    delay (2000);
    while (! DoorisFullyClosed) {
    analogWrite(enA, 255);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    DoorisFullyOpen = digitalRead(bottomSwitch);
    DoorisFullyClosed = digitalRead(topSwitch);
  }
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  
if (digitalRead(topSwitch), HIGH) {            //if top switch is connected
    digitalWrite(openDoorLED, HIGH);              //turn on red led
    Serial.println("Door Open");                  //print door open
  }
  else {
    digitalWrite(openDoorLED, LOW);
  }            //but if not turn the red led off

if (digitalRead(bottomSwitch), HIGH) {        //if the bottom switch is connected
    digitalWrite(closedDoorLED, HIGH);            //turn on the green led
    Serial.println("Door Closed");                //print doopr cloosed
  }

reading - thx

in1 and in2 are what is labeled on the L298N controller and as far as I could figure out and have seen before I have these set to turn on and off the motor.

Right now if I activate the bottom reed switch the motor runs regardless of the photocell reading and both LED lights stay on regardless of switch position and the motor stops when the reed switch is moved from the bottom position.

can you clarify how your motor works, what really gets it to move?

const int enA = 9;                            //door direction
const int in1 = 22;                            //door motor up
const int in2 = 24;                            //door motor down

then you repeat this:

analogWrite, (enA, [color=red]255[/color]); 
digitalWrite, (in1, LOW); 
digitalWrite, (in2, HIGH);

what is 255?

what does your L298N controller look like?

can you post a link to your version? and which Arduino pins are linked where?

It is just a normal 12V motor I have one wire on the "Out 1" and one on the "Out 2" on the control board. I have the power coming direct from a 12V source to the +12v and GND connectors. As of right now the only way to get it to move is to activate the bottom reed switch.

255 is to turn the motor on high speed. I tried to mimic two other people's codes to do the same thing and they both had that command in there.

Thanks for taking the time to help!

https://www.amazon.com/Qunqi-Controller-Module-Stepper-Arduino/dp/B014KMHSW6/ref=sr_1_1?ie=UTF8&qid=1469808753&sr=8-1&keywords=l298n

https://www.amazon.com/gp/product/B00PD92EJ8/ref=oh_aui_detailpage_o06_s00?ie=UTF8&psc=1

digital ground to Photocell ground

Pin 10 to DHT out pin 9 to enA on controller pin 22 to in1 pin 24 to in2 pin 35 to negative on reed switch with 470ohm resistor pin 37 to negative on reed switch with 470 ohm resistor pin 46 to positive red led then negative to 220 ohm pin 48 to positive green led then negative to 220 ohm

analogWrite, (enA, 255); is legit - just makes pin 9 high.

digitalWrite (enA, HIGH); would do the same.

CrossRoads: analogWrite, (enA, 255); is legit - just makes pin 9 high.

sure - in his case this is actually a PWM pin he needs to drive - this sets the speed to maximum for the motor

@gerard

can you connect only this (as you have) and disconnect the rest

pin 9 to enA on controller
pin 22 to in1
pin 24 to in2

Motor on OUT2
12V in the Motor Power supply
GND to ground

Make sure you KEEP the 12V jumper and that you REMOVE the ENA jumper

and load this program and run it and report on what you see

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 9;
int in1 = 22;
int in2 = 24;

void setup()
{
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}


// this function will run the motor in both directions at a fixed speed

void constantSpeed()
{
  // turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 200);
  delay(2000);

  // now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(2000);

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

// maximum speed is determined by the motor itself and the operating voltage
// the PWM values sent by analogWrite() are fractions of the maximum speed possible

void speedVariation()
{
  // turn on motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

  // accelerate from zero to maximum speed
  for (int i = 0; i < 256; i++)
  {
    analogWrite(enA, i);
    delay(20);
  }

  // decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i)
  {
    analogWrite(enA, i);
    delay(20);
  }

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

void loop()
{
  constantSpeed();
  delay(1000);
  speedVariation();
  delay(1000);
}