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.
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:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
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.
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:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
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 </> 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
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?
step(false,0);
What is the point of taking 0 steps?
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.
Please don't double post - it just wastes time. I have already replied to your other Thread.
...R
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.
Please do not cross-post. Threads merged.