Back to Parent

// Define a pin that we'll place the FSR on
// Remember to add a 10K Ohm pull-down resistor too.
int ledRedPin = D5;

// Create a variable to store the LED brightness
int ledRedBrightness = 0;

// Define a pin we'll place an LED on
int ledGreenPin = D2;

// Create a variable to store the LED brightness.
int ledGreenBrightness = 0;

//Sound is Piezo Sensor 
int speakerPin = D4;

//Define a pin that will place tempPin on 
int tempPin = A4;

double tempReading =0; 
double temperatureF =0; 

// 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()
{
  // Set up the LED and spearPin for output
  pinMode(ledRedPin, OUTPUT);
  pinMode(ledGreenPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(tempPin, INPUT);
  

  // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  Particle.variable("temperature", tempReading);
  Particle.variable("tempF", temperatureF);
  
}

void loop() {
// Use analogRead to read from the sensor
  // This gives us a value from 0 to 4095

  int reading = analogRead(tempPin);

  // The returned value from the device is going to be in the range from 0 to 4095
  // Calculate the voltage from the sensor reading
  double voltage = (reading * 3.3) / 4095.0;

  // Calculate the temperature and update our static variable
  tempReading = (voltage - 0.5) * 100;

  temperatureF = ((tempReading * 9.0) / 5.0) + 32.0;

// Any point below 49 °F: Red LED light is on, sounds is on, IFTTT app begins to work 
// 50-59 °F: Red LED light is on
// 60-75 °F: No light and sounds 
// Above 76 °F: Green light is on 
  if( temperatureF < 49.0){

    analogWrite(ledRedPin, 255 );
    playNotes();
    
  }else if( temperatureF < 70.0){

    analogWrite(ledRedPin, 127);

  }else{

    analogWrite(ledRedPin, 0);

  }
  
  
  if( temperatureF > 76.0){
      digitalWrite(ledGreenPin, HIGH);
  }else{
      digitalWrite(ledGreenPin, LOW);
      
  }

//   // Map this value into the PWM range (0-255)
//   // and store as the led brightness
//   ledRedBrightness = map(reading, 0, 4095, 0, 255);
//   ledGreenBrightness = map(reading, 0, 4095, 0, 255);

//   // fade the LED to the desired brightness
//   analogWrite(ledPin, ledBrightness);



  // wait 1/10th of a second and then loop
  delay(100);
  
}


  
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