Back to Parent

// Define input pins
int force = A1;
int light = A7;

int forcereading = 0;
int lightreading = 0;

// Define an outut pin
int ledPin = D1;

void setup()
{
  // Create a cloud variable of type integer
 Spark.variable("forcereading", &forcereading, INT);
 Spark.variable("lightreading", &lightreading, INT);

  // Set up the pins for input and output
  pinMode(ledPin, OUTPUT);
  pinMode(forcereading, INPUT);
  pinMode(lightreading, INPUT);
}

void loop()
{
  // take reading from force resistor and save it in 'forcereading'
  forcereading = analogRead(force);

  // Use analogRead to read the photo cell reading and save in 'lightreading'
  lightreading = analogRead(light);

  // if someone gets off the bed while it is still dark, switch on the night light
  if (forcereading<100 && lightreading<2000){
  //switch on night lights to guide him/her to bathroom
  digitalWrite(ledPin, HIGH);

}

  // if the pressure on the bed increases (person gets back on) and the room is still dark(its night),
  //then switch off the night light.
  else if(forcereading>1000 && lightreading<2000){
    // delay to wait for person to decide if he is ready to sleep?
    delay(20000);
    //switch off night lights
    digitalWrite(ledPin, LOW);
  }
// if it is day-time, switch off the light (in case it is on for some reason)
  else
  {
  digitalWrite(ledPin, LOW);
  }

}
Click to Expand

Content Rating

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

0