Back to Parent

Final code
//Sources:
  //switchPin - http://diotlabs.daraghbyrne.me/5-getting-input/switches/
  //temperature - http://diotlabs.daraghbyrne.me/3-working-with-sensors/DS18B20/
  //speakerPin - http://diotlabs.daraghbyrne.me/6-controlling-outputs/piezo/

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

// 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(D1 );

// 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 speakerPin = D2;
int switchPin = D0;
// 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 Core variable here
  Particle.variable("temperature", &temperature, DOUBLE);
  Particle.variable("temperatureF", &temperatureF, DOUBLE);

  // setup the library
  dallas.begin();
  Serial.begin(9600);
  pinMode( switchPin , INPUT_PULLUP);
  pinMode( speakerPin, OUTPUT );
  playNotes();
}

void loop()
{
  int buttonState = digitalRead( switchPin );
  if( buttonState == LOW ) {
  // Request temperature conversion (traditional)
  dallas.requestTemperatures();

  sin( 23423 );

  // 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;

  // Print out
  Serial.print( "Temp in C = ");
  Serial.print( tempC );
  Serial.print( "\t\t F = ");
  Serial.println( tempF );

  delay(5000);
if ( tempF > 200) {
  playNotes();
  Serial.println("The water is boiling! ");
  Particle.publish("The water is boiling!", PRIVATE);
  }
  else{
    Serial.println("Water is not ready! ");
    Particle.publish("Water is not ready!", PRIVATE);
  }
}
else {}
}

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!

0