Go Down

Topic: LDR and Stepper Motor on Arduino UNO (Read 2494 times) previous topic - next topic

labuschagne3

May 25, 2016, 05:38 pm Last Edit: May 25, 2016, 07:40 pm by Coding Badly
Hi

I am Johannes and I am new at this, can someone please help?

I am trying to make a door opener I need the door to open in the morning when it get light and close it when the sun go down.

The code I got at the moment make the motor turns one way and when I cover the LDR the motor turn the other way. the problem I got is I need it to give a certain amount of steps in one direction and stop until the value change then turn a certain amount of steps to the other direction and stop until the vale change again.

I am not sure how to let the motor stop after certain amount of steps and stay stopped until the value change.

Please see code below.


Code: [Select]
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;
  }

}


Moderator edit: [code] [/code] tags added.

labuschagne3

#1
May 25, 2016, 06:04 pm Last Edit: May 25, 2016, 07:41 pm by Coding Badly
Hi

I am Johannes and I am new at this, can someone please help?

I am trying to make a door opener I need the door to open in the morning when it get light and close it when the sun go down.

The code I got at the moment make the motor turns one way and when I cover the LDR the motor turn the other way. the problem I got is I need it to give a certain amount of steps in one direction and stop until the value change then turn a certain amount of steps to the other direction and stop until the vale change again.

I am not sure how to let the motor stop after certain amount of steps and stay stopped until the value change.

Please see code below.


Code: [Select]
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;
  }

}


Moderator edit: [code] [/code] tags added.

Robin2

Is this door on a chicken coop? There are several Forum Threads about chicken coop doors.

Please modify your post and use the code button </>
Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Also please use the AutoFormat tool in the IDE to indent the code properly and remove unnecessary white space to make the code easier to read.

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

PaulS

Quote
the problem I got is I need it to give a certain amount of steps in one direction and stop until the value change then turn a certain amount of steps to the other direction and stop until the vale change again.
How many steps? Until what value changes?

I would expect that the value read from the LDR would increase as the sun comes up, and decrease again as the sun sets. At some point, the reading will exceed the "it's light enough" threshold. At some point, the value will again fall below the "It's getting dark" threshold.

Some things to consider. How long should the value be above or below the threshold(s) before you take action? What should happen when a cloud passes by?

Code: [Select]
     step(false,0);
What is the point of taking 0 steps?


labuschagne3

Hi

Thanks for your reply

The step I will have to set later, but one revolution is 1600 steps.

If the value at A0 (Analog 0) changes from High to Low.

The motor must do all the steps in one direction and stop then wait until the Value of A0 change from High to Low then turn the other direction and do all the steps and then stop until the value of A0 change again from Low to High.

Robin2

Please don't double post - it just wastes time. I have already replied to your other Thread.

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

PaulS

Quote
If the value at A0 (Analog 0) changes from High to Low.
The value read from the analog pin will be in the range 0 to 1023. An LDR is not a digital device.

Coding Badly


Please do not cross-post.  Threads merged.


Go Up