Back to Parent

// Define a pin that we'll place the FSR on
// Remember to add a 10K Ohm pull-down resistor too.
int flexPin = A0;

// Create a variable to hold the FSR reading
int flexReading = 0;

// Define a pin we'll place an LED on
int ledPin = A2;

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

// Our button wired to D3
int buttonPin = D3;

//turning the circuit off 
volatile int state = 0;

 
void setup() 
{
    // Set up the LED for output
    pinMode(ledPin, OUTPUT);
    // Set up the flex for output
    pinMode(flexPin, INPUT);
    // Set up the buttom for output

    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("force", flexReading);
    
    pinMode( buttonPin , INPUT_PULLUP); 

 }


void loop() 
{
    // Use analogRead to read from the sensor
    // This gives us a value from 0 to 4095
    int buttonState = digitalRead( buttonPin );
    delay(100);
    
    //if button is pressed TURN ON OR OFF THE CURCUIT 
    if (buttonState == LOW) 
    {
        //turning the circuit on OR OFF
        // state = 1; circuit on 
        // state = 0; circuit off
        state = 1-state;
        // state = !0 = 1 
        // state = !1 = 0
    }
    
    //when the circuit is on, dim/brighten the led 
    //if the circuit is on, dim the light 
    if (state == 1)
    {
        flexReading = analogRead(flexPin);  
        
        // Map this value into the PWM range (0-255)
        // and store as the led brightness
        ledBrightness = map(flexReading, 110, 0, 0, 255);
        
        // fade the LED to the desired brightness
        analogWrite(ledPin, ledBrightness);
        
        // wait 1/10th of a second and then loop
        delay(100);
        
    }
    
    //when the circuit is off, turn off the led
    if (state == 0)
    {
    // fade the LED to dark
    
        // ledBrightness = 0;
        
        // analogWrite(ledPin, ledBrightness);
        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