Back to Parent

// Define a pin that we'll place the pot on
int potPin = A1;

// Create a variable to hold the pot reading
int potReading = 0;

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

// Create a variable to hold the light reading
int fsrReading = 0;

// Define a pin that we'll place the photo cell on
// Remember to add a 10K Ohm pull-down resistor too.
int photoCellPin = A5;

// Create a variable to hold the light reading
int photoCellReading = 0;

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

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

int photoCellThres = 2047;

void setup()
{

    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("force", &fsrReading, INT);
    
    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("light", &photoCellReading, INT);

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

void loop()
{
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    fsrReading = analogRead(fsrPin);
    
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    
    if (digitalRead(ledPin) == LOW) {
        if (photoCellReading < photoCellThres && digitalRead(ledPin) == LOW) {
            // turn the LED On
            digitalWrite( ledPin, HIGH);
            // Map this value into the PWM range (0-255)
            // and store as the led brightness
            ledBrightness = map(photoCellReading, 0, 4095, 0, 255);
            ledBrightness = 255 - ledBrightness;

            // fade the LED to the desired brightness
            analogWrite(ledPin, ledBrightness);
        };
    };
    
    
    if (digitalRead(ledPin)== HIGH) {
        if (fsrReading > 1000 && fsrReading <= 1500) {
            // fade the LED to the desired brightness
            digitalWrite( ledPin, HIGH);
            analogWrite(ledPin, 255);
            delay(2000);
        } else if (fsrReading > 1500 && fsrReading <= 2000) {
            digitalWrite( ledPin, HIGH);
            // fade the LED to the desired brightness
            analogWrite(ledPin, 127);
            delay(2000);
        } else if (fsrReading > 2000) {
            // fade the LED to the desired brightness
             digitalWrite(ledPin, LOW);
             delay(2000);
        } else if (digitalRead(ledPin)== HIGH) {
            // Use analogRead to read the potentiometer reading
            // This gives us a value from 0 to 4095
            potReading = analogRead(potPin);

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

            // fade the LED to the desired brightness
            analogWrite(ledPin, ledBrightness);

        };
    };

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