Back to Parent

//Define pins for temperature and force sensors
int fsrPin = A0;
int tempPin = A1;

// Create variables that store outputs from temperature and force sensors
double temperature = 0.0;
double temperatureF = 0.0;
int fsrReading = 0;

// Define a pin for output LED
int ledPin = D0;

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


void setup()
{
   // Create Spark cloud variables for sensor data
  Spark.variable("temperature", &temperature, DOUBLE);
  Spark.variable("temperatureF", &temperatureF, DOUBLE);
  Spark.variable("force", &fsrReading, INT);

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

  // Set up the LED for output
  pinMode(ledPin, OUTPUT);
}


void loop()
{
  // Use analogRead to read the force sensor reading
  // This outputs a value from 0 to 4095
  fsrReading = analogRead(fsrPin);

  //Store the reading from the temperature sensor
  //from this we can calculate actual temperature
  int reading = analogRead(tempPin);

  // The returned value 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;

  //Condition for sensor to turn on
  //temperature below zero and minimum force present
  if(temperatureF<32 && fsrReading>150)
  {
    // Map the temp sensor value into the PWM range (0-255)
    // and store as the led brightness
    ledBrightness = map(fsrReading, 0, 4095, 0, 255);

    // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
  }
  //If these conditions are not met then
  //the LED turns off
  else{
    ledBrightness = 0;
    analogWrite(ledPin, ledBrightness);
  }

  // wait 1/10 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