Skill Dev II - Miley Hu

Made by Miley Hu

Created: November 10th, 2021

0

Outcome

The final outcome is a connected circuit with one LED light, a photoresistor, and input from one tactile switch button. When the switch is on, the LED light will change the level of lumination based on the light level reading from the photoresistor. The more light detected by the photoresistor, the more lighted up the LED will be. Once the switch is turned off, the LED will not be illuminated.

0
Skill Dev II
Miley Hu - https://youtu.be/biz3RHEtl_4
0
int photoCellPin = A0;

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

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

int button = D4;
int buttonReading = digitalRead(button);

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

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(button, INPUT);
    
    Particle.variable("light", &photoCellReading, INT);
}

void loop() {
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);

    // Map this value into the PWM range (0-255)
    // and store as the led brightness
    if (digitalRead(button) == HIGH && photoCellReading > 1500)  {
        ledBrightness = 255;
    } else if (digitalRead(button) == HIGH && photoCellReading < 2000){
        ledBrightness = 80;
    } else {
        ledBrightness = 0;
    }

    // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
}
Click to Expand
0

Process

I first started to work through the exercises with the photoresistor to get familiarized with different functions.

1 Connecting the LED and the photoresistor

0
int photoCellPin = A0;

// 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;

void setup() {
    pinMode(ledPin, OUTPUT);
    
    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("light", &photoCellReading, INT);
}

void loop() {
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    
    // Map this value into the PWM range (0-255)
    // and store as the led brightness
    ledBrightness = map(photoCellReading, 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
0

Additional exercise #1

Modify the program so that the LED shows the opposite. When there is little light, it will be bright and when there’s lots of light, it will be off.

0
int photoCellPin = A0;

// 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;

void setup() {
    pinMode(ledPin, OUTPUT);
    
    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("light", &photoCellReading, INT);
}

void loop() {
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    
    // Map this value into the PWM range (0-255)
    // and store as the led brightness
    ledBrightness = map(photoCellReading, 0, 4095, 255, 0);
    
    // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
    
    // wait 1/10th of a second and then loop
    delay(100);
}
Click to Expand
0

Additional exercise #2

Instead of fading the light, why not turn it on or off when the light reaches certain levels. In this case, my threshold was 2000.

0
int photoCellPin = A0;

// 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;

void setup() {
    pinMode(ledPin, OUTPUT);
    
    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("light", &photoCellReading, INT);
}

void loop() {
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    
    // Map this value into the PWM range (0-255)
    // and store as the led brightness
    if (photoCellReading > 2000) {
        ledBrightness = 255;
    } else {
        ledBrightness = 0;
    }

    // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
    
    // wait 1/10th of a second and then loop
    delay(100);
}
Click to Expand
0

Adding additional input -- the switch button

After completing the additional exercises, I took some time to familiarize myself with some of the inputs and sensors in the kit. I eventually picked the switch button and used it as an on/off button for the light. Below was the code for turning the light on and off using the switch button.

0
int photoCellPin = A0;

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

// Define a pin we'll place an LED on
int ledPin = D2;
int button = D4;
int buttonReading = digitalRead(button);

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

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(button, INPUT);
    
    // Create a cloud variable of type integer
    // called 'light' mapped to photoCellReading
    Particle.variable("light", &photoCellReading, INT);
    Particle.variable("button", buttonReading);
}

void loop() {
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);

    // Map this value into the PWM range (0-255)
    // and store as the led brightness
    if (digitalRead(button) == HIGH)  {
        ledBrightness = 255;
    } else {
        ledBrightness = 0;
    }

    // fade the LED to the desired brightness
    analogWrite(ledPin, ledBrightness);
}
Click to Expand
0
Adding the switch button to the circuit
Miley Hu - https://youtu.be/ZKnmBjWgroY
x