Yifan Chen - skill dev 2

Made by yifanch2

skill dev2

Created: November 11th, 2021

0

I start with the circuit shown below : 

0
// Define a pin that we'll place the photo cell on
// Remember to add a 10K Ohm pull-down resistor too.
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() {
  // Set up the LED for output
  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

And it works!

0

Then I start by adding a switch and a if statement to the code. 

0
// Define a pin that we'll place the photo cell on
// Remember to add a 10K Ohm pull-down resistor too.
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;

int switchPin = D3;


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() {
// Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
  photoCellReading = analogRead(photoCellPin);
  
   int buttonState = digitalRead( switchPin );

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

if( buttonState == LOW ){
  // fade the LED to the desired brightness
  analogWrite(ledPin, 255-ledBrightness);

  // wait 1/10th of a second and then loop
  delay(100);
}
else{
    analogWrite(ledPin, ledBrightness);
}
}
Click to Expand
0
0

Reflection:

It is hard to understand how to connect the wire first, and I spend lots of time working on the sensor and trying to figure out when the code means. I should start with my own thoughts next time, so the project could be more creative. 

x
Share this Project


About

skill dev2