Back to Parent

// This #include statement was automatically added by the Spark IDE.
#include "OneWire.h"

// This #include statement was automatically added by the Spark IDE.
#include "spark-dallas-temperature.h"

// -----------------
// Read temperature
// -----------------

// Data wire is plugged into port 0 on the Arduino
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(D0 );

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature dallas(&oneWire);

// Create a variable that will store the temperature value
double temperature = 0.0;
double temperatureF = 0.0;

long frameCount = 0;

//=============BUTTON======================
// We will be using D1 to control our LED
//int ledPin = D1;

// Our button wired to D0
int buttonPin = D6;

//============MOTOR=======================

/*int motorPin = D6;*/
const int motor1Pin = D1;
const int motor2Pin = D2;
const int enablePin = D3;
const int switchPin = D4;

bool windowClosed = true;

void setup()
{
//===========BUTTON ==========================
  // For input, we define the
  // pushbutton as an input-pullup
  // this uses an internal pullup resistor
  // to manage consistent reads from the device

  pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
  Serial.begin(9600);

  // We also want to use the LED

  //pinMode( ledPin , OUTPUT ); // sets pin as output

//============TEMPERATURE SENSOR=========================
  // Register a Particle variable here
  Particle.variable("temperature", &temperature, DOUBLE);
  Particle.variable("temperatureF", &temperatureF, DOUBLE);

  // setup the library
  dallas.begin();

//============MOTOR===============
  pinMode(switchPin, INPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);

  digitalWrite(enablePin, HIGH);
}

void loop()
{
  frameCount++;
  //digitalWrite(motorPin, HIGH);
//================BUTTON PIN===========
  // find out if the button is pushed
  // or not by reading from it.
  int buttonState = digitalRead( buttonPin );
  /*Serial.println(buttonState);*/
  int digitalSwitchState = digitalRead( switchPin );
  Serial.println(digitalSwitchState);

//=====Temperature sensor===========
  // Request temperature conversion
if(frameCount%100 == 0) {
  dallas.requestTemperatures();

  // get the temperature in Celcius
  float tempC = dallas.getTempCByIndex(0);
  // convert to double
  temperature = (double)tempC;

  // convert to Fahrenheit
  float tempF = DallasTemperature::toFahrenheit( tempC );
  // convert to double
  temperatureF = (double)tempF;
}
  /*delay(5000);*/

//============MOTOR==============
//Window is open when the button is pressed. It is closed when button is high
  if(buttonState == LOW) {
    windowClosed = false;
    Particle.publish("open");
  }
//run motor if temperature is between 65 and 75 and if the button is
  if ((temperatureF > 65) && (temperatureF < 75) && (windowClosed = true))
  {
    digitalSwitchState == HIGH;//turn on the digital switch
  }
  else {
    digitalSwitchState == LOW;//turn off the digital switch
  }
  if (digitalSwitchState == HIGH){//when digital switch is on, run motor
    digitalWrite(motor1Pin, HIGH);
    digitalWrite(motor2Pin, LOW);
  }
}


/*
The API request will look something like this:
GET /v1/devices/{DEVICE_ID}/temperature

# EXAMPLE REQUEST IN TERMINAL
# Device ID is 0123456789abcdef
# Your access token is 123412341234
curl -G https://api.particle.io/v1/devices/0123456789abcdef/temperature \
  -d access_token=123412341234
*/
Click to Expand

Content Rating

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

0