Go Down

Topic: LDR activated stepper  (Read 898 times) previous topic - next topic

Robochook

Hi there, I have an issue with the attached sketch whereby it'll only activate when the LDR is below the threshold set in the first if statement. I have tried to add other statements with apposing thresholds without any luck.
So, the goal;
'To move a flap open when its daylight then close it when it gets dark'

So when it gets light the LDR threshold is met and the stepper moves NBSTEPS. It then stays at that point until the lower threshold is met (Dark) then steps the other direction to remain closed for the night.

Hardware being used;
UNO R3
LDR (Photoresistor)
ULN2003 driver board with 28byj-48 stepper motor

So the sketch attached does nothing until the threshold is met (<= 150 reading from the LDR) but I need it to stop after its reached its set steps (NBSTEP). Unfortunately it pauses for the delay period then reverses its direction.

I have read some chicken coop' threads here already but none have yield the answer i was after. I suspect its something very simple that i'm missing here. If anyone can assist or point me to a thread that answers the question I would greatly appreciate it.

Section of Loop below

#############################
  while (stepsLeft > 0 && photocellReading <= 150) {
    currentMicros = micros();
    if (currentMicros - lastTime >= STEPTIME) {
      stepper();
      time += micros() - lastTime;
      lastTime = micros();
      stepsLeft--;
    }
    delay(1);
    writeStep(arrayDefault);    //This appears to stop the motor driver, which is good
  }
  Serial.println(time);
  Serial.println("Checking Photocell value");
 delay(2000);
 // Added this if below to try and activate it when it becomes light but this doesn't work. Seems to stay in its pause, rotate alternate direction loop.
if (stepsLeft > 0 && photocellReading >= 150)
  stepper();
  currentMicros = micros();
  Clockwise = !Clockwise;
  stepsLeft = NBSTEPS;
}
##########################

Cheers

Robin2

Your program seems very convoluted for a relatively simple task.

If it was mine the code in loop() would be something like this

Code: [Select]
void loop() {
   photocellReading = analogRead(photocellPin);
   if (photocellReading > openThreshold) {
        doorMove = 'o';  // o for opening
   }
   if (photocellReading < closeThreshold) {
        doorMove = 'c';   // c for closing
   }
   updateDoorPosition();
}


and then

Code: [Select]
void updateDoorPosition() {
   if (doorMove == '0' and doorPosition == 'c') { // c for closed
     // code to move door to open position
     // and change doorPosition to 'o'
   }
   // and the same for doorMove == 'c'
}



You should ensure that there is  significant gap between the threshold for opening and closing. Otherwise the program will just oscillate between the two.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robochook

Robin, thanks for the quick response. I'll see how I can rework my sketch to integrate your suggestion.
I may end up using a library like accelstepper.h also to simplify the sketch.

Cheer's

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How do you know where your door is when you turn on the controller, open, closed, 1/4 open, 1/2 open.

When you make the door close how do you know when it is closed?
When you make the door open how do you know when it is fully open?


Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up