Skill Dev2- Chengzhi Zhang

Made by Chengzhi Zhang

SkillDev2 in IoT

Created: November 12th, 2021

0
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
0
SkillDev2.0
Chengzhi Zhang - https://youtu.be/dOc0RGjrpsc
0

Outcome

  • I created an LED that can use switch to turn ON/OFF and use the photo resistor to change its lightness, as demonstrated in the video. The lightness of the LED does not change so drastically, maybe because of the photo resistor is having trouble accurately detect the light, or I maybe because I mapped the lightness not so well. It's strange no matter how I change the mapping function, the lightness of LED does not change so much, though.

0

Process

  • I used the tutorials on our IoT website, the switch one and the photo resistor one for reference. These codes seems to work and I figured it out how to combine them together. 
  • It takes a long time for me to incorporate the switch in and change the mapping of lightness of the resistor. It turned out to be that I didn't link the switch to the GND. 
  • Finally the switch and the photo resistor is working.

0

Reflection

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.

x