Lab 3 - 3 LEDs, 5 Different Patterns

Submitted by bmcrae on Mon, 02/18/2013 - 22:57
bmcrae's picture

Description

In this lab, three LEDs were controlled using 3 potentiometers. In the beginning exercises, an LED's brightness was controlled using the rotational position of the potentiometer to change the input voltage. This concept of changing the input voltage using a potentiometer in order to change an output was used to create the final concept for this lab.

Three potentiometers were used to control three aspects of three LEDs on the breadboard. The first potentiometer changes the brightness of all three LEDs together. The second potentiometer controls the delay in between each blink of the LEDs. The third potentiometer controls the blinking pattern. The input values into the Arduino ranged from 0-1024. This was broken into 5 segments (ie. 0-204, 205-409, etc.), which corresponded to different blinking patterns. Depending on the rotational position of the third potentiometer, the blinking mode was changed. The first blinking mode has the LEDs light up one by one in the forward direction. The second mode has the LEDs light up one by one in the reverse direction. The third mode has the LEDs light up in a "back and forth" motion. The fourth mode has the 2 outer LEDs light up at once and then the middle LED light up afterwards in an alternating pattern. The fifth mode has plays the notes of the popular beginner's song "Hot Cross Buns" using LEDs.

This was a fun project that demonstrated the use of external inputs with code to control the outputs of the circuit.

Components Used

1- Arduino Uno

3 - 220 ohm resistors

1 - Red LED

1 - Blue LED

1 - Green LED

1 - Breadboard

3 - Potentiometers

Code

 

/*
 * Lab 3: Sensing Potentiometers
 * 
 * Modified version of AnalogInput
 * by DojoDave <http://www.0j0.org>
 * http://www.arduino.cc/en/Tutorial/AnalogInput 
 *
 * Modified by Brian McRae
 * 3 LEDs, 5 Different Modes, One Brightness Changer, and One Delay Changer
 *
 */
 
int potPin2 = A2;   // select the input pin for the potentiometer
int potPin1 = A1;
int potPin0 = A0;
 
int red_ledPin = 9;   // select the pin for the LED
int green_ledPin = 10;
int blue_ledPin = 11;
 
int brightness_val = 0;      // variable to store the brightness value coming from the sensor
int delay_val = 0;           // variable to store the delay value coming from the sensor
int decide_val = 0;          // variable to store the decision value coming from the sensor
 
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  brightness_val = analogRead(potPin2);    // read the value from the sensor, between 0 - 1024
  delay_val = analogRead(potPin1);
  decide_val = analogRead(potPin0);
  
  // Play the Forward Direction pattern
  if (decide_val <= 205) {
    Serial.println("Forward Direction pattern");
    Serial.print("LED brightness is: ");
    Serial.print(brightness_val);
    Serial.println();
    Serial.print("Delay value is: ");
    Serial.print(delay_val);
    Serial.println();
    Serial.println();
    
    analogWrite(red_ledPin, brightness_val/4); // analogWrite can be between 0-255
    delay(delay_val);
    analogWrite(red_ledPin, 0);
    delay(delay_val);
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(green_ledPin, 0);
    delay(delay_val);
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(blue_ledPin, 0);
    delay(delay_val);
  }
  
  // Play the Backward Direction pattern
  if (decide_val > 205) {
    if (decide_val <= 409) {
      Serial.println("Backward Direction pattern");
      Serial.print("LED brightness is: ");
      Serial.print(brightness_val);
      Serial.println();
      Serial.print("Delay value is: ");
      Serial.print(delay_val);
      Serial.println();
      Serial.println();
      
      analogWrite(blue_ledPin, brightness_val/4);
      delay(delay_val);
      analogWrite(blue_ledPin, 0);
      delay(delay_val);
      
      analogWrite(green_ledPin, brightness_val/4);
      delay(delay_val);
      analogWrite(green_ledPin, 0);
      delay(delay_val);
      
      analogWrite(red_ledPin, brightness_val/4); // analogWrite can be between 0-255
      delay(delay_val);
      analogWrite(red_ledPin, 0);
      delay(delay_val);
    }
  }
  
  // Play the Back and Forth pattern
  if (decide_val > 409) {
    if (decide_val <= 613) {
      Serial.println("Back and Forth pattern");
      Serial.print("LED brightness is: ");
      Serial.print(brightness_val);
      Serial.println();
      Serial.print("Delay value is: ");
      Serial.print(delay_val);
      Serial.println();
      Serial.println();
    
      analogWrite(red_ledPin, brightness_val/4); // analogWrite can be between 0-255
      delay(delay_val);
      analogWrite(red_ledPin, 0);
      delay(delay_val);
      analogWrite(green_ledPin, brightness_val/4);
      delay(delay_val);
      analogWrite(green_ledPin, 0);
      delay(delay_val);
      analogWrite(blue_ledPin, brightness_val/4);
      delay(delay_val);
      analogWrite(blue_ledPin, 0);
      delay(delay_val);  
      analogWrite(green_ledPin, brightness_val/4);
      delay(delay_val);
      analogWrite(green_ledPin, 0);
      delay(delay_val);
    }
  }
  
  // Play the Alternating Two and One pattern
  if (decide_val > 613) {
    if (decide_val <= 817) {
      Serial.println("Alternating Two and One pattern");
      Serial.print("LED brightness is: ");
      Serial.print(brightness_val);
      Serial.println();
      Serial.print("Delay value is: ");
      Serial.print(delay_val);
      Serial.println();
      Serial.println();
    
      analogWrite(red_ledPin, brightness_val/4);
      analogWrite(blue_ledPin, brightness_val/4);
      delay(delay_val);
      analogWrite(red_ledPin, 0);
      analogWrite(blue_ledPin, 0);
      analogWrite(green_ledPin, brightness_val/4);
      delay(delay_val);
      analogWrite(green_ledPin, 0);
    }
  }
  
  // Play the Hot Cross Buns song
  if (decide_val > 817) {
    Serial.println("Hot Cross Buns song");
    Serial.print("LED brightness is: ");
    Serial.print(brightness_val);
    Serial.println();
    Serial.print("Delay value is: ");
    Serial.print(delay_val);
    Serial.println();
    Serial.println();
  
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(blue_ledPin, 0);
    delay(delay_val);
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(green_ledPin, 0);
    delay(delay_val);
    analogWrite(red_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(red_ledPin, 0);
    delay(delay_val);
    
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(blue_ledPin, 0);
    delay(delay_val);
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(green_ledPin, 0);
    delay(delay_val);
    analogWrite(red_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(red_ledPin, 0);
    delay(delay_val);
    
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(blue_ledPin, 0);
    delay(delay_val/2);
    
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(blue_ledPin, 0);
    delay(delay_val/2);
    
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(blue_ledPin, 0);
    delay(delay_val/2);
    
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(blue_ledPin, 0);
    delay(delay_val/2);
    
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(green_ledPin, 0);
    delay(delay_val/2);
    
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(green_ledPin, 0);
    delay(delay_val/2);
    
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(green_ledPin, 0);
    delay(delay_val/2);
    
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val/2);
    analogWrite(green_ledPin, 0);
    delay(delay_val);
    
    analogWrite(blue_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(blue_ledPin, 0);
    delay(delay_val);
    analogWrite(green_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(green_ledPin, 0);
    delay(delay_val);
    analogWrite(red_ledPin, brightness_val/4);
    delay(delay_val);
    analogWrite(red_ledPin, 0);
    delay(delay_val);
  }
    
}
DesignPhoto
0
Your rating: None
Drupal theme by Kiwi Themes.