Blinking and dimming LEDs

Components

  • 1 Arduino Uno
  • 3 220 ohm resistors
  • 3 LEDs (one red, one green, one blue)
  • 2 potentiometers
  • 1 breadboard

Description

Make lights blink faster or slower by turning one of the pots. Make the lights dim by turning one of the pots.

Code

int sensorPin = A0; // select the input pin for the potentiometer
int blueLedPin = 11; // select the pin for the LED
int greenLedPin = 10;
int redLedPin = 9;
int sensorValue = 0; // variable to store the value coming from the sensor

int potPin = A1; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
//int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(blueLedPin, OUTPUT);
 pinMode(redLedPin, OUTPUT);
 pinMode(greenLedPin, OUTPUT);

 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 // declare the led pin as an output:
// pinMode(led, OUTPUT);
}

void loop() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 potValue = analogRead(potPin); // read the pot value
 // turn the ledPin on
 analogWrite(blueLedPin, potValue/4);
 analogWrite(redLedPin, potValue/4);
 analogWrite(greenLedPin, potValue/4);
 // stop the program for <sensorValue> milliseconds:
 delay(sensorValue);
 // turn the ledPin off:
 analogWrite(blueLedPin, 0);
 analogWrite(redLedPin, 0);
 analogWrite(greenLedPin, 0);
 // stop the program for for <sensorValue> milliseconds:
 delay(sensorValue);
}

<img class="alignnone size-medium wp-image-1012" src="http://blogs.ischool.berkeley.edu/i262/files/2016/09/IMG_7357-300x225.jpg" alt="IMG_7357" width="300" height="225" />

Leave a Reply