Introduction: Arduino Fading Light

Picture of Arduino Fading Light

This is a Arduino controlled night light.

materials:

1 x LED
5 x jumper wires
1 x arduino
1 x photocell
1 x breadboard
1 x 10k ohm resistor

Here is the code, don't type the stuff after the //, the code will get too long:

// These constants won't change:
const int sensorPin = A0;    // pin that the sensor is attached to
const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


void setup() {
  // turn on LED to signal the start of the calibration period:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW);
}

void loop() {
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

Comments

MarceloS102 made it! (author)2017-03-25

Thank you so much, it helped me a lot!

You're welcome!

audreyobscura (author)2014-04-14

Do you mind sharing your circuit diagram too?

I have not made one yet.

But thanks for asking, and vote for it (the project) on the sensor contest!!

About This Instructable

1,930views

22favorites

License:

More by origamiAirControl:how to draw a birdNutball gliderArduino radar
Add instructable to: