Back to Parent

// Define a pin that I'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;

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

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

// Define a pin I'll place a switch on
int switchPin = D3;

void setup()
{
    // for input, I define the switch as an input-pullup
    // this uses an internal pullup resistor to manage consistent reads from the device
    
    pinMode( switchPin, INPUT_PULLUP ); // sets pin as input
    
    // Set up the LED for output
    pinMode(ledPin, OUTPUT);
  
    // Create a cloud variable of type integer called 'force' mapped to fsrReading
    Particle.variable("force", &fsrReading, INT);
  
}

void loop()
{
    // find out if the switch is thrown to the wired terminal ot not or not by reading from it
    int switchState = digitalRead( switchPin );
    
    // when the switch is thrown to the wired terminal, we will get a LOW signal
    // when the button is thrown to the unwired terminal, we'll get a HIGH signal

    if( switchState == LOW )
    {
        // Use analogRead to read the fsr reading
        // This gives us a value from 0 to 4095
        fsrReading = analogRead(fsrPin);
        
        // find out if pressure levels are below 3000
        // set our LED on or off
        if ( fsrReading < 3000){
            ledBrightness = 0;
        }else {
            ledBrightness = 255;
        }
        
        // fade the LED to the desired brightness
        analogWrite(ledPin, ledBrightness);

        // wait 1/10th of a second and then loop
        delay(100);
        
    }else{
        // otherwise, turn the LED off
        digitalWrite( ledPin, LOW );
    }
}
Click to Expand

Content Rating

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

0