//Designing for IoT 10/31/2019
//Anna Krakora
//Creative Project 1, Internet of Plants
//Code outline from https://diotlabs.daraghbyrne.me/docs/working-with-sensors
//Imperial march code from https://gist.github.com/nicksort/4736535
//Initiate photoresistor components
int lightSensorPin = A0;
int lightSensorValue = 0;
int ledPinRed = D2;
int ledBrightnessRed = 0;
//Initiate force sensor components
int forceSensorPin = A1;
int forceSensorValue = 0;
int ledPinGreen = D3;
int ledBrightnessGreen = 0;
int forceval = 0;
//initiate speaker components
int speakerPin = D4;
// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {1908,1908,1908,0,1908,1908,1908,0,1908,1908,1908};
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {8,8,8,4,4,4,4,4,8,8,8};
//initialize beep counter
int counter = 0;
//initialize musical notes
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
//initiate pulishing to IFTTT
int last_published = -1;
void setup() {
//setup leds
pinMode ( ledPinRed, OUTPUT );
pinMode ( ledPinGreen, OUTPUT );
//setup sensors
pinMode( lightSensorPin, INPUT );
pinMode( forceSensorPin, INPUT );
//output sensor values to particle console
Particle.variable( "lightValue", &lightSensorValue, INT);
Particle.variable( "forceValue", &forceSensorValue, INT);
//setup Piezo
// pinMode( speakerPin, OUTPUT );
// playNotes();
// firstSection();
//Ready beep
readybeep();
}
void loop() {
//read sensors
lightSensorValue = analogRead ( lightSensorPin );
forceSensorValue = analogRead ( forceSensorPin );
//map sensor readings to led values
int lightBrightness = map (lightSensorValue, 0, 3000, 255, 0);
//int touchBrightness = map (forceSensorValue, 100, 2500, 0, 255);
//set photoresitor led values
ledBrightnessRed = lightBrightness;
//write led values to led pins
analogWrite( ledPinRed, ledBrightnessRed );
delay (500);
if (forceSensorValue >= 3000){
ledBrightnessRed = 0;
firstSection();
forceval = 6000;
}
else if (forceSensorValue >= 500 && forceSensorValue <= 2500){
//if force sensor is pressed, turn on green led
analogWrite( ledPinGreen, 250 );
//if force sensor is pressed, play a tone
playNotes();
analogWrite( ledPinGreen, 0);
ledBrightnessRed = 0;
forceval = 10;
}
//send light data to spreadsheet
Plant_status2();
forceSensorValue == 0;
analogWrite (ledPinRed, 0);
}
//function to play sound
void playNotes()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 11; 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);
}
}
//play a note
void beep(int note, int duration)
{
//Play tone on buzzerPin
tone(speakerPin, note, duration);
//Play different LED depending on value of 'counter'
if(counter % 2 == 0)
{
digitalWrite(ledPinGreen, HIGH);
delay(duration);
digitalWrite(ledPinGreen, LOW);
}else
{
digitalWrite(ledPinRed, HIGH);
delay(duration);
digitalWrite(ledPinRed, LOW);
}
}
//initialize beep
void readybeep(){
beep(a, 650);
}
// song1
void firstSection()
{
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
beep(eH, 500);
beep(eH, 500);
beep(eH, 500);
beep(fH, 350);
beep(cH, 150);
beep(gS, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
}
//Send to spreadsheet funtion
void Plant_status2(){
// check if 1 minute has elapsed
if (last_published + 60000 < millis() && forceval == 6000){
Particle.publish( "Plant_status2", String("The dark side is getting stronger. Be gentle with the force."));
last_published = millis();
}
else if (last_published + 60000 < millis() && forceval == 10 ){
Particle.publish( "Plant_status2", String("The dark side is warning you. SOS. Lighten up"));
last_published = millis();
}
else if
( last_published + 60000 < millis() && ledBrightnessRed <= 3000 ){
Particle.publish( "Plant_status2", String("Hey! Its too dark in here. Use the force to bring in more light"));
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!
You must login before you can post a comment. .