Laser and LDR programming

Hi, i have LDR, Laser, and a Gear motor and a microcontroller (Arduino Uno) to create a small conveyor. when the laser is block, then the gear motor will stop as the object passed through the laser. which is i tried and its working…

now what if the laser should be pass “twice” before it will stop?
like two object should pass the laser before it stop…
ex. if(i=0;i<2;i++)

can you guide me. I don’t know how to program it.

If you mean that you only want the motor to stop when you your beam is blocked for the second time you just need to increment a counter every time the beam is blocked. When the count gets to 2 stop the motor and reset the counter back to 0.

...R

Start by posting the code that you are using now for one object.

if(i=0;i<2;i++)That is not the right way to do what you want (apart from the if and 2 curly brackets). As Robin has suggested you need a counter but your code snippet has a mixture of a test for a value and a loop that counts from zero to 1.

I’m testing now, am i in the correct path?
int ldr=A0;
int ldrstate=0;
void setup()
{
pinMode(ldr,INPUT);
}
void loop()
{
int i;
ldrstate=digitalRead(ldr);
for(i=0;i<2;)
{
if(ldrstate==LOW) i+1;
}
Serial.print(i);
}

That code will never leave the for() loop. The reason is that it never updates the ldrstate inside the loop. You need to keep doing digitalRead() to update the information about the outside world.

void loop() {
  while(digitalRead(ldr)){};   //there is nothing in front of the sensor
  while(!digitalRead(ldr)){};  //the first object is passing in front of the sensor
  while(digitalRead(ldr)){};   //there is nothing in front of the sensor

  //OK, now the second object is now in front of the sensor
}

This code obviously does nothing but continually read the sensor so it's not very useful. But it depends what else you need the Arduino to do. Perhaps this is enough?

You are a long way from doing what you want. Rather than write a long description of what is wrong try this

const byte ldr = A0;           //const because it will not change and byte to save memory
byte counter = 0;              //counter for the number of items seen
byte currentLdrState = HIGH;   //variables to remember LDR states
byte previousLdrState = HIGH;

void setup()
{
  Serial.begin(9600);
  pinMode(ldr, INPUT);
}

void loop()
{
  currentLdrState = digitalRead(ldr);      //read the LDR state
  if (currentLdrState == LOW && previousLdrState == HIGH)  //has the LDR changed state ?
  {
    counter++;                                //add 1 to the counter
    previousLdrState == currentLdrState;      //save the current state
  }

  if (counter >=2)                            //have 2 items passed the LDR ?
  {
    //the LDR has counted 2 items so do what you want here
    Serial.println("2 items have passed the LDR");
    counter = 0;                              //put the counter back to zero for next time
  }
}

Note that I have assumed from your program that the LDR pin goes low when the laser is interrupted by an object. If it goes HIGH, and that will depend on how it is wired, you will need to change the logic of the test to suit.

Try this and let us know how you get on.