Back to Parent

int redPin = D0;
int greenPin = D1;
// Define a pin that we'll place the FSR on
// Remember to add a 10K Ohm pull-down resistor too.
int fsrPin = A0;
// Create a variable to hold the FSR reading
int fsrReading = 0;
// Create a variable to store the force number.
int force = 0;
// Define the Pin the Temperature sensor is on
int tempPin = A1;

char coffee[4];
// Create a variable that will store the temperature value
double temperature = 0.0;

void setup()
{

   Spark.variable("temperature", &temperature, DOUBLE);
   Spark.variable("coffeealarm", coffee, STRING);
   // Configure the pins to be outputs
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   // configure A1 to be temp input
   pinMode(tempPin, INPUT);
   // Initialize both the LEDs to be OFF
   digitalWrite(redPin, LOW);
   digitalWrite(greenPin, LOW);
}


void loop()
{
   //hold FSR for 3 seconds first then blink red LED for 3 seconds

  fsrReading = analogRead(fsrPin);

  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  force = map(fsrReading, 0, 4095, 0, 255);

  //wait 3 seconds
  delay( 3000 );
  //check force
  if(force > 5 )
  {
    strcpy(coffee, "good");
    turnoffLED( D0 );
    turnonLED( D1 );

  }
  else
  {
    strcpy(coffee, "add ");
    turnoffLED( D1 );
    turnonLED( D0 );
  }

  // read temp value and store it as reading
  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.5) * 100;

  blinkLED(D1,3); //blick 3 times to show the data is updated

  delay(100);
  
}


//turnon LED
void turnonLED(int pinNumber)
{

 digitalWrite( pinNumber, HIGH );

}

//turnoff LED
void turnoffLED(int pinNumber)
{

  digitalWrite(pinNumber, LOW);

}

//blink LED for specific times

void blinkLED( int pinNumber, int times )

{

 for( int i = 0 ; i < times ; i++ )

 {

 digitalWrite( pinNumber, HIGH );

 delay( 500 );

 digitalWrite( pinNumber, LOW );

 delay( 500 );

 }

}

void checkFSR()
{
  fsrReading = analogRead(fsrPin);

  // Map this value into the PWM range (0-255)
  // and store as force
  force = map(fsrReading, 0, 4095, 0, 255);

  //wait 3 seconds
  delay(3000);
  if(force > 1 )
  {
    turnoffLED(D0);
    turnonLED(D1);
 }
  else if(force < 1);
  {
    turnoffLED(D1);
    turnonLED(D0);
  }
}
Click to Expand

Content Rating

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

0