People Counter / Mbed 2 deprecated person_counter

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

Revision:
19:c78aafa613b8
Parent:
18:7b1d65b007ab
Child:
20:1dc516a6a941
--- a/main.cpp	Sun Apr 26 18:15:12 2020 +0000
+++ b/main.cpp	Sun Apr 26 19:07:20 2020 +0000
@@ -40,6 +40,8 @@
 volatile int values[5] = {0,0,0,0,0};
 volatile int index = 0;
 volatile bool allZero = true;
+volatile bool movingTowards = false;
+volatile bool movingAway = false;
 int state = 0;
 int non_zeros = 0;
 
@@ -61,6 +63,8 @@
     
     //check if more than one value is zero (noise cancellation)
     allZero = true;
+    movingTowards = false;//NEW CODE
+    movingAway = false;// NEW CODE
     for (int i = 0; i < 5; i++)     //Walk the list
     {
         if (values[i] != 0)         //If value is not zero
@@ -72,6 +76,16 @@
                 allZero = false;    //Change the allZero flag to false
             }
         }
+        //NEW CODE
+        if(values[i] < values[(i+1)%5] && values[(i+1)%5] < values[(i+2)%5])
+        {
+            movingTowards = true;
+        }
+        if (values[i] > values[(i+1)%5] && values[(i+1)%5] > values[(i+2)%5])
+        {
+            movingAway = true;
+        }
+        //
     }
     non_zeros = 0;
     
@@ -123,7 +137,7 @@
 // Start song and return once playing starts
     //mySpeaker.PlaySong(note1,duration);
     while(1) {
-        if (counter > last_counter) {
+        if (counter != last_counter) {//CHANGE > to !=
             switch (musicChoice) {
                 case 0:
                     printf("Beep\r\n");
@@ -267,14 +281,27 @@
         
         //this 'state machine' should make sure that individuals are only counted once
         if ((state != 1) && !allZero){
-           state = 1;
-           //do logic to figure out direction
+           //state = 1;
+           //do logic to figure out direction  remove state = 1 above and counter++ below
+           //NEW CODE
+                if(movingTowards)
+                {
+                    ++counter;
+                    state = 1;
+                }
+                if(movingAway)
+                {
+                    --counter;
+                    state = 1;
+                }
+           //
            //right now just adding to main counter
-           counter++;
+           //counter++;
         }
         if (state == 1 && allZero){
             state = 0;
         }
+        printf("State %d\r\n", state);
         //MyLED = !MyLED;
         Thread::wait(100);
     }