Skill Dev II - Miley Hu
Made by Miley Hu
Made by Miley Hu
Created: November 10th, 2021
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.
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
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
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
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
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.
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