My first arduino project! This was a really basic circuit that switches the LED state when I press the switch.
The Automatic Night Light.
The LED comes on automatically when the room is dark.
This is where I played around a little and learnt about reading analog values and giving digital output.
It really helped me understand how the resistance value changes in a photo resistor and how the range could be successfully captured to give an appropriate output.
Arduino Code:
const int ledPin = 9; // pin that the LED is attached to int analogValue = 0; // value read from the pot int brightness = 0; // PWM pin that the LED is on.
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); // declare the led pin as an output: pinMode(ledPin, OUTPUT); } void loop() { analogValue = analogRead(A0); // read the pot value //divide by 4 to fit in a byte int brightness = map(analogValue, 200, 800, 5, 0);
analogWrite(ledPin, brightness); // PWM the LED with the brightness value Serial.println(analogValue); // print the brightness value back to the serial monitor Serial.println("\t"); Serial.println(brightness); }