do you like my code

i was trying to copy this which is basically a chicken coop door but the guy who made it didn't reply to me when i asked if i could borrow his code so i had a go at it myself even though i don't have a clue about arduino or programming.

i'll have to adjust the photoresistor thresholds for when it's dark or morning and the amount of spins of the stepper motor so the door goes up the right amount, and maybe do something to try to make it sleep so it is not awake all the time wasting battery, but i tried it all hooked up with the code and the concept works, even if the programming is probably a mess as i took it from everywhere with bits myself, what do you think.

#include <Stepper.h>
#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(STEPS, 8, 10, 9, 11); 
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider

boolean x = true;

void setup() {
  Serial.begin(9600);

}

void loop() {
   photocellReading = analogRead(photocellPin);  
   Serial.print("Analog reading = ");
  Serial.println(photocellReading);     // the raw analog reading
  delay(5000);
if ( photocellReading<400 && x == true){
  stepper.setSpeed(5); // 5 rpm
  stepper.step(2038); // do 2038 steps -- corresponds to one revolution in one minute
   x = false;
}

else if (photocellReading >600 && x == false){  
stepper.setSpeed(10); // 10 rpm
stepper.step(-2038); // do 2038 steps in the other direction with faster speed -- corresponds to one revolution in 10 seconds
x = true;
}
}

do you like my code

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0

Although it does not matter I find that using the A* pin names for analogue inputs less confusing when reading code. The pin number does not need to be an int, a byte would do, and as it will never change its value it could be declared as const or you could use a #define

I presume that the code does what you want, which is good, but you might like to think ahead and consider the possibility of needing to read inputs in loop(), perhaps a manual override, which means that the delay() could become a nuisance

My preference would be to use switches to detect when the door is closed or fully open, blindly driving a stepper motor a specific number of steps can give you problems if the door ever sticks. Your code also assumes the door will be in a specific position when the arduino is powered on, something you will have to remember whenever you change the batteries.

Which arduino are you using? Some boards are more suitable than others for low power operation, and there are modifications you can make to reduce power consumption, such as disabling the power LED.

(Edit)
Another possibility, if you use limit switches, then a continuous-rotation servo could be used instead of a stepper motor, making the design a bit simpler.

UKHeliBob:

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0

Although it does not matter I find that using the A* pin names for analogue inputs less confusing when reading code. The pin number does not need to be an int, a byte would do, and as it will never change its value it could be declared as const or you could use a #define

I presume that the code does what you want, which is good, but you might like to think ahead and consider the possibility of needing to read inputs in loop(), perhaps a manual override, which means that the delay() could become a nuisance

i agree i must have stolen that part from somewhere but i've seen it written A0 too so it was a bit confusing to me at first with there being digital pins too. i know what you mean about it not being a variable i changed it to: const photocellPin = A0;

it does do what i want which was great feeling of relief considering it didn't for about 4 days while i was learning. i thought about a manual override but to be honest i was just pretty happy to get this out and working and putting that in is not at my level of expertise yet.

i was thinking of making the delay even bigger because i didn't want the arduino working too hard to conserve battery power but you're probably right i can make it 1000 or even 100 seems to be what people use for sensors and look into making the arduino only wake a few times a day to check the sensors instead would probably be the most efficient way to go.

i just thought i'd share my joy and you didn't find too much to dislike which is good

david_2018:
My preference would be to use switches to detect when the door is closed or fully open, blindly driving a stepper motor a specific number of steps can give you problems if the door ever sticks. Your code also assumes the door will be in a specific position when the arduino is powered on, something you will have to remember whenever you change the batteries.

Which arduino are you using? Some boards are more suitable than others for low power operation, and there are modifications you can make to reduce power consumption, such as disabling the power LED.

(Edit)
Another possibility, if you use limit switches, then a continuous-rotation servo could be used instead of a stepper motor, making the design a bit simpler.

i saw that guys design if you saw the link and thought wow that'd be great so went and got the pieces and didn't look at alternatives beforehand. i've rationalised that it will be okay based on i'll put water outside if it gets stuck and that we rarely get predators so i will notice before it's a problem.

i never thought about the positioning when changing the batteries, i would have to change the batteries at the end of the code or the string would get messed up, that is a good observation.

im using the arduino uno which i know isn't very good for power conservation i might look at getting a arduino pro mini depending on how this goes, if the stepper motor chews through batteries quite quickly then i figured i will have to look at getting a bigger battery or with a solar panel instead.

from what i've read since the servo motor is quite greedy with power too or it might have been another one i can't remember but the whole switches thing i'm not familar with yet but it will be something to look into for sure.

jumpingjimmy:
what do you think.

Glad it works the way you want!

Now, get yourself some extra style points by removing extra white space (blank lines that aren't used to separate functions and the like). And then hit "Ctrl+t" to auto-format your code. Makes it easier to read and easier to keep track of where you are when programming.

i changed it to:

const photocellPin = A0;

That does not compile

A chicken coop door opener would probably be a good candidate for solar power to charge batteries. Lots of time to recharge, with a long interval between opening/closing of the door.

The delay length will not effect the power consumption significantly, the arduino is still running and using power, it just doesn't do anything useful during the delay. You can put the arduino in sleep mode to save significantly on power, waking it at intervals to check the light level.