Multiple Digital inputs

The function should be more like this:

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

I added the digitalRead in the String and it is only triggering once, I have triggered it everytime I trigger the prox so the prox value and photoeye should be equal.

The point is, the way you are doing this the trigger has to happen at the exact time your are calling PublishLaneCount(). You are publishing whether there currently is a trigger active when you publish or not.
I guess this is not what you want.

Do you not rather want to count the triggers into your array LaneCount[] and then after some time collecting data publish the content of that very array - instead of the current pin states?

BTW, in C/C++ there is a neater way to write this

  LaneCount[i] = LaneCount[i] + 1;

You’d just write

  LaneCount[i]++;    // post increment (in an expression the current value is evaluated first and incremented after) 
  // or 
  ++LaneCount[i];    // pre increment (in an expression the value is incremented first and then evaluated)
  // or
  LaneCoune[i] += 1; // this is handy when incrementing for other values than 1 
                     //   just replace the 1 with whatever you want - even a variable

So this worked to give me the correct count

    //photoelectric counter

    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;
    }

    void loop()
    {
      val = digitalRead(ProxSensor);
      if (val == 0)
      {
        CycleSensorDebounce = 0;
      }
      else
      {
        if (5 >= CycleSensorDebounce);
        {
          CycleSensorDebounce++;
          if (5 == CycleSensorDebounce)
          {
            Cycle++;
            for (int i = 0; i < NumberOfLanes; i++)
            {
              if (digitalRead(LaneSensor[i]))
              {
                LaneCount[i] = LaneCount[i] + 1;
              }
            }
            if (Cycle%5 == 0)
            {
              Particle.publish("fruit_detect", PublishLaneCount()); //publish event and counter
            }
          }
        }
      }
    }
1 Like

The void loop() {} really moves or cycles, zoom zoom… I have a similar application where I use a latch bit for the LOW or HIGH pin state which triggers a Count of 1 to keep the loop’s speed from counting each cycle… Once the input pin returns to the non-trigger state, I reset the latch bit… In my app, I also use an onDelay or Debounce timer using millis() when I compare an analog input to a set point or to keep a level switch from triggering while bouncing on/off. @dtuttoil: Just curious, are the Photos discretes?

These are the Photoelectric sensors I’m using.

http://www.ifm.com/products/us/ds/O6T305.htm

1 Like

So now that I have this code working correctly, I’m wondering if anyone has suggestions on the best platform to use to display this data. For testing we’ll be installing these control boxes on 6 machines. I’ll need the ability to reset the photons remotely and to monitor data. I was looking at the Losant platform.

I would talk to @RWB. He is the resident expert on Losant and has written a few tips on how to implement it.

Also see here.