Lab 3 - LED and Rotational Pots

Submitted by jooddang on Tue, 02/19/2013 - 22:57

Description

My assignment has two parts. First, three rotational pots control 3 LEDs for each. If rotated, the first one controls brightness of red LED. The second one controls green one, and the third one does blue one. Each pot sends signal of 0~1023. Arduino receives the signal and transform it into 0~255 boundary.

 

Extra points:
Come up with an interesting mapping between the rotational position pot and LED output.

 

The second one is "Guessing the Answer" mapping. At the first time, it comes up with random "Answer Number" for red, green, and blue each. While rotating each pot, a user has to guess the number for each LED. As user's guessing comes closer to the answer, blinking of the LED becomes faster. If the user succeeds to find the answer (+- 5 margin) the LED stops blinking. If the user succeeds to find all three LED's number, LEDs are on for 5 seconds, and then another random numbers come.

Components

 

Arduino Uno board
- potentiometers (x3)
- red LED / green LED / blue LED
- 220ohm resistor (x3)
 
 

Code

 

Ordinary Control

 
// Analog pin settings
int aIn = 0;    // Potentiometers connected to analog pins 0, 1, and 2
int bIn = 1;    //   (Connect power to 5V and ground to analog ground)
int cIn = 2;  
 
// Digital pin settings
int aOut = 9;   // LEDs connected to digital pins 9, 10 and 11
int bOut = 10;  //   (Connect cathodes to digital ground)
int cOut = 11;  
 
// Values
int aVal = 0;   // Variables to store the input from the potentiometers
int bVal = 0;  
int cVal = 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         = 3; // 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 = 1; // Set to 1 to turn on debugging output
 
void setup()
{
  pinMode(aOut, OUTPUT);   // sets the digital pins as output
  pinMode(bOut, OUTPUT);   
  pinMode(cOut, OUTPUT); 
  Serial.begin(9600);     // Open serial communication for reporting
}
 
void loop()
{
  i += 1; // Count loop
 
  aVal = analogRead(aIn) / 4;  // read input pins, convert to 0-255 scale
  bVal = analogRead(bIn) / 4; 
  cVal = analogRead(cIn) / 4;  
 
  analogWrite(aOut, aVal);    // Send new values to LEDs
  analogWrite(bOut, bVal);
  analogWrite(cOut, cVal);
 
  if (i % wait == 0)                // If enough time has passed...
  {    
    checkSum = aVal+bVal+cVal;      // ...add up the 3 values.
    if ( abs(checkSum - prevCheckSum) > sens )   // If old and new values differ 
                                                  // above sensitivity threshold
    {
      if (PRINT)                    // ...and if the PRINT flag is set...
      {
        Serial.print("A: ");        // ...then print the values.
        Serial.print(aVal);         
        Serial.print("\t"); 
        Serial.print("B: ");        
        Serial.print(bVal);
        Serial.print("\t");
        Serial.print("C: ");                
        Serial.println(cVal); 
        PRINT = 0;
      }
    }  
    else
    {
      PRINT = 1;  // Re-set the flag   
    } 
    prevCheckSum = checkSum;  // Update the values
 
    if (DEBUG)   // If we want debugging output as well...
    {
      Serial.print(checkSum);
      Serial.print("<=>");
      Serial.print(prevCheckSum);
      Serial.print("\tPrint: ");
      Serial.println(PRINT);
    }
  }
}
 
 
 
 

"Guessing the Answer" mapping

 
 
// Analog pin settings
int aIn = 0;    // Potentiometers connected to analog pins 0, 1, and 2
int bIn = 1;    //   (Connect power to 5V and ground to analog ground)
int cIn = 2;  
 
// Digital pin settings
int aOut = 9;   // LEDs connected to digital pins 9, 10 and 11
int bOut = 10;  //   (Connect cathodes to digital ground)
int cOut = 11;  
 
// Values
int aVal = 0;   // Variables to store the input from the potentiometers
int bVal = 0;  
int cVal = 0;
 
// Answer value
int aAnswer = 0;
int bAnswer = 0;
int cAnswer = 0;
 
int aAnswerFlag = 0;
int bAnswerFlag = 0;
int cAnswerFlag = 0;
 
// Variables for comparing values between loops
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
int SUCCESS = 1; // Set to 1 if succeed to find the brightness
 
void setup()
{
  pinMode(aOut, OUTPUT);   // sets the digital pins as output
  pinMode(bOut, OUTPUT);   
  pinMode(cOut, OUTPUT); 
  Serial.begin(9600);     // Open serial communication for reporting
}
 
void loop()
{
  if (SUCCESS == 1) {
    aAnswer = random(256);
    bAnswer = random(256);
    cAnswer = random(256);
    Serial.print("[");
    Serial.print(aAnswer);
    Serial.print("]");
    Serial.print("[");
    Serial.print(bAnswer);
    Serial.print("]");
    Serial.print("[");
    Serial.print(cAnswer);
    Serial.println("]");
    
    SUCCESS = 0;
  }
  
  aVal = analogRead(aIn) / 4;  // read input pins, convert to 0-255 scale
  bVal = analogRead(bIn) / 4; 
  cVal = analogRead(cIn) / 4;  
 
//    Serial.print("1111[");
//    Serial.print(aVal);
//    Serial.print("]");
//    Serial.print("[");
//    Serial.print(bVal);
//    Serial.print("]");
//    Serial.print("[");
//    Serial.print(cVal);
//    Serial.println("]");
 
//  analogWrite(aOut, aVal);    // Send new values to LEDs
//  analogWrite(bOut, bVal);
//  analogWrite(cOut, cVal);
 
//    checkSum = aVal+bVal+cVal;      // ...add up the 3 values.
    checkSum = analogRead(aIn)+analogRead(bIn)+analogRead(cIn);
    
    if ( abs(checkSum - prevCheckSum) > sens )   // If old and new values differ above sensitivity threshold
    {
      if (aVal < aAnswer + 5 && aVal > aAnswer - 5) {
        analogWrite(aOut, 255);
        aAnswerFlag = 1;
      }
      else {
        for (int i = 0; i < 5; i++) {
          int diff = abs(aVal - aAnswer);
          analogWrite(aOut, 255);
          delay(diff);
          analogWrite(aOut, 0);
          delay(diff);
        }
      }
      
      if (bVal < bAnswer + 5 && bVal > bAnswer - 5) {
        analogWrite(bOut, 255);
        bAnswerFlag = 1;
      }
      else {
        bAnswerFlag = 0;
        for (int i = 0; i < 5; i++) {
          int diff = abs(bVal - bAnswer);
          analogWrite(bOut, 255);
          delay(diff);
          analogWrite(bOut, 0);
          delay(diff);
        }
      }
      
      if (cVal < cAnswer + 5 && cVal > cAnswer - 5) {
        analogWrite(cOut, 255);
        cAnswerFlag = 1;
      }
      else {
        cAnswerFlag = 0;
        for (int i = 0; i < 5; i++) {
          int diff = abs(cVal - cAnswer);
          analogWrite(cOut, 255);
          delay(diff);
          analogWrite(cOut, 0);
          delay(diff);
        }
      }
      
      if (PRINT)                    // ...and if the PRINT flag is set...
      {
        Serial.print("A: ");        // ...then print the values.
        Serial.print(aVal);         
        Serial.print("\t"); 
        Serial.print("B: ");        
        Serial.print(bVal);
        Serial.print("\t");
        Serial.print("C: ");                
        Serial.println(cVal); 
        PRINT = 0;
      }  
    }
    else
    {
      PRINT = 1;  // Re-set the flag   
    } 
    prevCheckSum = checkSum;  // Update the values
 
    if (aAnswerFlag == 1 && bAnswerFlag == 1 && cAnswerFlag == 1) {
      aAnswerFlag = 0;
      bAnswerFlag = 0;
      cAnswerFlag = 0;
      SUCCESS = 1;
      delay (5000);
    }
 
    if (DEBUG)   // If we want debugging output as well...
    {
      Serial.print(checkSum);
      Serial.print("<=>");
      Serial.print(prevCheckSum);
      Serial.print("\tPrint: ");
      Serial.println(PRINT);
    }
 
}
 
ar_0.jpg
0
Your rating: None
Drupal theme by Kiwi Themes.