Lab 3 - Potentiometers (plus photocells)

Submitted by seanchen on Tue, 02/19/2013 - 23:43

Description

I chose option 1 but worked with 3 LEDs and added photocells to control the variation of each color. The 2 potentiometers still control the blinking frequency and the total brightness of all 3 LEDs. But when a photocell is covered, it decreases the corresponding LED value. The calculation was performed by mapping the photocell value from its Max and Min (measured manually) to a range from 0 to the total brightness value provided by the potentiometer. 

Video Demo

A possible issue of this setting is that the ambient light is not consistent so a fine tune may be required for it to show the best result. However, the demo video was taken in a different lighting then the testing environment and it turned out ok.

 

Components

1 x Arduino UNO Board

3 x LED lights (1 red, 1 blue, 1 green) with 3 x 220-ohm resistors
1 x bottle cap
2 x Potentiometers
3 x photocells with 3 x 10KΩ resistors
 

Code

const int potBrightPin = 0;   // select the input pin for the potentiometer 1

const int potFreqPin = 1;   // select the input pin for the potentiometer 2
int brightVal = 0;   // variable to store the value coming from pot 1
int freqVal = 0;   // variable to store the value coming from pot 2 
const int led1Pin = 9;   // select the pin for the LED 1
const int led2Pin = 10;  // select the pin for the LED 2
const int led3Pin = 11;  // select the pin for the LED 2
 
const int xpin = 2;                  // x-axis of the accelerometer
const int ypin = 3;                  // y-axis
const int zpin = 4;                  // z-axis
 
const int xMax = 382;
const int yMax = 663;
const int zMax = 848;
 
const int xMin = 68;
const int yMin = 175;
const int zMin = 346;
 
int xVal = 0;
int yVal = 0;
int zVal = 0;
 
void setup() {
  pinMode(led1Pin, OUTPUT);  // declare the led1Pin as an OUTPUT
  pinMode(led2Pin, OUTPUT);  // declare the led2Pin as an OUTPUT
  pinMode(led3Pin, OUTPUT);  // declare the led2Pin as an OUTPUT
  Serial.begin(9600);
}
 
void loop() {
  brightVal = analogRead(potBrightPin);   // read the value from pot 1, between 0 - 1024, for dimming
  freqVal = analogRead(potFreqPin);   // read the value from pot 2, between 0 - 1024, for blinking
   
  xVal = analogRead(xpin);
  yVal = analogRead(ypin);
  zVal = analogRead(zpin);
 
  Serial.print(xVal);
  Serial.print(", ");
  Serial.print(yVal);
  Serial.print(", ");
  Serial.print(zVal);
  Serial.println();
 
  
//  Serial.print("Bright: ");
//  Serial.println(brightVal/4);
//  Serial.print("Freq: ");
//  Serial.println(freqVal);
  
  setLEDon(xVal, yVal, zVal, brightVal/4);
  delay(freqVal);                  // stop the program for some time, meaning, LED is on for this time
  setLEDoff();
  delay(freqVal);                  // stop the program for some time, meaning, LED is OFF for this time
}
 
void setLEDon(int x, int y, int z, int bright) {
  
  Serial.print("Bright: ");
  Serial.println(bright);
  
  //to fine tune color
  x = map(x, xMin, xMax, 0, bright);
  y = map(y, yMin, yMax, 0, bright);
  z = map(z, zMin, zMax, 0, bright);
 
 
  // calibrate since the LED brightness is not consistant
  x = x * 1;
  y = y*0.7;
  z = z*0.5;
  
  x = constrain(x, 0, 255);
  y = constrain(y, 0, 255);
  z = constrain(z, 0, 255);
    
  Serial.print("After adjustment: ");
  Serial.println();
  Serial.print(x);
  Serial.print(", ");
  Serial.print(y);
  Serial.print(", ");
  Serial.print(z);
  Serial.println();
  Serial.println();
  
  analogWrite(led1Pin, x);
  analogWrite(led2Pin, y);
  analogWrite(led3Pin, z);
}
 
void setLEDoff() {
  analogWrite(led1Pin, 0);
  analogWrite(led2Pin, 0);
  analogWrite(led3Pin, 0);
}

 

demo pic
0
Your rating: None
Drupal theme by Kiwi Themes.