This is my first program using the arduino, and I am sure I have made a 'silly' beginners mistake somewhere in the code and was wondering if someone could cast their eye over my code please.
Project:
Electronic dog door that raises and lowers dependant upon the light level (ie raises in the morning, lowers at night).
Inputs:
- Light sensor (LDR).
- Top limit microswitch
- bottom limit mircroswitch
Outputs:
- Motor power
- Motor direction (to lift or lower door)
What is supposed to happen:
At dawn - door lifts up, hits top limit micro, then stops.
At dusk - door lowers, hits bottom micro, then stops.
What is actually happening:
At dawn, door lifts up, hits top micro, then stops - perfect.
At dusk, door keeps opening and closing for 3-4 full times, then stops. Weird.
The way I tested was to use a torch at night time. I shined the torch onto the LDR to re-create dawn, then I simply turned the torch off for dusk.
Here is a copy of my actual code. I would be very grateful if someone can review my code and point out my silly mistake.
Greatly appreciated.
John
/*
created June 2011
Electronic pet door project.
The circuit:
* light_sensor is an input connected to Analog input 0
* top_limit_switch is an input on Digital input 5
* bottom_limit_switch is an input on Digital input 4
* motor_power is an output on Digital output 6
* motor_direction is an output on Digital output 7
* the drive motor is connected to the door through a second cog, which reverses the direction.
*/
// constants won't change. They're used here to show what pins certain things are connected to on the Arduino.
const int light_sensor = 0; // the analog port that the LDR is connected to.
const int top_limit_switch = 5; // the digital pin that the top limit switch is connected to.
const int bottom_limit_switch = 4; // the digital pin that the bottom limit switch is connected to.
const int motor_direction = 6; // the digital pin that changes the direction of the motor (0 = down, 1 = up)
const int motor_power = 7; // the digital pin that turns the power on to the motor.
// these are variables and they change throughout the program.
int light_level = 0; // declare light_level as an integer variable.
int top_switch = 0; // declare top_switch as an integer variable.
int bot_switch = 0; // declare bot_switch as an integer variable.
void setup()
{
pinMode(light_sensor, INPUT); // tell the Arduino that this is an input.
pinMode(top_limit_switch, INPUT); // tell the Arduino that this is an input.
pinMode(bottom_limit_switch, INPUT); // tell the Arduino that this is an input.
pinMode(motor_power, OUTPUT); // tell the Arduino that this is an output.
pinMode(motor_direction, OUTPUT); // tell the Arduino that this is an output.
}
void loop()
{
light_level = analogRead(light_sensor); // read the light sensor and the top and bottom switches before we go into the loops
top_switch = digitalRead(top_limit_switch);
bot_switch = digitalRead(bottom_limit_switch);
while ((light_level < 50) && (bot_switch == 0)) // if it is dusk and the bottom switch has not been hit.
{
digitalWrite(motor_direction, LOW); // change motor direction to 'down'
digitalWrite(motor_power, HIGH); // turn motor on
bot_switch = digitalRead(bottom_limit_switch); // read the bottom switch, the door might have touched it.
} // go back up to the curly bracket above
while ((light_level > 100 ) && (top_switch == 0)) // if it is dawn and the top switch has not been hit.
{
digitalWrite(motor_direction, HIGH); // change motor direction to 'up'
digitalWrite(motor_power, HIGH); // turn motor on (and down the door goes)
top_switch = digitalRead(top_limit_switch); // read the top switch, the door might have touched it.
} // go back up to the curly bracket above
digitalWrite(motor_power, LOW); // turn motor off when either limit switch is hit.
} // go back up to the top of the program