People Counter / Mbed 2 deprecated person_counter

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library

Revision:
18:7b1d65b007ab
Parent:
17:b8b68ff5e33a
Child:
19:c78aafa613b8
--- a/main.cpp	Sun Apr 26 17:56:57 2020 +0000
+++ b/main.cpp	Sun Apr 26 18:15:12 2020 +0000
@@ -41,6 +41,7 @@
 volatile int index = 0;
 volatile bool allZero = true;
 int state = 0;
+int non_zeros = 0;
 
 
 void sonar(int distance)
@@ -58,14 +59,22 @@
         index = 0;
     }
     
-    //check if all the values are zero
+    //check if more than one value is zero (noise cancellation)
     allZero = true;
-    for (int i = 0; i < 5; i++){
-        if (values[i] != 0)
+    for (int i = 0; i < 5; i++)     //Walk the list
+    {
+        if (values[i] != 0)         //If value is not zero
         {
-             allZero = false;
+             ++non_zeros;           //Incriment non_zero counter
+             //allZero = false;
+             if(non_zeros > 1)      //If there is more than one non zero
+            {
+                allZero = false;    //Change the allZero flag to false
+            }
         }
     }
+    non_zeros = 0;
+    
     //printf("Sonar Distance:\r\n %d", sdist);
 }