Countdown counter using IR TCRT5000

(Read 219 times)
Countdown counter using IR TCRT5000 on: January 17, 2021, 06:10:53 PM
Hello all,

I need help for a small project I am actually working on.

An Arduino got two analog IR sensors, these are working fine.
Also a 3digit 7 segments LED display is connected to the Arduino, also working fine.

Here is where is getting tricky, I need to detect an object passing in front of the both sensor, then decrease a counter on the display.
My problem is when an object pass the first sensor, the countdown start but not stop, going fast to zero, not to count -1.





Re: Countdown counter using IR TCRT5000 Reply #1 on: January 17, 2021, 08:53:08 PM
Hi you need to share the code, othewise I can't understand. But anyway, you need to increase/decrease the cunter on state chane of the sensor and not on the actual state. so you need to store the previous state of the sensor. So for example if the sensor is low and then you detect it is high, then you decrease your counter. Otherwise the loop will be executed non-stop and very fast. Also, maybe add a delay... Share the code and more details...



Re: Countdown counter using IR TCRT5000 Reply #2 on: January 23, 2021, 06:13:34 PM
A highly modified NerfGun, including backpack for 1000 rounds, counter, speed controller, semi-auto/burst/auto shoot...


here is my code
Code: [Select]
//Library
#include "SevenSeg.h"

// parameters
SevenSeg disp (10, 6, 4, 2, 1, 9, 5); //Defines the segments A-G: SevenSeg(A, B, C, D, E, F, G);
const int numOfDigits = 3;      //number of digits
int digitPins [numOfDigits] = {11, 8, 7}; //CC(or CA) pins of segment

//Variables
int AmmoCount; // Ammo_current_count
int AmmoMax; // Ammo_full magazine
int Threshold;
int inLEDvar;
int inLEDvarState;
int inLEDvarLastState;
int outLEDvar;
int outLEDvarState;
int outLEDvarLastState;



void setup()
{
  pinMode (A0, INPUT); // in_LED_Sensor
  pinMode (A1,INPUT);  // out_LED_Sensor
  Threshold = 950;
  AmmoMax = 100;
  AmmoCount = AmmoMax;
  disp.setRefreshRate(150);
  inLEDvarLastState = 0;
  inLEDvarState = 0;
  outLEDvarLastState = 0;
  outLEDvarState = 0;
  disp.setDigitPins ( numOfDigits , digitPins );
  disp.setCommonAnode();
}

void loop()
{
  disp.write(AmmoCount);
  inLEDvar = analogRead (A0); // read in_LED_Sensor
  outLEDvar = analogRead (A1); // read out_LED_Sensor
  if (inLEDvar > Threshold)
  {
    inLEDvarState = 1;
    if (inLEDvarState != inLEDvarLastState)
    {
      if (AmmoCount == 0)
      {
        disp.write("000");
      }
      else
      {
        AmmoCount = AmmoCount - 1;
        inLEDvarState = 0;
      }
    }
    else
    {
      inLEDvar = 0;
      inLEDvarLastState = inLEDvarState;
    }
  }
}



Re: Countdown counter using IR TCRT5000 Reply #3 on: January 25, 2021, 09:53:47 AM
Hi. First of all, I would use digitalRead instaead of Analog Read and make sure the sensor will jump from 0V to 5V or close to that. Any way, I can see you don't use in the code the output led sensor. Why is that?
Any way, I would do something like this. (see code)
Code: [Select]
//Library
#include "SevenSeg.h"

// parameters
SevenSeg disp (10, 6, 4, 2, 1, 9, 5); //Defines the segments A-G: SevenSeg(A, B, C, D, E, F, G);
const int numOfDigits = 3;      //number of digits
int digitPins [numOfDigits] = {11, 8, 7}; //CC(or CA) pins of segment

//Variables
int AmmoCount; // Ammo_current_count
int AmmoMax; // Ammo_full magazine
int Threshold;
int inLEDvar;
bool inLEDvarState;
bool inLEDvarLastState;
int outLEDvar;
int outLEDvarState;
int outLEDvarLastState;



void setup()
{
  pinMode (A0, INPUT); // in_LED_Sensor
  pinMode (A1,INPUT);  // out_LED_Sensor
  Threshold = 950;
  AmmoMax = 100;
  AmmoCount = AmmoMax;
  disp.setRefreshRate(150);
  inLEDvarLastState = 0;
  inLEDvarState = 0;
  outLEDvarLastState = 0;
  outLEDvarState = 0;
  disp.setDigitPins ( numOfDigits , digitPins );
  disp.setCommonAnode();
}

void loop()
{
  disp.write(AmmoCount);
  inLEDvar = analogRead (A0); // read in_LED_Sensor
  outLEDvar = analogRead (A1); // read out_LED_Sensor

  if (inLEDvar > Threshold && !inLEDvarState)  //Dectect if sensor is HIGH and previous state is LOW (change detected)
  {
    inLEDvarState = true;     //Save new state to HIGH
    if (AmmoCount != 0)
      {
        AmmoCount = AmmoCount - 1;
      }
    else
      {               
        disp.write("000");
      }
  }
  //Then, when the sensor is back to LOW, we check that and change the previous value
  else if (inLEDvar < Threshold && inLEDvarState)
  {
    inLEDvarState = false;  //Previous value is back to LOW
  } 
 
}