Go Down

Topic: LDR and stepper motor (Read 999 times) previous topic - next topic

labuschagne3

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.

Code:

 int dirPin = 8;

 int stepperPin = 9;

 void setup() {

   Serial.begin(9600);

   pinMode(dirPin, OUTPUT);

   pinMode(stepperPin, OUTPUT);

   pinMode(A0, INPUT);

 }

 void step(boolean dir,int steps){

   digitalWrite(dirPin,dir);

   delay(50);

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

   {

     digitalWrite(stepperPin, HIGH);

     delayMicroseconds(800);

     digitalWrite(stepperPin, LOW);

     delayMicroseconds(800);

   }

 }

 void loop()

 {

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

   int lowVal = 200;

   int highVal = 250;

   Serial.print(" s0 ");

   Serial.print(s0);

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

 {

     step(true,900);

     delay(500);

     step(false,0);

     delay(500);
     lastRoundHigh = 1;
   }

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

   {

     step(true,0);

     delay(500);

     step(false,900);

     delay(0);
     lastRoundHigh = 0;
   }

 }

Go Up