int speakerPin = D2;
// 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};
//int melody[] = {1915,1700,1519,1700,1915,1915,1915,0,1700,1700,1700,0,1915,1915,1915};
int melodyLow[] = {1915,1700,1519,1300,1300};
int melodyHigh[] = {1300,1519,1700,1915,1915};
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {8,8,8,8,4};
// Define a pin that we'll place the FSR on and variable for reading
int fsrPin = A0;
int fsrReading = 0;
// define moist sensor and its value
int moistPin = A5;
int moistVal = 0;
int isMoist = false;
int wasMoist = isMoist;
int ledPinRed = D0;
int ledPinBlue = D5;
void setup() {
pinMode( speakerPin, OUTPUT );
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinBlue, OUTPUT);
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("force", fsrReading);
Particle.variable( "soil", moistVal );
}
void loop() {
// Use analogRead to read the photo cell reading
// This gives us a value from 0 to 4095
fsrReading = analogRead(fsrPin);
// read and print moist value (between 0 and 4095)
moistVal = analogRead(moistPin);
isMoist = moistVal>300;
if (wasMoist != isMoist) {
if ( isMoist ) {
digitalWrite(ledPinBlue, HIGH);
digitalWrite(ledPinRed, LOW);
playNotes(melodyHigh);
delay(1500);
} else {
digitalWrite(ledPinBlue, LOW);
digitalWrite(ledPinRed, HIGH);
playNotes(melodyLow);
}
wasMoist = isMoist;
}
if ( !isMoist ) {
playNotes(melodyLow);
digitalWrite(ledPinRed, HIGH);
if ( fsrReading>700 ) {
delay(8500);
}
delay(500);
}
ThingsBent();
digitalWrite(ledPinRed, LOW);
}
void playNotes(int melody[])
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 4; 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);
}
}
//ThingsBent
// store the time when you last published
int last_published = -1;
void ThingsBent( ){
// check if 1 minute has elapsed
if( last_published + 60000 < millis() ){
Particle.publish( "ThingsBent", String( fsrReading ) );
last_published = millis();
}
}
Susan Xu
(2019)
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. .