Photocel readings to open chicken coop door - need help verifying code

howdy,

somewhat of a newbie… so please be gentle. =^ |

i have an arduino mega 2560, hooked up to a stepper motor (ln298n) driving a 24v/25rpm motor, which i’m trying to get to trigger via photocel readings. (to drive a chicken coop door)

driving me a bit crazy that i can’t get a simple if statement to work correctly. the readings within serial look correct, but the functions are simply toggling and not if/else. exa:

Photocel Analog Reading = 491 - Light

  • it’s dark out, close the coop door
    Photocel Analog Reading = 490 - Light
  • it’s light out, open the coop door
    Photocel Analog Reading = 241 - Light
  • it’s light out, open the coop door

would someone mind taking a peek at my first attempt at coding this? (haven’t yet even started to deal with the microswitches or lcd) i’m just trying to get the motor to trigger 1 direction with a dark reading/ another direction with a light reading.

any help would be appreciated.

cheers,
//dave

here’s my code:

// #include <LiquidCrystal.h>

// ************************************* definitions *************************************
 /* pin assignments */
 
 // door motor
int enable_chicken_door_motor_b = 7; // enable motor b
int direction_close_chicken_door_motor_b = 8; // direction close motor b
int direction_open_chicken_door_motor_b = 9; // direction open 2 motor b

//microswitches

// top switch
int top_switch_pin = 1;         // top switch is connected to pin 1
int top_switch_pin_val;         // top switch var for reading the pin status

// bottom switch
int bottom_switch_pin = 31;      // bottom switch is connected to pin 1
int bottom_switch_pin_val;      // bottom switch var for reading the pin status

// lcd
// LiquidCrystal LCD (2, 8, 4, 5, 6, 7);

// photo cell controller ints
int photocellPin = 0;           // photocell connected to analog0
int photocellReading;           // analog reading of the photocel

// ************************************* the setup *************************************

void setup ()
{
pinMode (enable_chicken_door_motor_b, OUTPUT);          // enable motor pin = output
pinMode (direction_close_chicken_door_motor_b, OUTPUT); // motor close direction pin = output
pinMode (direction_open_chicken_door_motor_b, OUTPUT);  //  motor open direction pin = output
pinMode (top_switch_pin, INPUT);                         // top switch pin as input
pinMode (bottom_switch_pin, INPUT);                      // bottom switch pin as input

Serial.begin (9600);                                    // set the serial connection at 9600 baud

}

// ************************************* functions *************************************
// operate the coop door motor

// photocel

void readPhotoCel() {
 photocellReading = analogRead(photocellPin);  
  Serial.print(" Photocel Analog Reading = ");
  Serial.print(photocellReading);
  
//  set photocel threshholds
  if (photocellReading < 10) {
    Serial.println(" - Dark");
  } else if (photocellReading < 200) {
    Serial.println(" - Dim");
  } else if (photocellReading < 500) {
    Serial.println(" - Light");
  } else if (photocellReading < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }
  
 delay (5000);
}
  

void close_coop_door_motor_b()
{
//  LCD.print ("Coop Door Motor B Speed:");    
    Serial.println(" - it's dark out, close the coop door");
    digitalWrite (direction_close_chicken_door_motor_b, HIGH); // turn close direction motor on
    digitalWrite (direction_open_chicken_door_motor_b, LOW);
    analogWrite (enable_chicken_door_motor_b, 255);
    delay (5000);
}

void open_coop_door_motor_b()
{


    Serial.println(" - it's light out, open the coop door");
    digitalWrite (direction_close_chicken_door_motor_b, LOW);
    digitalWrite (direction_open_chicken_door_motor_b, HIGH);
    analogWrite (enable_chicken_door_motor_b, 255);
    delay (5000);
}

