I keep getting errors while compiling my chicken door sketc, need help debugging

I am trying to automate my chicken door to open and close according to how much sunlight is available. If the door were to fail to open when closed, fail to close when open, or never reach the intended position to flash an led.

/*
This is my Chicken door Sketch
*/

#include "Arduino.h"
#define debounce_delay 50  
int doordownPin = 5; //Door down Limit swictch
int doorupPin = 6;  // Door up limit switch
int commanddoorUp = 8;  // Move the door to the up position
int commanddoorDown = 9;  // Move the door to the down position
//starting an average light level
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int avelight = 0;// the average
// ending average light level
int analogPin = 3; //light meter

int averagelightLevel = 0;

void setup(){
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(13, OUTPUT);
  pinMode(A0, INPUT);
  Serial.begin(9600);  
}

void loop(){ 

  averagelightLevel(){
   total= total - readings[index];   // subtract the last reading:     
   readings[index] = analogRead(A0);// read from the sensor:
   total = total + readings[index];// add the reading to the total:      
   index = index + 1 ;// advance to the next position in the array:                    
   if (index >= numReadings)// if we're at the end of the array...              
   index = 0;// ...wrap around to the beginning:                          
   (avelight) = total / numReadings;// calculate the average:        
   delay(1000); // delay in between reads for stability 
  }
if (avelight) >= 600 (closeDoor);
if (avelight) <= 300 (openDoor);
}

//----------------------------------------------
// End of main program
//----------------------------------------------

 

void closeDoor(){
if (doorupPin) = HIGH); 
(commanddoorDown) HIGH;
delay (2000);
if (doordownPin) != HIGH;
fault();
}

void openDoor(){
if (doordownpin) = HIGH
(commanddoorUp) = HIGH;
delay (2000);
if (doorupPin) != HIGH;
fault();
}

void fault(){
  digitalWrite(13, HIGH);
  delay (500);
  digitalWrite(13, LOW);
  delay (500);
  fault();
}
void loop(){ 

  averagelightLevel(){
   ...
  }
  ...
}

What exactly are you trying to do here?

You've defined averagelightLevel as a variable, and now you are trying to define it as a function. On top of that, you are trying to define it within another function, which doesn't work.

(avelight) = total

Parenthesis are completely unnecessary here

if (doordownpin) = HIGH

This is not how you write an if statement, I recommend looking at some of the examples on how to properly write one.

if (avelight) >= 600 (closeDoor);
if (avelight) <= 300 (openDoor);

This is also not how you call a function. Again, I recommend looking at example sketches on how to properly call one.

int doordownPin = 5; //Door down Limit swictch
int doorupPin = 6;  // Door up limit switch
int commanddoorUp = 8;  // Move the door to the up position
int commanddoorDown = 9;  // Move the door to the down position
...
if (doorupPin) = HIGH); 
(commanddoorDown) HIGH;
delay (2000);
if (doordownPin) != HIGH;

This is not how you check the state of a pin; you're checking the numbers of the pin against values HIGH and LOW. You need to be checking the state of the pin, which is done with digitalRead().

void fault(){
  ...
  fault();
}

Recursion without an exit condition is a big no-no (I'd also go as far as to say no recursion at all on the Arduino, considering the limited memory).

if (doorupPin) = HIGH);
  • Number of parenthesis doesn;t match.
    = is assignment, == is comparison.
    Semicolon at the end of an if statement means just check it, don't do anything based on the condition.

You need to work through some of the examples that come with the IDE, particularly how to call functions, and the return types of functions.

Arrch:

void loop(){ 

averagelightLevel(){
  ...
 }
 ...
}



What exactly are you trying to do here?

You've defined averagelightLevel as a variable, and now you are trying to define it as a function. On top of that, you are trying to define it within another function, which doesn't work.

I am trying to write a function where the light level read on A0 is taken 10 times with a delay of (1000) between each reading. These 10 readings will then be averaged and the result given to the if statements to determine if the door should open or close. This at least in my thinking would keep the door closed should the neighbors headlights shine on the sensor or lightning strikes occur.

redneck:
I am trying to write a function where the light level read on A0 is taken 10 times with a delay of (1000) between each reading. These 10 readings will then be averaged and the result given to the if statements to determine if the door should open or close. This at least in my thinking would keep the door closed should the neighbors headlights shine on the sensor or lightning strikes occur.

In that case, you need to declare and implement the function outside of all other functions (like how you did with closeDoor and openDoor) and call it from within the loop.

Here are some suggestions for developing code:

Start from a known working point, such as an example sketch.

Write the skeleton of your program using English language pseudo-code in comments. Say things like

// check the ambient light level
// if the light level is bright, check the door state (open or closed)
// if the door is closed, open it

// if the light level is dark, check the door state
// if the door is open, close it

Now start writing the real code, using your comments as a guide. Click the 'Verify' button in the IDE FREQUENTLY, as in, every statement or three. If verification fails, fix it before you move on. If you write a whole bunch of code and then go to verify, you'll have a harder time finding the problem.

Also as a tip, for your constants, avoid generic names like HIGH or LOW. Define and use things like DOOROPEN or DOORCLOSED, makes debugging a lot easier.

avoid generic names like HIGH or LOW

HIGH and LOW are not generic names, they are defined constants.

http://arduino.cc/en/Reference/Constants

dxw00d:

avoid generic names like HIGH or LOW

HIGH and LOW are not generic names, they are defined constants.

http://arduino.cc/en/Reference/Constants

Very well... not generic... but unclear. HIGH doesn't tell me if a chicken coop door is open or closed unless I read a bunch of other code and/or know what kind of hardware is being used to figure out what HIGH is supposed to mean in this context. When the door is open, it could be HIGH or LOW depending on if you're using a NC limit switch or a NO limit switch or a photodiode or a photoresistor or...

Well, you're going to have a lot of problems with sketches here then. HIGH and LOW are used a lot.

dxw00d:
Well, you're going to have a lot of problems with sketches here then. HIGH and LOW are used a lot.

It's just a tip. You're welcome to use it or discard it. :slight_smile:

I've not had a problem so far.