Workshop 2 resources

 

1x button 1x LED

1x potentiometer (variable resistor/pot) 1x LED

1x LDR (light dependent resistor/light sensor) 1x LED



/*
Control LED fading with potentiometer

The circuit:
Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 9
* LED cathode (short leg) attached to ground

Created by Luke Woodbury
6 Jan 2016

*/

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 9;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
   // initialize serial:
 Serial.begin(9600);
 // declare the ledPin as an OUTPUT:
 pinMode(ledPin, OUTPUT);
}

void loop() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 //remap the sensor value from 0-1023 to 0-255 for LED PWM fading
 sensorValue = map(sensorValue, 0, 1023, 0, 255);
 //print sensorVal to serial monitor
 Serial.println(sensorValue);
 //write value to LED pin as PWM
 analogWrite(ledPin, sensorValue);

}