Back to Parent

// Define the Pin the Temperature sensor is on
int tempPin = A2;

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

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

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

// Define a pin we'll place an LED on
int ledPin = D2;

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

void setup()
{
    // Register a Particle variable here
    Particle.variable("temperature", &temperature, DOUBLE);
    Particle.variable("temperatureF", &temperatureF, DOUBLE);
    
    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("light", &photoCellReading, INT);

    // Connect the temperature sensor to A0 and configure it
    // to be an input
    pinMode(tempPin, INPUT);
    // Set up the LED for output
    pinMode(ledPin, OUTPUT);
}

void loop()
{
    // Keep reading the sensor value so when we make an API
    // call to read its value, we have the latest one
    int reading = analogRead(tempPin);

    // The returned value from the device is going to be in the range from 0 to 4095
    // Calculate the voltage from the sensor reading
    double voltage = (reading * 3.3) / 4095.0;

    // Calculate the temperature and update our static variable
    temperature = (voltage - 0.5) * 100;

    // Now convert to Farenheight
    temperatureF = ((temperature * 9.0) / 5.0) + 32.0;
    
    // 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 as reverse mapping
    ledBrightness = map(photoCellReading, 0, 4095, 0, 255);
    ledBrightness = 255 - ledBrightness;

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

    // wait 1/10th of a second and then loop
    delay(100);
}
Click to Expand

Content Rating

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

0