Skill Dev2- Chengzhi Zhang
Made by Chengzhi Zhang
Made by Chengzhi Zhang
SkillDev2 in IoT
Created: November 12th, 2021
int photoCellPin = A0;
int switchPin = D4;
// 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()
{
// Set up the LED for output
pinMode(ledPin, OUTPUT);
pinMode( switchPin , INPUT_PULLUP);
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("light", &photoCellReading, INT);
}
void loop()
{
int buttonState = digitalRead( switchPin );
// Using a pulldown resistor we get a LOW
// Signal when its on
if( buttonState == LOW )
{
photoCellReading = analogRead(photoCellPin);
ledBrightness = map(photoCellReading, 0, 2000, 0, 255);
analogWrite(ledPin, ledBrightness);
delay(50);
}else{
// otherwise
// turn the LED Off
digitalWrite( ledPin, LOW);
}
// Use analogRead to read the photo cell reading
// This gives us a value from 0 to 4095
}
Click to Expand
I find it hard to map the LED lightness. I tried to detect the range of photo resistor using photoCellReading, finding it to be among 500 to 1500. However, I tried to map the LED lightness to this range, it still doesn't work so well. Maybe because I'm in the library and the ambient lightness is not so ideal. I also found we should be careful with the resistor. Incorporating the right one in the right place takes time.