Lab submission 3 - 3 Pots for 3 LEDs

Submitted by stlim on Mon, 02/18/2013 - 23:08

 

Description

I made a safe dial, using an arduino uno board with 3 LEDs and 3 potentiometers.
While each dial is rotating, a corresponding LED keeps blinking.
As dials' value (0-255) change, the LEDs' brightness change.
When the dials are stable, the program checks if the combination is correct to unlock the (virtual) safe. Each LED's brightness represents a digit (0-9) for the combination.
If it's correct, the three LEDs blink in turn. In this demo, the combination if 782 (blue, red, and green).
 
 

Video Demo

http://youtu.be/lZw7et5i9pE
 

Components Used

For arduino:
- 1 red LED
- 1 green LED
- 1 blue LED
- 3 220  ohm resistor
- 3 potentiometers
- 1 Arduino Uno board
 

Code: Safe Dial

 
The code is based on an example code.
 
// Analog pin settings
int bIn = 0;    // Potentiometers connected to analog pins 0, 1, and 2
int rIn = 1;    //   (Connect power to 5V and ground to analog ground)
int gIn = 2;  
 
// Digital pin settings
int bOut = 9;   // LEDs connected to digital pins 9, 10 and 11
int rOut = 10;  //   (Connect cathodes to digital ground)
int gOut = 11;  
 
// Values
int bVal = 0;   // Variables to store the input from the potentiometers
int rVal = 0;  
int gVal = 0;  
 
//  Previous values
int prevB = 0;
int prevR = 0;
int prevG = 0;
 
//  Safe combination
int bCode = 7;
int rCode = 8;
int gCode = 2;
 
//  input for combination
int bNum = 0;
int rNum = 0;
int gNum = 0;
 
// Variables for comparing values between loops
int i = 0;            // Loop counter
int wait = (1000);    // Delay between most recent pot adjustment and output
 
int checkSum     = 0; // Aggregate pot values
int prevCheckSum = 0;
int sens         = 1; // Sensitivity theshold, to prevent small changes in 
                      // pot values from triggering false reporting
// FLAGS
int PRINT = 1; // Set to 1 to output values
int DEBUG = 0; // Set to 1 to turn on debugging output
 
void setup()
{
  pinMode(bOut, OUTPUT);   // sets the digital pins as output
  pinMode(rOut, OUTPUT);   
  pinMode(gOut, OUTPUT); 
  Serial.begin(9600);     // Open serial communication for reporting
}
 
void loop()
{
  i += 1; // Count loop
 
  bVal = analogRead(bIn) / 4;  // read input pins, convert to 0-255 scale
  rVal = analogRead(rIn) / 4; 
  gVal = analogRead(gIn) / 4;  
 
  analogWrite(bOut, bVal);    // Send new values to LEDs
  analogWrite(rOut, rVal);
  analogWrite(gOut, gVal);
 
  if (i % wait == 0)                // If enough time has passed...
  {    
    checkSum = bVal+rVal+gVal;      // ...add up the 3 values.
    if ( abs(checkSum - prevCheckSum) < sens )  //  If values are stable
    {
      rNum = rVal / 26;
      gNum = gVal / 26;
      bNum = bVal / 26;
      
      if(PRINT)                    // ...and if the PRINT flag is set...
      {
        Serial.print("B: ");        // ...then print the values.
        Serial.print(bNum);         
        Serial.print("\t"); 
        Serial.print("R: ");        
        Serial.print(rNum);
        Serial.print("\t");
        Serial.print("G: ");                
        Serial.println(gNum); 
        PRINT = 0;
      }
      
      if(rNum==rCode && gNum==gCode && bNum==bCode)
      {
        delay(300);        
        analogWrite(bOut, 255);
        analogWrite(gOut, 0);
        analogWrite(rOut, 0);
        delay(300);
        analogWrite(bOut, 0);
        analogWrite(gOut, 255);
        analogWrite(rOut, 0);
        delay(300);
        analogWrite(bOut, 0);
        analogWrite(gOut, 0);
        analogWrite(rOut, 255);
        delay(300);
      }
    }  
    else
    {
      PRINT = 1;  // Re-set the flag
      
      if(abs(rVal - prevR) > sens)
      {
        delay(100);
        analogWrite(rOut, 0);
        delay(100);
        analogWrite(rOut, rVal);
      }
      
      if(abs(gVal - prevG) > sens)
      {
        delay(100);
        analogWrite(gOut, 0);
        delay(100);
        analogWrite(gOut, gVal);
      }
      
      if(abs(bVal - prevB) > sens)
      {
        delay(100);
        analogWrite(bOut, 0);
        delay(100);
        analogWrite(bOut, bVal);
      }
    }
    
    prevCheckSum = checkSum;  // Update the values
    prevR = rVal;
    prevG = gVal;
    prevB = bVal;
  }
}
IMG_1187.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.