Back to Parent

/*
 * Project Thermal-Compare
 * Description:
 * Author: Nicholas Stone
 * Date: 1/29/2017
 * Version: 4
 */

#include "OneWire.h"
#include "spark-dallas-temperature.h"

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "glcdfont.h"

//instances
OneWire oneWire(D2);
DallasTemperature dallas(&oneWire);

//vars
int tempF = 0;
int MasterTempF = 70;
int MaxTempDiff = 5;
//Sample Rate est (Loop Rate)
int sampleRate = 1000;
//Burst Rate of published messages (15 minutes)
int burstRate = 900000;
int timeSincePublish= 0;

//motion read
int motionPin = D5;
int motionValue = 0;
int motionState = 0;
int motionRate = 900000;
int timeSinceMotion = 0;

//LED
int ledPin = D4;

//Fan / Servo
int servoPin = D3;
Servo fanServo;

//LED Array
Adafruit_8x8matrix ledMatrix = Adafruit_8x8matrix();





void setup() {

  Particle.variable("tempF", &tempF, INT);
  Particle.variable("motionStatus",&motionState,INT);

  //negates burstRate at startup
  timeSincePublish = burstRate;

  pinMode(motionPin, INPUT);
  pinMode(ledPin, OUTPUT);

  fanServo.attach(servoPin);

  //start interface
  ledMatrix.begin(0x70);
  ledMatrix.setCursor(0,0);


  //start library
  dallas.begin();

}

void loop() {

  //check motion, run low if no motion after set time
  motionValue = digitalRead(motionPin);
  if(motionValue == 1) {
    motionState = 1;
    timeSinceMotion = 0;
    digitalWrite(ledPin, HIGH);
  } else{if(timeSinceMotion>=motionRate) {
    motionState = 0;
    digitalWrite(ledPin, LOW);
    } else {
      timeSinceMotion = timeSinceMotion + sampleRate;
      digitalWrite(ledPin, LOW);
    }
  }



  //request conversion
  dallas.requestTemperatures();

  //get temperature in Celsius and convert to Fahrenheit
  tempF = (int)DallasTemperature::toFahrenheit(dallas.getTempCByIndex(0));

  ledMatrix.fillRect(2,2, 4,4, LED_ON);
  ledMatrix.writeDisplay();

//check if difference exceeds configuration and enough time has passed since the last publication
if(abs(tempF - MasterTempF)>MaxTempDiff && timeSincePublish>=burstRate && motionState == 1
)
  {
    //Particle.publish("TempPhoneCall","true");
    if(tempF<MasterTempF) {
    Particle.publish("Colder", "true");
    } else {
    Particle.publish("Warmer", "true");
    }
    // reset burst rate time counter
    timeSincePublish = 0;
    //
    ledMatrix.clear();
    ledMatrix.print("NO");
    ledMatrix.writeDisplay();

    //run fan for 10 seconds
    fanServo.write(180);
    delay(10000);
    fanServo.write(91);
    }

  timeSincePublish = timeSincePublish + sampleRate;
  delay(sampleRate);



}
Click to Expand

Content Rating

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

0