Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <clickButton.h>
#include <time.h>

// Define a pin that we'll place the photo cell on
// Remember to add a 10K Ohm pull-down resistor too.
int photoCellPin = A0;

// Create a variable to hold the light reading
int photoCellReading = 0;

// Define a pin we'll place an LED on
int ledPin = D2;
// Define a pin for the button
int buttonPin = D3;

// Create a variable to store the LED brightness.
int ledBrightness = 0;


//tracking time
long int startTime;

long int endTime;

long int timeDiff;

int timerRunning = 0;


void setup() {
 Particle.variable("Brightness", ledBrightness);
 
   // Set up the LED for output
  pinMode(ledPin, OUTPUT);
  
  analogWrite(ledPin, 0);

  // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  
  pinMode(buttonPin, INPUT_PULLUP);
//  Serial.begin(9600);
}

void loop(){
    
    // Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
  photoCellReading = analogRead(photoCellPin);

  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  ledBrightness = map(photoCellReading, 3971, 4025, 255, 0);

  // fade the LED to the desired brightness
  analogWrite(ledPin, ledBrightness);
  

if(timerRunning == 0 && digitalRead(buttonPin) == LOW){
    
    //digitalWriteFast(ledPin, HIGH);
    startTime = Time.now();
    timerRunning = 1;
    delay(300);
    
}
else if(timerRunning == 1 && digitalRead(buttonPin) == LOW){
//digitalWriteFast(ledPin, LOW);
    endTime = Time.now();
    timeDiff = endTime - startTime;
    Particle.publish("Total Reading time", Time.format(timeDiff,"%X"));
    timerRunning = 0;
    delay(300);
}
else{
}

}
Click to Expand

Content Rating

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

0