Question

I have been trying for the past few hours to get a light gate timer to work with 2 photocells and lasers. It is like a trip wire, when the 1st sensor is low (when it is tripped), it triggers to start the time. This then is supposed to continue and then stop the timer when the second sensor is triggered using the same method as sensor 1 is triggered. The problem that I am having now is that the timer never stops. It seems to be a problem with my second sensor, I have switched the second sensor and the sensor seems to be fine. The other problem is that the timer does weird things when the second sensor is triggered also. It also seems that each time I reset or place a new version of the code onto the Arduino, it does a different thing, very strange. I have tried various other codes but none seem to work. If you have a similar code or could help me with this code, that would be great.

Below is my code:

int sensor1 = 2;
int sensor2 = 3 ;

long startTime;
long endTime;
float elapsedTime;
long pause = 0;

void setup() {
  Serial.begin (9600);
  Serial.println ("RPV");

  pinMode (sensor1, INPUT);
  pinMode (sensor2, INPUT);

}

void loop() {

  if (analogRead(sensor1) == 0) {
    Start ();
    Serial.println (elapsedTime);
  }

  if (analogRead(sensor2) == 0) {
    Stop ();
    //Serial.println (endTime);

  }
}

void Start(){
  startTime = millis();
}

void Stop() {
  endTime = millis();
  elapsedTime = (endTime - startTime);
  elapsedTime = elapsedTime/1000;
  Serial.print("Time Seconds: ");
  Serial.println(elapsedTime);

}
Was it helpful?

Solution

Try testing the sensors by writing the analogRead to serial. My guess is that the analogRead will never really be 0 since there will always be ambient light.

Your threshold should be more analogRead(sensor1) < somevalue )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top