// ************************************* the loop *************************************
void loop() {
// if it's dark out, close the coop door  
    if (photocellReading < 10)  { // if the photocel reads less than 10
    readPhotoCel();             //  photocel reading
    close_coop_door_motor_b();  //  close the coop door
  } 
  
// otherwise it's light out, open the coop door
  else {                           //  the photocel reads more than 100 
    readPhotoCel();                //  photocel reading
    open_coop_door_motor_b();      //  OPEN the coop door
  }
}
    if (photocellReading < 10)  { // if the photocel reads less than 10
    readPhotoCel();             //  photocel reading

Shouldn’t you read the photo cell first, and then test what value it returns? Rather than the other way around?

Things that don’t change like pin numbers would be better if assigned as constants

// door motor
const int enable_chicken_door_motor_b = 7; // enable motor b
const int direction_close_chicken_door_motor_b = 8; // direction close motor b
const int direction_open_chicken_door_motor_b = 9; // direction open 2 motor b

Plus the other 3 pins…

const int top_switch_pin = 1;         // top switch is connected to pin 1

Your not using it yet but this will interfere with serial communications so if you have spare pins then maybe move it.

// photo cell controller ints
const int photocellPin = A0;           // photocell connected to analog0

Analogue 0 is normally referred to as A0 not 0.

  analogWrite (enable_chicken_door_motor_b, 255);

To do this you may as well use digitalWrite unless your expecting to slow start motors at a later date.

In the main loop you should maybe read the photocell before acting on it’s result. You would be advised to also have a dead band on it’s reading to stop door potentially opening/closing several time at dawn/dusk. Close at <10 but don’t open till say >20. For safety you would have a variable to hold the state of the door (open/closed) and only call open/close if the state does not match the desired state. As it stands it will call door open/close continually but I assume the micro switches should trap this in later code but if a switch fails you could burn out the motor quickly. You should also put some form of time delay and then disable the motor if it’s not tripped the expected switch in time.

Analogue 0 is normally referred to as A0 not 0.

I disagree. The A0 alias was defined to be used when using the analog pin as a digital pin. When using the analog pin as an analog pin, 0 is the correct value.

1st off... thanks all for responding (especially so quickly) loving the fact that the arduino nation is helpful!

nick:

Shouldn't you read the photo cell first, and then test what value it returns? Rather than the other way around?

indeed... that was a big "duh" on my part. thanks!

riva:

Things that don't change like pin numbers would be better if assigned as constants

ah... makes complete sense. changed them.

Your not using it yet but this will interfere with serial communications so if you have spare pins then maybe move it.

a little confused... you mean since i'm not yet using it, this will interfere? (so i should comment them out for now) and "move" it?? you mean connect it to other pin(s) b/c i'm using "1" for analog?

both riva and pauls

Analogue 0 is normally referred to as A0 not 0.

yeah, i was a bit confused by this... in my research, i found different examples of each and went without using "A0" but you know, i just changed it to "A0" and poof the if statements began working correctly! yes!!!!!!!!!!

--i still need to research what paul s stated, as i have seen references to his reply.

=)

serial printout: Photocel Analog Reading = 23 - Dim - it's dark out, close the coop door Photocel Analog Reading = 22 - Dim - it's dark out, close the coop door Photocel Analog Reading = 742 - Bright - it's light out, open the coop door Photocel Analog Reading = 745 - Bright - it's light out, open the coop door Photocel Analog Reading = 745 - Bright - it's light out, open the coop door Photocel Analog Reading = 745 - Bright - it's light out, open the coop door Photocel Analog Reading = 734 - Bright - it's light out, open the coop door Photocel Analog Reading = 745 - Bright - it's light out, open the coop door Photocel Analog Reading = 745 - Bright - it's light out, open the coop door Photocel Analog Reading = 745 - Bright - it's light out, open the coop door Photocel Analog Reading = 746 - Bright - it's light out, open the coop door

thanks again!

dhnaves: a little confused... you mean since i'm not yet using it, this will interfere? (so i should comment them out for now) and "move" it?? you mean connect it to other pin(s) b/c i'm using "1" for analog?

Since you are using Serial, you can't use D1 or D0. If you try to use D1 as a digital input, you're going to have issues. Riva was simply pointing out that you should be using a different pin, to avoid the conflict.

Arrch:

ahhhhh... thanks. yes, i was having issues. so d0 d1 can't be used with serial.. guess i really need to read up on that. also, with a microswitch, those should really be used as analog anyway, yes? (i need to read up on that as well, but pretty sure mechanical switches are analog in nature?)

thanks again.

and thank you too, riva!

dhnaves: Arrch:

ahhhhh... thanks. yes, i was having issues. so d0 d1 can't be used with serial.. guess i really need to read up on that. also, with a microswitch, those should really be used as analog anyway, yes? (i need to read up on that as well, but pretty sure mechanical switches are analog in nature?)

thanks again.

and thank you too, riva!

Switches tend to be digital more often than analog. You can use the analogs, however, as digital inputs.

thanks for the 411... now i need to read all about it. (still new to this)

cheers