// Define a pin that we'll place the FSR on
// Remember to add a 10K Ohm pull-down resistor too.
int fsrPin = A0;
// Create a variable to hold the FSR reading
int fsrReading = 0;
// Define a pin we'll place an LED on
int ledPin = D2;
// Create a variable to store the LED brightness.
int ledBrightness = 0;
int speakerPin = D3;
// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody1[] = {1318,1318,1318,1046,1318,1568,0,784,1046};
int melody2[] = {1967,1760,1568,1397,1318,1175,1046};
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {8,8,8,8,4,8,4,4 };
//below are for the soil moisture sensor
int rainPin = A3;
//int thresholdValue = 800;
int sensorValue = 0;
void setup()
{
// Set up the LED for output
pinMode(ledPin, OUTPUT);
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("force", &fsrReading, INT);
pinMode( speakerPin, OUTPUT );
pinMode(rainPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Particle.variable("rain", &sensorValue, INT);
}
void loop()
{
// Use analogRead to read the photo cell reading
// This gives us a value from 0 to 4095
fsrReading = analogRead(fsrPin);
if (fsrReading > 1500)
{
digitalWrite( ledPin, HIGH );
delay(500);
digitalWrite( ledPin, LOW );
delay(500);
playNotes1();
}
//int sensorValue = analogRead(rainPin);
sensorValue = analogRead(rainPin);
if(sensorValue < 2500){
digitalWrite(ledPin, HIGH);
playNotes2();
}else{
digitalWrite(ledPin, LOW);
}
// Map this value into the PWM range (0-255)
// and store as the led brightness
ledBrightness = map(fsrReading, 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 playNotes1()
{
// 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, melody1[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 playNotes2()
{
// 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, melody2[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!
You must login before you can post a comment. .