Back to Parent

int redPin = A1;    // RED pin of the LED to PWM pin **A0**
int greenPin = D0;  // GREEN pin of the LED to PWM pin **D0**

int redValue = 255;
int greenValue = 255;
// Photo Resistor
int photoCellPin = A0;
// Store the sensor reading
int photoCellReading = 0;
// threshold for being covered
int threshold = 3850;
// hold a state to say if it's covered or not
bool isCovered = false;
// store the time value since it was first covered
long timeSinceFirstThreshold = 0 ;
const int speakerPin = D2;
// We'll set up an array with the notes we want to play
// change these values to make different songs!
// Length must equal the total number of notes and spaces
const int songLength = 5;
// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)
char notes[] = "cdfda"; // a space represents a rest
// Beats is an array of values for each note and rest.
// A "1" represents a quarter-note, 2 a half-note, etc.
// Don't forget that the rests (spaces) need a length as well.
int beats[] = {1,1,1,1,1};
// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.
int tempo = 150;


void setup()
{  // connect the photo cell reading to the cloud
  Particle.variable( "stove_on", &photoCellReading, INT );

  Serial.begin( 9600 );
  pinMode(speakerPin, OUTPUT);
  pinMode( redPin, OUTPUT);
  pinMode( greenPin, OUTPUT);
  analogWrite( redPin, redValue);
  analogWrite( greenPin, greenValue);
}

 void loop()
 {  // reading from the sensor
  photoCellReading = analogRead( photoCellPin );
  if (photoCellReading > threshold)
  {
    analogWrite(redPin, 0);
    analogWrite(greenPin, 255);
  }
 else {
   analogWrite(greenPin, 0);
 analogWrite(redPin, 255);
}

  // see if the sensor is covered
  if( photoCellReading > threshold ){
    if( timeSinceFirstThreshold == 0 ){
      timeSinceFirstThreshold = millis();
    }else if( timeSinceFirstThreshold+ 5000 < millis() ){
      isCovered = true;
      music();}
    }


  Serial.print( "Sensor reading is: " );
  Serial.println( photoCellReading );
  delay( 50 );
 }


int frequency(char note)
{

  int i;
  const int numNotes = 5;  // number of notes we're storing

  // The following arrays hold the note characters and their
  // corresponding frequencies. The last "C" note is uppercase
  // to separate it from the first lowercase "c". If you want to
  // add more notes, you'll need to use unique characters.

  // For the "char" (character) type, we put single characters
  // in single quotes.

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.

  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.
}

 void music ()
 {
 int i, duration;

 for (i = 0; i < songLength; i++) // step through the song arrays
 {
   duration = beats[i] * tempo;  // length of note/rest in ms

   if (notes[i] == ' ')          // is this a rest?
   {
     delay(duration);            // then pause for a moment
   }
   else                          // otherwise, play the note
   {
     tone(speakerPin, frequency(notes[i]), duration);
     delay(duration);            // wait for tone to finish
   }
   delay(tempo/10);}
   }
Click to Expand

Content Rating

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

0