Back to Parent

// Define a pin that we'll place the photocell and temp pins on
// Remember to add a 10K Ohm pull-down resistor too (photocell only).
int photoCellPin = A0;
int tempPin = A1;

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

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

// Define pins we'll place an LED on
int ledPinR = D0;
int ledPinG = D1;

// Create a variable to store the LED brightnesses
int ledBrightnessR = 0;
int ledBrightnessG = 0;

int lightOutput;

void setup(){

  // Register a Spark variable here
  Spark.variable("temperature", &temperature, DOUBLE);
  Spark.variable("temperatureF", &temperatureF, DOUBLE);

  // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  Spark.variable("light", &photoCellReading, INT);

  // Connect the temperature sensor to A1 and configure it
  // to be an input
  pinMode(tempPin, INPUT);
  pinMode(lightOutput, INPUT);

  // Set up the LEDs for output
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinR, 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 Core 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.49) * 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);

  if(temperatureF > 65 && temperatureF < 80){
    digitalWrite(ledPinG, HIGH);
    digitalWrite(ledPinR, LOW);
  }
  else
  {
    digitalWrite(ledPinG, LOW);
    digitalWrite(ledPinR, HIGH);
  }

  // wait 1/10th of a second and then loop
  delay(50);

}
Click to Expand

Content Rating

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

0