Back to Parent

const int doorSensorA = A7;
const int doorSensorB = A0;
const int checkingLength = 500; //ms
const int delayAfterCount = 300; //ms
const int recheckTimes = 50;
const int checkingInterval = checkingLength/recheckTimes; //ms

int valveA = 0;
int valveB = 0;
int peopleCounts = 0; 
int sensorAValue = 0; //Analog measure of sensor1
int sensorBValue = 0; //Analog meausre of sensor2
int recheckCounter = 0;

void setup() {
    
    pinMode( doorSensorA, INPUT );
    pinMode( doorSensorB, INPUT );
    
    
    //To auto-calibrate the sensors. Averages 100 samples. Adds an extra 40 to weed out the peaks
    for(int i=0; i<100; i++){
        valveA += analogRead( doorSensorA );
        valveB += analogRead( doorSensorB );
    }
    
    
    valveA = valveA / 100 + 40;
    valveB = valveB / 100 + 40;
    
    //Variable to monitor performance
    Spark.variable( "peopleCounts", &peopleCounts, INT );
    Spark.variable( "sensorAValue", &sensorAValue, INT );
    Spark.variable( "sensorBValue", &sensorBValue, INT );
    Spark.variable( "valveA", &valveA, INT );
    Spark.variable( "valveB", &valveB, INT );
    Spark.function( "resetPeopleCounts", resetPeopleCounts );
    
}

void loop() {
    
    recheckCounter = 50;

    sensorAValue = analogRead( doorSensorA );
    sensorBValue = analogRead( doorSensorB );
    
    //if A is blocked and B is not
    if( analogRead( doorSensorA ) >= valveA && analogRead( doorSensorB ) < valveB ){
        
        while( recheckCounter >= 0 ){
            sensorAValue = analogRead( doorSensorA );
            sensorBValue = analogRead( doorSensorB );
            
            //if B is blocked
            if( analogRead( doorSensorB ) >= valveB ){
                
                peopleCounts++; //add people count
                delay( delayAfterCount );
                recheckCounter = 0; //break the while loop
            }
            
            delay( checkingInterval );
            recheckCounter--;
        }
        
    //if B is blocked and A is not
    }else if( analogRead( doorSensorA ) < valveA && analogRead( doorSensorB ) >= valveB ){
        
        while( recheckCounter >= 0 ){
            
            sensorAValue = analogRead( doorSensorA );
            sensorBValue = analogRead( doorSensorB );
            
            //if A is blocked
            if( analogRead( doorSensorA ) >= valveA ){
                if (peopleCounts<=0) //i added
                peopleCounts++; //i added
                else
                peopleCounts--; //minus people count
                delay( delayAfterCount );
                recheckCounter = 0; //break the while loop
            } 
            
            delay( checkingInterval );
            recheckCounter--;
        }
        
    }
}

int resetPeopleCounts(String whatever){
    peopleCounts = 0;
    return 1;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0