Abhishek
Created May 31, 2018

Wake Up

This project tries to help people wake up when the sun comes up. This is necessary for a healthy and happy life.

Wake Up

Things used in this project

Story

Read more

Code

main.ino

Arduino
int boardLed = D7; // This is the LED that is already on your device.
// On the Core, it's the LED in the upper right hand corner.
// On the Photon, it's next to the D7 pin.

int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
float lowValue, highValue, intactValue, beamThreshold, brokenValue;

void setup() {
  // This part is mostly the same:
  pinMode(boardLed,OUTPUT); // Our LED pin is output (lighting up the LED)

  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
 delay(2000);
  Serial.begin(9600);
  // Then, the D7 LED will go off and the LED will turn on.
  digitalWrite(boardLed,LOW);
  
  delay(500);

  // Now we'll take some readings...
  int on_1 = analogRead(photoresistor); // read photoresistor
  delay(200); // wait 200 milliseconds
  int on_2 = analogRead(photoresistor); // read photoresistor
  delay(300); // wait 300 milliseconds

  // Now flash to let us know that you've taken the readings...
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);

  // Now the D7 LED will go on to tell you to remove your hand...
  digitalWrite(boardLed,HIGH);
  delay(500);

  // ...And we will take two more readings.
  int off_1 = analogRead(photoresistor); // read photoresistor
  delay(200); // wait 200 milliseconds
  int off_2 = analogRead(photoresistor); // read photoresistor
  delay(300); // wait 1 second

  // Now flash the D7 LED on and off three times to let us know that we're ready to go!
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);


  // Now we average the "on" and "off" values to get an idea of what the resistance will be when the LED is on and off
  lowValue = (on_1+on_2)/2;
  highValue = (off_1+off_2)/2;

  // Let's also calculate the value between ledOn and ledOff, above which we will assume the led is on and below which we assume the led is off.
  beamThreshold = (intactValue+brokenValue)/2;

}


// Now for the loop.

void loop() {
  Particle.publish("LightStatus",String(analogRead(photoresistor)),60,PRIVATE);
  Serial.printlnf("Observation %d", analogRead(photoresistor));

}

Credits

Abhishek

Abhishek

3 projects • 8 followers

Comments