Final code
//codes for Speaker from: http://diotlabs.daraghbyrne.me/6-controlling-outputs/piezo/
//codes for Temperature Sensor from: http://diotlabs.daraghbyrne.me/3-working-with-sensors/DS18B20/
// 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;
int waterSensor = A0;
int water;
int waterHot = 100;
int waterCold = 97;
int ledPin = D2;
int speakerPin = D1;
// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };
void setup()
{
// Register a Particle variable here
Particle.variable("temperature", &temperature, DOUBLE);
Particle.variable("temperatureF", &temperatureF, DOUBLE);
Particle.variable("water", &water, INT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode( speakerPin, OUTPUT );
// setup the library
dallas.begin();
//check if water is at correct level
waterlevel();
tempCheck();
}
void loop()
{
}
void waterlevel(){
for ( int i = 0; i < 1;) {
water = analogRead(waterSensor);
if (water < 100){
Particle.publish("Turn-off-water");
playNotes();
i = 2;
}else{
i = 0;
delay(5000);
}
}
}
void tempCheck(){
for (int r = 0; r < 1;) {
// Request temperature conversion
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;
if (temperatureF < waterHot){
if (temperatureF > waterCold){
Particle.publish("Water-temp-is-good");
digitalWrite(ledPin, HIGH);
r = 2;
}else{
r = 0;
}
}else{
r = 0;
}
delay(5000);
}
}
void playNotes()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(speakerPin, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .