Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>
// This #include statement was automatically added by the Particle IDE.
#include <SharpIR.h>
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include <math.h>

#define PIXEL_PIN D3
#define PIXEL_COUNT 41
#define PIXEL_TYPE WS2812B
#define LEDCOUNT 41
// air condition
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
// light sensor 
int temt6000Pin = A0;
//soil sensor
int soil_moisture_pin = A4;


Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

float light=0;
float humidity = 0;
float temp_F = 0;
float moisture = 0;
float distance_value = 0.0;
bool seeingPerson = false;
long lastCount = millis();
int passCount = 0;
int totalCount = 0;
int IR = A1;
int fade=0;
int max_delay_ON = 1000;
int delayON = 10;
void setup() {
    
    pinMode(IR, INPUT);
    Serial.begin(9600);
    strip.begin();
    strip.show();
    Serial.begin(9600);
    Serial.println("ready");
    pinMode(soil_moisture_pin, INPUT); 
    pinMode(temt6000Pin, INPUT); 
    dht.begin();
}

void loop() {
    light = ambient_light();
    humidity = air_condition2();
    temp_F = air_condition1();
    moisture = soil_moisture();
    bool x = person_detected();
    Particle.publish("Moisture", String(moisture));
    Particle.publish("Light", String(light));
    Particle.publish("Total people crossing this point", String(totalCount));
    float light_mod = light/1024;
    float temp_F_mod = (temp_F-20)/70;
    float humidity_mod = humidity/100;
    float moisture_mod = 1-(moisture/100);
    float y;
    y = (light_mod*0.40) + (moisture_mod*0.55) + (temp_F_mod*0.025) + (humidity_mod*0.025);
    fade = map(y, 0.0, 1.0, 0.0, 255.0);
    fade = int(fade);
    seeingPerson = false;
    if (x == true){
        //fade = max(passCount * 30, 0);
        delayON = delayON + 10;
        if (delayON > max_delay_ON){
            delayON = max_delay_ON;
        }
    }    // ON
        for(int j = 0; j<LEDCOUNT;j++){
            strip.setPixelColor(j, 255 - fade, fade, 0);
        }
        strip.show();
        delay(delayON);
        //OFF
        for(int j = 0; j<LEDCOUNT;j++){
                strip.setPixelColor(j, 0,0,0);
            }
        strip.show();
        delay(delayON);
    
}

bool person_detected(){
    // Determine IR sensor distance
    float volts = float(analogRead(IR))*0.0048828125; 
    int distance = 13 * pow(volts, -1);
    distance_value = (distance_value * 0.7) + (distance * 0.3);
    if (distance_value <= 5){
        if(!seeingPerson && (millis() - lastCount > 2000)){
            lastCount = millis();
            if (passCount < 40) {
                totalCount++;
                passCount++;
                }
            else{
                totalCount++;
            }
            }
            seeingPerson = true;
        return true;
    }
    else{
        return false;
    }
}

float ambient_light(){
    float light;
    // Read sensor
    int light_value = analogRead(temt6000Pin);
    light = light_value * 0.0976;// percentage calculation
    
    return light;
}
float air_condition1(){
    float temp_F = dht.getTempFarenheit();
    return temp_F;
}
float air_condition2(){
    float humidity = dht.getHumidity();
    return humidity;
}

float soil_moisture(){
    float moisture_percentage;
    int sensor_analog;
    // Read sensor
    sensor_analog = analogRead(soil_moisture_pin);
    moisture_percentage = (100 - ( (sensor_analog/4095.00) * 100 ));
    return moisture_percentage;
}
Click to Expand

Content Rating

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

0