DIY Light Gate

So I’m trying to build a light gate, using an LDR and an LED. I want this light gate to count items that pass through it, and once a certain number of items have passed through, a servo to be moved. After this, I want it to reset, and repeat.

I’m pretty amateur in coding, and so have been unsuccessful. Can anyone help me with this problem?

Here are two different ways I’ve tried to do this;

#include <Servo.h>

Servo servo;

int sensorPin = A0;
int sensorValue = 0;

int c = 0;
int t = 0;

void setup() {
Serial.begin(9600);
servo.attach(13);

}
void loop() {
delay(50);
sensorValue = analogRead(sensorPin);

Serial.println(sensorValue);

while(sensorValue<10) {
t + 1 ;}

if(t == 4);
{c + 1;}

if(c == 4) {
servo.write(180);
} else {servo.write(0);}

if(sensorValue > 200) { t * 0;}

}

and another

#include <Servo.h>

Servo servo;

int sensorPin = A0;
int sensorValue = 0;

int c = 0;
int t = 0;

void setup() {
Serial.begin(9600);
servo.attach(13);

}
void loop() {
delay(50);
sensorValue = analogRead(sensorPin);

Serial.println(sensorValue);

if(sensorValue<10) {
timer = millis();
} else { timer = 0;}

if(timer > 20) {
c + 1 ;}

if(c == 4) {
servo.write(180);
c * 0;
} else {servo.write(0);}

}

Thanks for the help!

while(sensorValue<10) {
  t + 1 ;}/code]This adds 1 to t when the condition is met then throws away the result

Try
[code]t = t + 1;

or

t++;

Your use of millis() is also way off beam. Have you read Using millis() for timing. A beginners guide ?[/code]

UKHeliBob:

while(sensorValue<10) {

t + 1 ;}/code]This adds 1 to t when the condition is met then throws away the result

Try

[code]t = t + 1;



or


t++;




Your use of millis() is also way off beam. Have you read [Using millis() for timing. A beginners guide](http://forum.arduino.cc/index.php?topic=503368.0) ?[/code]

Thanks for the help!

I want to start the timer when the LDR is giving a value less than 10, then when the value has been less than 10 for a certain period of time ( in my case 20ms), the timer reset, and count one onto a variable. Is it possible to use startMillis = millis() within the loop in order to achieve this?

Remember the value of millis() is the time since the Arduino started running, so yes you would have to save the millis() value in loop() when the LDR reading is less than 10.

If the reading is over 10 you should stop the timer. Just remember that you will not save millis() if the previous reading was less than 10, in that case you need to calculate how long it has been since the first < 10 reading.

Have a look at the following, it should give you an idea of what I mean.

int sensorPin = A0;
int sensorValue = 0;
int count = 0;
unsigned long timer = 0UL;

void setup() {

}

void loop() {

  sensorValue = analogRead(sensorPin);

  // should timer be off?
  if ( sensorValue >= 10 )
  {

    // clear timer
    timer = 0UL;

  } else {

    // if timer is not set, initialize it
    if ( timer == 0UL )
    {

      timer = millis();

    } else {

      // check timer
      if ( (millis() - timer ) >= 20 )
      {

        // increment count
        count++;

        // reset timer
        timer = millis();

      } // if


    } // else

  } // else


}