LDR loop

Hi all

I am struggling to figure out software to make my project work and I would appriciate any help I can get please :o I am building a chicken door opener and closer.

I have a light sensor and a motor connected via the Arduino. I need to write code that allows me to continously monitor the light sensor and once the sensor hits a value, I need the motor to turn and when the light sensor drops a value I need the motor to do turn again.

I found code that allows me to continously monitor the light (in a loop) and move the motors clock and anti-clockwise but putting everything in the loop does not help.

I need to put the LDR sensor in the loop and it records the value of the sensor, but then when a value is triggered come out of the loop and turn the motor till the job is done and stop moving the motor, then go back into the loop till the next LDR value is hit and then come out of the loop turn the motor, stop the motor and go back into the loop. It is kind of a loop in a loop.

I am looking for an example of a loop in a loop program. Can someone please point out some examples?

Robin2’s excellent tutorial is a good place to start

Thanks I will check it out.

but putting everything in the loop does not help.

The loop has to contain the origin of everything you want to do repeatedly.

You don’t go in and out of it, your code is contained within it. What you do is to use if statements to conditionally run some lines of code and miss out others. Breaking down actions into functions will help you organise things. But ultimately if you want code run it has to be within the reach of the loop function.

The setup function is where you put code you want to do only once at switch on or reset.

spotynoise:
Thanks I will check it out.

If you need more help please post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

…R

spotynoise:
I am looking for an example of a loop in a loop program.

Typically that's the wrong approach; can usually get by with an if() in loop(). If you just scan down the code in the thread linked in #1 (even without understanding it first time round*) you'll see a dozen or more if() statements.

Secret is to let all run as quickly as it can: loop() can be so fast that when sensors are changing quickly, the if() will get it next time round even if it had just missed it this time...

  • and you probably won't, you're not alone, hang in there :wink:

Hi there guys, thanks for all your tips :wink:

is this the place to post my fustrations :stuck_out_tongue_closed_eyes: of trying to code this?

I have learnt about for loops and while loop and have to get my head around how to make it work. I also learnt about millis function.

I have learnt about for loops and while loop and have to get my head around how to make it work.

It is rather like saying I am trying to design a wrist watch but I don’t know how clocks work. I keep putting the parts in a bag and shaking them up but I can’t even get it to tick.

OP, your response should have been based on reply #4. We also experience frustration when people don't respond to requests.