A3: blinking and dimming

Posted by lizzy

Anonymous's picture

Description

 I slightly modified the code for Option 1.
After completing option 1, I decided I wanted each pot to control its own LED. So Pot1 controls the blinking of the blue LED, whereas Pot2 controls the dimming of the green LED.

At a certain point, I removed 'delay', which stopped the blinking (not removed in the code below). This allowed me to play with the dimming effect, which allowed me to  mix the green and blue LEDs!
If I had a third pot, I would have modified this code to include red. It's fun to modified code!
 

Components

Breadboard

Arduino Microcontroller

RGB LEDs

Resistors

2 Potentiometors

Code

/*
 * I modified the code. Instead of 1 led, I used 2 pots.
 * Pot1 controls the blinking of Blue LED and Pot2 changes the Green LED's brightness (dimming).
 * modification of the following
 * http://www.arduino.cc/en/Tutorial/AnalogInput
 */
int pot1Pin = 2;   // select the input pin for the potentiometer 1. Blinks
int pot2Pin = 5;   // select the input pin for the potentiometer 2. dims
int pot1Val = 0;   // variable to store the value coming from pot 1
int pot2Val = 0;   // variable to store the value coming from pot 2
int led1Pin = 11;   // select the pin for the LED 1 (blue)
int led2Pin = 10;  // select the pin for the LED 2 (green)
void setup() {
   pinMode(led1Pin, OUTPUT);  // declare the led1Pin as an OUTPUT
   pinMode(led2Pin, OUTPUT);  // declare the led2Pin as an OUTPUT
}
void loop() {
   pot1Val = analogRead(pot1Pin);   // read the value from pot 1, between 0 - 1024, for dimming
   pot2Val = analogRead(pot2Pin);   // read the value from pot 2, between 0 - 1024, for blinking
   analogWrite(led2Pin, pot2Val/4); // Controls dimming of green LED
   

  analogWrite(led1Pin, pot1Val/4); // Controls blinking of blue LED
  delay(pot1Val);                  // stop the program for some time, meaning, LED is on for this time. This causes blinking.
   analogWrite(led1Pin, 10);       // when blinking goes faster, blue LED does not turn off, which it was doing before. Now, LED is just 10.
   delay(pot1Val);                 // stop the program for some time, meaning, LED is OFF for this time
}

Lizzy A3: image 2
Lizzy A3: image 1
0
Your rating: None
Tags: