Back to Parent

#include <Adafruit_NeoPixel.h>
#include <Arduino_LPS22HB.h>

#define PIXEL_PIN D11
#define PIXEL_COUNT 8
#define PIXEL_TYPE NEO_GRB + NEO_KHZ800
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

const int atnOptions[3] = {10,30,50}; //attention period options 
int atnPeriod = atnOptions[random(0, 2)]; //randomly select atnPeriod 
int duration = 0;
int r;
int g;
int b;
int color;

void setup() {
  Serial.begin(9600);

  if (!BARO.begin()) {
    Serial.println("Failed to initialize pressure sensor!");
    while (1);
  }
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  pinMode(D2, INPUT_PULLDOWN);   
}

void loop() {
    // read the sensor value
  float pressure = BARO.readPressure();

  // print the sensor value
  Serial.print("Pressure = ");
  Serial.print(pressure);
  Serial.println(" kPa");
  float temperature = BARO.readTemperature();
  Serial.print("temperature");
  Serial.print(temperature);
  Serial.println();
   
  color = setTempColor(temperature);
  r = floor(color/1000000);
  g = floor(color/1000%1000);
  b = floor(color%1000);  
  Serial.print("rgb: ");
  Serial.print(r);
  Serial.print(g);
  Serial.print(b);
  Serial.println();
  boolean val = digitalRead(D2);  

  if (val == 1){ // pressed doorbell 
    duration = 0;
    Serial.print("true");
    Serial.print(atnPeriod);
    Serial.println();
    //set to correct temp 
  }

  else{ // did not press 
    duration += 1;  
    if (duration <= int(0.5*atnPeriod)){ // if less than half of attention period  

      uint32_t neutral = strip.Color(r, g, b) ;
      for( int i = 0; i < strip.numPixels(); i++ ){
        strip.setPixelColor(i, neutral); // set a color 
      }
      strip.show();
      delay(100);

      Serial.println(atnPeriod);
      Serial.print("less than half; duration: ");
      Serial.print(duration);
      Serial.println();
    }
    
    else if (duration <= atnPeriod){ // more than half 
      // make light fade  
      Serial.print("within atn");
      Serial.println();
      FadeInOut(r, g, b); // blue

    }
    else { //late 
      //make light strobe
      strip.clear();
      Serial.print("late atn");
      Serial.println();
      
      Strobe(r, g, b, 10, 50, 1000);
    }
  }
  delay(1000);
}

void FadeInOut(byte red, byte green, byte blue){
  float r, g, b;
     
  for(int k = 0; k < 256; k=k+1) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    strip.show();
  }
     
  for(int k = 255; k >= 0; k=k-2) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    strip.show();
  }
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < PIXEL_COUNT; i++ ) {
    setPixel(i, red, green, blue);
  }
  strip.show();
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
}

void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
  for(int j = 0; j < StrobeCount; j++) {
    setAll(red,green,blue);
    strip.show();
    delay(FlashDelay);
    setAll(0,0,0);
    strip.show();
    delay(FlashDelay);
  }
 
 delay(EndPause);
}

int setTempColor(int temp){
  Serial.print("setting color");
  if (temp <= 0){
    return 0;
  }  
  else if (temp > 0 && temp <=10 ){
    return 158255246;
  }  
  else if (temp > 10 && temp <=24 ){
    return 249217091;
  }  
  else if (temp > 24 && temp <=25 ){
    return 246012238;
  }  
  else {
    return 255000000;
  }  
}
Click to Expand

Content Rating

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

0