Go Down

Topic: LDR and stepper motor (Read 1 time) previous topic - next topic


Hi Everyone

I got a Arduino UNO board with a LDR and stepper motor connected to it.

I got code that works the motor will turn one way and when I put my finger on the LDR the motor turns the opposite direction. the problem I have is to program it so it give certain amount of steps when the output is low and then stop and when the value change to High it must give certain amount of steps to the opposite direction and stop until the value change again.

What I am trying to achieve is when it goes dark closing a door and when sun come up in the morning the door must open.


 int dirPin = 8;

 int stepperPin = 9;

 void setup() {


   pinMode(dirPin, OUTPUT);

   pinMode(stepperPin, OUTPUT);

   pinMode(A0, INPUT);


 void step(boolean dir,int steps){



   for(int i=0;i<steps;i++)


     digitalWrite(stepperPin, HIGH);


     digitalWrite(stepperPin, LOW);




 void loop()


  static int lastRoundHigh =2;
 int s0 = analogRead(A0);

   int lowVal = 200;

   int highVal = 250;

   Serial.print(" s0 ");


    if ((s0 >= highVal) && (lastRoundHigh != 1))





     lastRoundHigh = 1;

 else if ((s0 <= lowVal) && (lastRoundHigh != 0))





     lastRoundHigh = 0;


Go Up