Back to Parent

#include <DHT.h>
#include <FastLED.h>

FASTLED_USING_NAMESPACE

// FastLED "100-lines-of-code" demo reel, showing just a few 
// of the kinds of animation patterns you can quickly and easily 

#define DATA_PIN    3
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS    16
CRGB leds[NUM_LEDS];

#define BRIGHTNESS          50
#define FRAMES_PER_SECOND  120

int ThermistorPin = 0;
const int LED = 2;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
double avg_temp = 0;
double sum_temp = 0;
double cur_temp = 0;
bool isOn = false;
long int t1;
long int t2;


// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain


#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(9600);
    Serial.println("HEY HEY I AM A MAGIC LAMP.\n");
  
    dht.begin();
    for (int i = 0; i < 6; i++) {
        sum_temp += getTemp("h");
        delay(1000);
    }
    avg_temp = sum_temp / 6;
    Serial.println("Avg Temp is: " + String(avg_temp) + "\n");

FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
    // Wait a few seconds between measurements.
    delay(2000);  

    double holding_sum = 0;
    double holding_avg = 0;

    Serial.print("Please hold me.\n");
    for (int i = 0; i < 5; i++) {
        cur_temp = getTemp("h");
        holding_sum += cur_temp;
        print_info(cur_temp);
        delay(1000);
    }
    Serial.print("Calculating avg.\n");
    holding_avg = holding_sum / 5;

    if (holding_avg < avg_temp * 1.2) {
        if (isOn) {
            t2 = millis();
            Serial.print("T2: " + String(t2) + "\n");
            Serial.print("T1: " + String(t1) + "\n");
            long time = t2 - t1;
            Serial.print("Time: " + String(t2 - t1) + "\n");
            delay(time);
            Serial.print("ligts is turning off!\n");
            fade_out();
            isOn = false;
        } else {
            Serial.print("hey ligts off!\n");
        }
    } else {
        Serial.print("yesyesyes ligts up!\n");
        if (!isOn) {
            isOn = true;
            fade_in ();
            t1 = millis();
            Serial.print("T1: " + String(t1) + "\n");
        } else {
            for ( int i = 0; i < NUM_LEDS; i++) {
                leds[i] = CHSV(0, 0, 255);
            }
            FastLED.show();
        }
        
    }
}

void fade_out () {
    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 220);
    }
    FastLED.show();
    Serial.print("I feel a little bit cold.\n");
    delay(60000);

    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 169);
    }
    FastLED.show();
    Serial.print("Time to warm me!.\n");
    delay(60000);

    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 105);
    }
    FastLED.show();
    Serial.print("Don't need me any more?.\n");
    delay(60000);

    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 50);
    }
    FastLED.show();
    Serial.print("I am freezing!!!\n");
    delay(60000);
    
    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 0);
    }
    FastLED.show();
}

void fade_in () {
    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 50);
    }
    FastLED.show();
    delay(300);
    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 100);
    }
    FastLED.show();
    delay(300);
    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 169);
    }
    FastLED.show();
    delay(300);
    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 220);
    }
    FastLED.show();
    delay(300);
    for ( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = CHSV(0, 0, 255);
    }
    FastLED.show();
}

void print_info(double temp) {
    Serial.print("Humidity: " + String(temp) + "\n");
    Serial.println("===========================\n");
}

float getTemp(String req) {

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up
    //  to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println("Failed to read from DHT sensor!");
        return 0.0;
    }
    // Compute heat index in Kelvin 
    float k = t + 273.15;
    if(req =="c"){
        return t;//return Cilsus
    }else if(req =="f"){
        return f;// return Fahrenheit
    }else if(req =="h"){
        return h;// return humidity
    }else if(req =="hif"){
        return hif;// return heat index in Fahrenheit
    }else if(req =="hic"){
        return hic;// return heat index in Cilsus
    }else if(req =="k"){
        return k;// return temprature in Kelvin
    }else{
        return 0.000;// if no reqest found, retun 0.000
    }
}
Click to Expand

Content Rating

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

0