Back to Parent

int speakerPin = D8;

int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};

int noteDurations[] = {4,8,8,4,4,4,4,4 };

int ledPin1 = D5;

int ledPin2 = D4;

int photoCellPin = A0;

int photoCellReading = 0;

int last_published = -1;

void setup() {

pinMode( speakerPin, OUTPUT );

pinMode(ledPin1, OUTPUT);

pinMode(ledPin2, OUTPUT);

Particle.variable("light", &photoCellReading, INT);

}

void loop() {

photoCellReading = analogRead(photoCellPin);

if(photoCellReading <2000 ){

digitalWrite(ledPin1, HIGH);

delay(100);

digitalWrite(ledPin1, LOW);

delay(100);

digitalWrite(ledPin2, HIGH);

delay(100);

digitalWrite(ledPin2, LOW);

delay(100);

digitalWrite(ledPin1, HIGH);

delay(100);

digitalWrite(ledPin1, LOW);

delay(100);

digitalWrite(ledPin2, HIGH);

delay(100);

digitalWrite(ledPin2, LOW);

delay(100);

digitalWrite(ledPin1, HIGH);

delay(100);

digitalWrite(ledPin1, LOW);

delay(100);

digitalWrite(ledPin2, HIGH);

delay(100);

digitalWrite(ledPin2, LOW);

delay(100);

digitalWrite(ledPin1, HIGH);

delay(100);

digitalWrite(ledPin1, LOW);

delay(100);

digitalWrite(ledPin2, HIGH);

delay(100);

digitalWrite(ledPin2, LOW);

delay(100);

digitalWrite(ledPin1, HIGH);

delay(100);

digitalWrite(ledPin1, LOW);

delay(100);

digitalWrite(ledPin2, HIGH);

delay(100);

digitalWrite(ledPin2, LOW);

delay(100);

playNotes();

}else{

digitalWrite(ledPin1, LOW );

digitalWrite(ledPin2, LOW );}

delay(2000);

}

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

}

}

void log_to_spreadsheet( ){

// check if 1 minute has elapsed

if( last_published + 60000 < millis() ){

Particle.publish( "log_to_spreadsheet", String( photoCellReading ) );

last_published = millis();

}

}
Click to Expand

Content Rating

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

0