You are not authorized to access this page. ×

Back to Parent

Code for this project.
// By Jesse Park
// 1/28/16
// This code makes the stove smarter

// Set the variables
int hallpin = A0; // hall effect sensor pin
int ledpin = D0; // led pin
int beeppin = D1; // beeper pin
int buttonpin = D2; // button pin
int hallEffectValue = 0; // value of the hall effect sensor
int ledbrightness = 0; // control the led brightness
int long timer = 0; // control time
int beep = 0; // loudness of the beeper
int duration = 0; // records how long the stove is on
int pushbuttonstate = 0; // shows the state of the push button
int ifttHallEffectValue = 0; // special variable to work with IFTT

// Set up
void setup() {
  Serial.begin(9600); // for easy debugging
  pinMode(ledpin, OUTPUT); // set ledpin as an OUTPUT
  pinMode(buttonpin, INPUT_PULLUP); // set buttonpin as an INPUT_PULLUP
  pinMode(beeppin,OUTPUT); // set beeppin as an OUTPUT
}

// Set the loop
void loop() {
  hallEffectValue = analogRead(hallpin); // set the hall effect sensor
  pushbuttonstate = digitalRead(buttonpin); // set the value of the pushbuttonstate

  // Activates when the stove is on
  if (hallEffectValue > 100){

    // Used for publishing data to IFTT. Checks if the hall effect value has changed
    if (ifttHallEffectValue == 0){
      ifttHallEffectValue = 1; // set the hall effect value to 1
      Particle.publish("onStatus", "ON"); //sends an event that the stove is on
    }
    ifttHallEffectValue = 1; // set the hall effect value to 1

    // set time function
    if( timer == 0 ){
      timer = millis();
    }

    // Changes the brightness depending on time
    if (ledbrightness < 255){
      ledbrightness = ledbrightness + 1;
    }
    else ledbrightness = 255;
    analogWrite(ledpin,ledbrightness); //record the brightness

    // Checks if 10 min has passed. If so, beeps untill pushbutton is pressed
    if (timer + 600000 < millis() ) {
      analogWrite(beeppin, 1);
      duration = duration + 1;
      if (pushbuttonstate == LOW) {
        timer = 0; //restart time
        analogWrite(beeppin, 0);
        duration = 0;
        Particle.publish("PushButton", "Delay"); //sends an event that the button is pressed
      }
    }
    else duration = duration + 1;
  }

  // If the stove is not on, the stove is off.
  else {

    // Used for publishing data to IFTT. Checks if the hall effect value has changed
    if (ifttHallEffectValue == 1){
      ifttHallEffectValue = 0;
      Particle.publish("offStatus", "OFF"); //sends an event that the stove is off
    }
    ifttHallEffectValue = 0;
    timer = 0;   //restart time
    ledbrightness = 0;
    duration = 0;
    analogWrite(ledpin,ledbrightness); //turn off the led
    analogWrite(beeppin, 0);
  }

  // Used for debugging
  Serial.print( "HallEffect " );
  Serial.println( hallEffectValue);
  Serial.print( "LED " );
  Serial.println( ledbrightness );
  Serial.print( "Timer " );
  Serial.println( timer );
  Serial.print( "Duration " );
  Serial.println( duration );
  delay(1000);
}
Click to Expand

Content Rating

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

0