Arduino counter need help please

Hello, thank you for taking time to read my issue and help me out a bit.

I am programming two ultrasonic sensors (HC-SR04) as a counter for occupancy in a room. However the counter is very inconsistent and sometimes marks the occupancy and sometimes it does not. I have worked off another project (Room Occupancy Counter - Hackster.io) for reference; i do not use the display.

I mainly have trouble figuring out what is wrong with the counter function inside loop. is there something im missing. Like previously stated the counter works at times but then it does not mark anything. I ran test on each individual sensor and they both are working fine, im sure it is just the code.

[int maxPeople = 5; // maximum number of people allowed before the alarm goes off
int sensitivity = 5; //lower values will make it more sensitive and higher values will make it less sensitive
//---------------------------------------------------



int currentPeople = 0;

int buzzer = 8;



int sensor1[] = {4,5};
int sensor2[]= {6,7};
int sensor1Initial;
int sensor2Initial;

String sequence = "";

int timeoutCounter = 0;

void setup() {
  //Setup code
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);
//  tm.init();
//  tm.set(2);

  delay(500);
  sensor1Initial = measureDistance(sensor1);
  sensor2Initial = measureDistance(sensor2);
}

void loop() {
  //Read ultrasonic sensors
  int sensor1Val = measureDistance(sensor1);
  int sensor2Val = measureDistance(sensor2);
  
  //Process the data
  if(sensor1Val < sensor1Initial - 30 && sequence.charAt(0) != '1'){
    sequence += "1";
  }else if(sensor2Val < sensor2Initial - 30 && sequence.charAt(0) != '2'){
    sequence += "2";
  }
  
  if(sequence.equals("12")){
    currentPeople++;  
    sequence="";
    delay(550);
  }else if(sequence.equals("22") && currentPeople > 0){
    currentPeople--;  
    sequence="";
    delay(550);
  }

  //Resets the sequence if it is invalid or timeouts
  if(sequence.length() > 2 || sequence.equals("11") || sequence.equals("22") || timeoutCounter > 200){
    sequence="";  
  }

  if(sequence.length() == 1){ //
    timeoutCounter++;
  }else{
    timeoutCounter=0;
  }

  //Print values to serial
  Serial.print("Seq: ");
  Serial.print(sequence);
  Serial.print(" S1: ");
  Serial.print(sensor1Val);
  Serial.print(" S2: ");
  Serial.print(sensor2Val);
  Serial.print("currentPeople=");
  Serial.println(currentPeople);
  
//  //Display current people count on 4-digit display
//  tm.display(3, currentPeople % 10);
//  int pos2 = currentPeople / 10;
//  tm.display(2, pos2 % 10);
//  int pos1 = pos2 / 10;
//  tm.display(1, pos1 % 10);
//  int pos0 = pos1 / 10;
//  tm.display(0, pos0 % 10);

  //If the number of people is too high, trigger the buzzer
  if(currentPeople > maxPeople){
    tone(buzzer, 1700);  
  }else{
    noTone(buzzer);  
  }
}

//Returns the distance of the ultrasonic sensor that is passed in
//a[0] = echo, a[1] = trig
int measureDistance(int a[]) {
  pinMode(a[1], OUTPUT);
  digitalWrite(a[1], LOW);
  delayMicroseconds(2);
  digitalWrite(a[1], HIGH);
  delayMicroseconds(10);
  digitalWrite(a[1], LOW);
  pinMode(a[0], INPUT);
  long duration = pulseIn(a[0], HIGH, 100000);
  return duration / 29 / 2;
}]

Thank you for looking at my issue hope you can help.

Try to make sure that you don't ping too frequently, like no more than about 25Hz aggregate.

Please remember to use code tags when posting code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

how can i delete post, no option is given when i try to edit post? i re posted question with correct format NOT to waste people time.

The pencil icon allows you to edit posts

1 Like

okay well i was able to figure out that i need get the TM1637 4 digit display readings into serial monitor. I just dont know how to transfer the code from
//Display current people count on 4-digit display // tm.display(3, currentPeople % 10); // int pos2 = currentPeople / 10; // tm.display(2, pos2 % 10); // int pos1 = pos2 / 10; // tm.display(1, pos1 % 10); // int pos0 = pos1 / 10; // tm.display(0, pos0 % 10);

to show as serial.print(currentPeople), i just dont know how.