Timer delay in code

I need help with my code, it was working great but I had a proximity sensor triggering at the same time as my photoelectric sensors. In reality the prox triggers but the photoelectric sensors don’t trigger until a half a second later. This time gap is causing my code to not count the photoeyes as high. I tried adding a timer but it won’t compile now. Am I using the wrong commands for the timer?
Added line 3-5 see comment!
Added line 45 – 72 – the meat of the code
Added line 89 – 1000 = 1 second delay before sampling, 500 = ½ second. That is if CLOCKS_PER_SEC is set correct!

//photoelectric counter
#include <time.h>
// not sure if you need the following, or what it should be,  hopefully already defined for you
//const int CLOCKS_PER_SEC 1000
const int NumberOfLanes = 8;
int LaneSensor[] = { D0, D1, D2, D3, D4, D5, D6, D7 };
int ProxSensor = A0;
int Cycle = 0;
int val = 0; //variable for reading pin status
int LaneCount[NumberOfLanes]; //counter variable for dectection
int CycleSensorDebounce = 0;

void setup()
{
 for (int i = 0; i < NumberOfLanes; i++)
 {
   pinMode(LaneSensor[i], INPUT_PULLDOWN);
 }
 pinMode(ProxSensor, INPUT_PULLDOWN);

 Serial.begin(9600);
 initializeLaneCount();
}

void initializeLaneCount()
{
 for (int i = 0; i < NumberOfLanes; i++)
 {
   LaneCount[i] = 0;
 }
 Cycle = 0;
}

String PublishLaneCount()
{
 String StringReturn = String(Cycle);
 for (int i = 0; i < NumberOfLanes; i++)
 {
   StringReturn += " ," + String(LaneCount[i]);
 }
 return StringReturn;
}

/*
    Implementation simple timeout

    Input: count milliseconds as number

    Usage:
        setTimeout(1000) - timeout on 1 second
        setTimeout(10100) - timeout on 10 seconds and 100 milliseconds
 */
void setTimeout(int milliseconds)
{
    // If milliseconds is less or equal to 0
    // will be simple return from function without throw error
    if (milliseconds <= 0) {
        return;
    }

    // a current time of milliseconds
    int milliseconds_since = clock() * 1000 / CLOCKS_PER_SEC;

    // needed count milliseconds of return from this timeout
    int end = milliseconds_since + milliseconds;

    // wait while until needed time comes
    do  {
        milliseconds_since = clock() * 1000 / ClocksPerSec;
        } while (milliseconds_since <= end);
}

void loop()
{
 val = digitalRead(ProxSensor);
 if (val == 0)
 {
   CycleSensorDebounce = 0;
 }
 else
 {
   if (5 >= CycleSensorDebounce);
   {
     CycleSensorDebounce++;
     if (5 == CycleSensorDebounce)
     {
       setTimeout(1000);  // delay 1 second before sampling sensor
       Cycle++;
       for (int i = 0; i < NumberOfLanes; i++)
       {
         if (digitalRead(LaneSensor[i]))
         {
           LaneCount[i] = LaneCount[i] + 1;
         }
       }
       if (Cycle%5 == 0)
       {
         Particle.publish("Pear_Counter", PublishLaneCount());
       }
     }
   }
 }
}

Usually you would want to use something with the millis() function, I’ve not seen clock() before.

Is there something special about your proximity sensor and photoelectric sensors that need to be synchronized?

I would do this for a simple timeout function, you can move it into another function if you want:

//Setup a cooldown time like you did
int sampleCooldown = 1000;
int milliseconds_since = 0;

//Your setup()

void loop() {
  if(millis() - milliseconds_since > sampleCooldown) {
    milliseconds_since = millis();
    //Do the things
  }
}

Do you need to have the device wait while the sensor returns a value? I usually allow my loop to run as fast as possible but use millis() to time certain actions or keep a tally like you do with CycleSensorDebounce.

edit: FYI your timeout function will do the same thing as delay(1000); as currently written. If your sensors require that you only poll them for data every certain intervals I recommend using millis() as I have setup but use an if statement for each sensor with their own cooldown variable.

Yes they need to be synchronized, I need them triggering at the exact same time. The problem is I can’t mount them so that they are triggered at the same time, the way I have them mounted the prox triggers just a half a second before the photoeyes but stays triggered while the photoeye is triggered. If they aren’t triggering together though it doesn’t record the photoeye as high. So basically I need to delay my pull for data to allow for the photoeye to catch up

I’m going to throw in a delay(1000) on line 57, I think that should do the trick.

Ok if I am understanding you correctly you need to delay your photeye from looking for a reading 0.5 seconds after the prox sensor is triggered then I would replace your setTimeout() function with a simple delay(500). settimeout() will not function correctly with the comment on line 4, and the normal delay function should suffice for your needs.

I believe this should work for you. Hopefully it’s alright I took out the debounce, I wasn’t sure if you needed it or not. The loop should only be able to read the lanes if the Proximity Sensor is returning a reading and it has been cleared again. This way you should not get values being added until everything has left the lane. You can adjust the delay using PhotoEyeDelay at the top of the code to better time the counting, but if there is an item in front of the Photoeyes at the same time that the proximity sensor is triggered I would remove the delay all together.

const int NumberOfLanes = 8;
int LaneSensor[] = { D0, D1, D2, D3, D4, D5, D6, D7 };
int ProxSensor = A0;
int Cycle = 0;
int val = 0; //variable for reading pin status
int LaneCount[NumberOfLanes]; //counter variable for dectection
bool ProxSensorCleared = true; //use this to avoid a loops counting the same items multiple times
int PhotoEyeDelay = 1000; //adjust the time to wait before checking the photoeyes in each lane

void setup()
{
 for (int i = 0; i < NumberOfLanes; i++)
 {
   pinMode(LaneSensor[i], INPUT_PULLDOWN);
 }
 pinMode(ProxSensor, INPUT_PULLDOWN);

 Serial.begin(9600);
 initializeLaneCount();
}

void initializeLaneCount()
{
 for (int i = 0; i < NumberOfLanes; i++)
 {
   LaneCount[i] = 0;
 }
 Cycle = 0;
}

String PublishLaneCount()
{
 String StringReturn = String(Cycle);
 for (int i = 0; i < NumberOfLanes; i++)
 {
   StringReturn += " ," + String(LaneCount[i]);
 }
 return StringReturn;
}

void loop()
{
    val = digitalRead(ProxSensor);
    if (val == 0)
    {
        ProxSensorCleared = true;
    }
    
    if (val != 0 && ProxSensorCleared) 
    {
        ProxSensorCleared = false;
        Cycle++;
        delay(PhotoEyeDelay);
        for (int i = 0; i < NumberOfLanes; i++)
        {
            if (digitalRead(LaneSensor[i]))
            {
                LaneCount[i] += 1;
            }
        }
        Particle.publish("Pear_Counter", PublishLaneCount());
    }
}