Fading and Flashing!

Description:

Today I decided to try to have my 3 LED lights change based on 2 potentiometers. I have one pot into the Analog input A0 and another into the Analog input A1. If I change A0 pot manually, it changes how bright the light is. If I change A1 pot manually, it changes how fast the blinking is.

Movie of my potentiometers in action!

Components:

  • Arduino Uno
  • Breadboard
  • 2x potentiometers
  • 3x LEDs (red, green, blue)
  • 3x 220Ω resistors
  • jumper wires
  • USB cable
  • computer

Code:


int sensorPin = A0; // select the input pin for the potentiometer
int potPin = 0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot

int sensorPin_blink = A1; // select the input pin for the potentiometer
int potPin_blink = 0; // Analog input pin that the potentiometer is attached to
int potValue_blink = 0; // value read from the pot

int redledPin = 9;
int blueledPin = 10;
int greenledPin = 11;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(redledPin, OUTPUT);
pinMode(blueledPin, OUTPUT);
pinMode(greenledPin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
potValue = analogRead(potPin); // read the pot value
analogWrite(redledPin, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(blueledPin, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(greenledPin, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
Serial.println("hello"); // print the pot value back to the debugger pane
delay(10);// wait 10 milliseconds before the next loop
////Blinking
potValue_blink = analogRead(sensorPin_blink);
// turn the ledPin on
analogWrite(redledPin, potValue/4);
analogWrite(blueledPin, potValue/4);
analogWrite(greenledPin, potValue/4);
// stop the program for milliseconds:
delay(potValue_blink);
// turn the ledPin off:
analogWrite(redledPin, 0);
analogWrite(blueledPin, 0);
analogWrite(greenledPin, 0);
// stop the program for for milliseconds:
delay(potValue_blink);

}

Leave a Reply