Back to Parent

#include <libfixmath.h>
#include <spark-dallas-temperature.h>
#include <OneWire.h>

int sensorBend=A2;
int sensorLight=A4;
int ledRed=D2;
int ledBlue=D3;
int ledGreen=D4;
int sensorValueBend;
int sensorValueLight;

int ledAlarm=D7;


float tempC;

int BendBright;
int LightBright;


int speakerPin = D5;

// 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}; 

// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };

OneWire oneWire( D6 );

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature dallas(&oneWire);

// Create a variable that will store the temperature value
double temperature = 0.0;

void setup() {
    pinMode (ledRed, OUTPUT);
    pinMode (ledBlue, OUTPUT);
    pinMode (ledGreen, OUTPUT);
    
    pinMode (sensorBend, INPUT);
    pinMode (sensorLight, INPUT);
    
    pinMode( speakerPin, OUTPUT );
    pinMode(ledAlarm, OUTPUT);
     //Particle.variable ("fsr", sensorValue);
    
    Particle.variable("temperature", &temperature, DOUBLE);
    // setup the library
    dallas.begin();

}

void loop() {
    sensorValueBend = analogRead (sensorBend);
    sensorValueLight = analogRead (sensorLight);

    temp();
    setLight();
    setAlarm();
    log_to_spreadsheet();
    
    delay(2000);
    
}

void setColor (int red, int green, int blue){
    analogWrite(ledRed, red);
    analogWrite(ledBlue, blue);
    analogWrite(ledGreen, green);
}


// store the time when you last published
int last_published = -1;

void log_to_spreadsheet(  ){

  // check if 1 minute has elapsed
	if( last_published + 60000 < millis() ){
		Particle.publish( "log_to_spreadsheet", String( sensorValueBend  ) );
		Particle.publish( "log_to_spreadsheet", String( sensorValueLight  ) );
		Particle.publish( "log_to_spreadsheet", String( temperature  ) );
		last_published = millis();
	}

}

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 setAlarm (){
    
    if (sensorValueBend>1000 && ((temperature<15)||(temperature>30))) {
        playNotes();
    }//bend & extreme temperature, meaning too windy and cold/hot
        
    if (sensorValueBend>1000 && ((temperature>=15)&&(temperature<=30))) {
        digitalWrite(ledAlarm, HIGH);
        delay(500);
        digitalWrite(ledAlarm, LOW);
        delay(500);
    }//bend & moderate temperature, meaning too windy but warm
        
    if (sensorValueBend<1000 && ((temperature<15)||(temperature>30))) {
        digitalWrite (ledAlarm, HIGH);
        delay(1000);
        digitalWrite (ledAlarm, LOW);
        delay(1000);
    }//straight & extreme temperature, meaning not windy but cold/hot
        
    if (sensorValueBend<1000 && ((temperature>=15)&&(temperature<=30))) {
        digitalWrite (ledAlarm, LOW);
        delay(2000);
    }//straight & moderate temperature, meaning not windy and warm
}

void setLight(){
    if ((sensorValueLight>=0)&&(sensorValueLight<500))
        setColor(255,255,255);
    else if ((sensorValueLight>=500)&&(sensorValueLight<1500))
        setColor(0,255,255);
    else if((sensorValueLight>=1500)&&(sensorValueLight<2500))
        setColor(255,255,0);
    else if((sensorValueLight>=2500)&&(sensorValueLight<=4095))
        setColor(0,0,0);
}

void temp()
{
  // Request temperature conversion
  dallas.requestTemperatures();

  // get the temperature in Celcius
  tempC = dallas.getTempCByIndex(0);
  // convert to double
  temperature = (double)tempC;

  delay(5000);

}
Click to Expand

Content Rating

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

0