Back to Parent

Recording warmth
// Define the Pin the Temperature sensor is on
int tempPin = A0;
int ledPin1 = D2;
int ledPin2 = D3;
int ledPin3 = D4;
int ledPin4 = D5;
int fsrPin = A5;
int fsrReading = 0;
int ledBrightness = 0;
bool isTouched = false;
int fsrave = 0;
float fsweight = 0.9;

// float expave0 = 0.0;
// float expave1 = 0.0;
// float expave2 = 0.0;

// double weightcap = 0.99;

// int roundedexpave0 = 0;
// int roundedexpave1 = 0;
// int roundedexpave2 = 0;

// int roundedexpave = 0;

// Create a variable that will store the temperature value
double temperature = 0.0;
double tempRnd = 0.0;
double weight = 0.99;
double avetmp = 0.0;
int count = 0;
// bool istouched = true;

void setup()
{
  // Register a Particle variable here
  Particle.variable("force", &fsrReading, INT);
  Particle.publish( "ready to feel the warmth" );
  delay(3000);
//   pinMode(TouchPin0, INPUT);  
//   pinMode(TouchPin1, INPUT);  
//   pinMode(TouchPin2, INPUT);  
 
    //Initiate Touch Pin as Input Pin
//   Particle.variable("expave0", &roundedexpave0, INT);
//   Particle.variable("expave1", &roundedexpave1, INT);
//   Particle.variable("expave2", &roundedexpave2, INT);
//   Particle.variable("touchreading", &touchreading, INT);
  
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

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


void resetLEDs(){
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
}

void outputLEDs(int level){
    if (level > 10000){
        level = 10000;
    }
    
    if (level < 1){
        level = 1;
    }
    
    if (level >= 3500){
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, HIGH);
        digitalWrite(ledPin4, HIGH);
    }else{
        if (level >= 2500){
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
            digitalWrite(ledPin3, HIGH);
        }else{
            if (level >= 1800){
                digitalWrite(ledPin1, HIGH);
                digitalWrite(ledPin2, HIGH);
            }else{
                if (level >= 10){
                    digitalWrite(ledPin1, HIGH);
                }
            }
        }
    }
}

void loop()
{
   fsrReading = analogRead(fsrPin);
   fsrave = fsweight * fsrave + (1 - fsweight) *fsrReading;
  //ledBrightness = map(fsrReading, 0, 4095, 0, 255);
//   analogWrite(ledPin, ledBrightness);
   //delay(100);
   if (fsrave > 2000){
      isTouched = true;
      count++;
   }else{
      count = 0;
      resetLEDs();
   }
   //count = 9000;
   //outputLEDs(count);
   
   //Particle.publish( "FSR Reding publish", String(fsrReading));
   //delay(6000);
   if (isTouched == true){
      // 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;
      avetmp = weight * avetmp + temperature * (1 - weight);
      
      tempRnd = round(avetmp * 100) / 100;
      outputLEDs(count);
      
   }
   
   delay(2); 
}
  
 /*
   //read the touch value and assign to touch variable
    // touch = analogRead(TouchPin1);
    // expave1 = weightcap * expave1 + (1 - weightcap) * touch;
    // roundedexpave1 = round(expave1);
    
    // touch = analogRead(TouchPin2);
    // expave2 = weightcap * expave2 + (1 - weightcap) * touch;
    // roundedexpave2 = round(expave2);
    
    // touch = analogRead(TouchPin0);
    // expave0 = weightcap * expave0 + (1 - weightcap) * touch;
    // roundedexpave0 = round(expave0);
    
    // roundedexpave = round((expave1 + expave2 + expave0) /3);
    // touchreading = map( roundedexpave, 0, 4095, 0, 255);
    // delay(1);
    //Publish the result
    //Particle.publish( "TouchDetected" );
    //Particle.publish( "TouchSensorReading", String( touchreading ));
    //delay(6000);
    
// }
*/
Tarika Jain (2019) Click to Expand

Content Rating

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

0