Homework 02 – RGB LED Blink with Potentiometer – Josiah Song

29 Oct

Image

Because we are learning to use a potentiometer to control the time duration of the LEDs, I decided to create my code so that I could see the contrast of blinking lights determined by the potentiometer and lights determined by input values. My pattern is a sequence of 9 blinks, 3 by red, 3 by blue, and then 3 by green. The first blink in each set of 3 is determined by the potentiometer, the second is a set long blink, and the last is a set short blink. So the variation in the sequence is based on the potentiometer changing the time of the 1st, 4th, and 7th blinks.

Here is my code:

int red = 3; //setting red led variable
int blue = 5; //setting blue led variable
int green = 6; //setting green led variable
int pot = 0; //setting potentiometer

void setup() {
  pinMode(red, OUTPUT); //sets pin 3 to output
  pinMode(blue, OUTPUT); //sets pin 5 to output
  pinMode(green, OUTPUT); //sets pin 6 to output
}

void loop() {
  pot = (analogRead(A0));//sets variable to register potentiometer
  digitalWrite(red, HIGH);//turns red led on
  delay(pot);//delays how long the light is on based on potentiometer
  digitalWrite(red, LOW);//turns red led off
  delay(pot);//delays how long the light is off based on potentiometer
  digitalWrite(red, HIGH);//turns red led on
  delay(2000);//keeps light on for 2 seconds
  digitalWrite(red, LOW);//turns red led off
  delay(500);//keeps light off for 1/2 second
  digitalWrite(red, HIGH);//turns red led on
  delay(100);//keeps light on for 100 milliseconds
  digitalWrite(red, LOW);//turns red led off
  delay(100);//keeps light off for 100 milliseconds
  digitalWrite(blue, HIGH);//turns blue led on
  delay(pot);//delays how long the light is on based on potentiometer
  digitalWrite(blue, LOW);//turns blue led off
  delay(pot);//delays how long the light is off based on potentiometer
  digitalWrite(blue, HIGH);//turns blue led on
  delay(2000);//keeps light on for 2 seconds
  digitalWrite(blue, LOW);//turns blue led off
  delay(500);//keeps light off for 1/2 second
  digitalWrite(blue, HIGH);//turns blue led on
  delay(100);//keeps light on for 100 milliseconds
  digitalWrite(blue, LOW);//turns blue led off
  delay(100);//keeps light off for 100 milliseconds
  digitalWrite(green, HIGH);//turns green led on
  delay(pot);//delays how long the light is on based on potentiometer
  digitalWrite(green, LOW);//turns green led off
  delay(pot);//delays how long the light is off based on potentiometer
  digitalWrite(green, HIGH);//turns green led on
  delay(2000);//keeps light on for 2 seconds
  digitalWrite(green, LOW);//turns green led off
  delay(500);//keeps light off for 1/2 second
  digitalWrite(green, HIGH);//turns green led on
  delay(100);//keeps light on for 100 milliseconds
  digitalWrite(green, LOW);//turns green led off
  delay(100);//keeps light off for 100 milliseconds
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s