Go Down

Topic: Easy Color Mixer + LCD (Read 1 time) previous topic - next topic

bebo

Sep 30, 2011, 04:25 pm Last Edit: Sep 30, 2011, 04:31 pm by bebo Reason: 1
I'm designing metal sculpture and it will incorporate different colors of light.  I wanted an easy way to choose and mix colors, and to know what the values would be, so that I can write the PWM values into the code.  Basically, the three pots control the PWM output to the rgb led[common anode], and the values are written to the LCD.  With this setup, I can bring my Arduino to the shop and play with the light values instead of bringing the sculpture to the computer.  Here's the code, and a Fritzing picture of the circuit.
Quote

#include <LiquidCrystal.h>  //libraries to get the LCD to work
#include <Wire.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);   // ----------this assigns the pins on the Arduino to the pins on the LCD.
const int rPin = 3;  // these pins go to               // for a 16 pin LCD the values correspond to pins (4,6,11,12,13,14)  
const int gPin = 5;  // the transistors,                 // LCD pins 1, 5, and 16 go to ground.  Pins 2 and 15 go to 5V.  Pin 3 is for the contrast pot.
const int bPin = 6;  // they need to be PWM pins
const int rPotPin = A0, gPotPin = A1, bPotPin = A2;  //these pins read the pot values
int rPotVal = 0; // a place to keep the pot value
int gPotVal = 0;
int bPotVal = 0;
int rColorVal = 0;  // a place to store the color value
int gColorVal = 0;
int bColorVal = 0;

void setup(){
  lcd.begin(16,2); // sets up the 16 x 2 LCD
  lcd.print("R:   G:   B:   ");  //no need to reprint this every time, so put it in setup()
  pinMode (3,OUTPUT);  
     pinMode (5,OUTPUT);
        pinMode (6,OUTPUT);
  pinMode (A0,INPUT);
  pinMode (A1,INPUT);
  pinMode (A2,INPUT);
}

void loop(){
  rPotVal = analogRead(rPotPin);  //reads the pot value
  rPotVal = rPotVal/4;  //since analogRead is 0-1023 and analogWrite is 0-255, divide by four to give analogWrite proper values
  gPotVal = analogRead(gPotPin);
  gPotVal = gPotVal/4;
  bPotVal = analogRead(bPotPin);
  bPotVal = bPotVal/4;
  analogWrite (rPin, rPotVal); // writes brightness value to proper channel
  lcd.setCursor(2,1);          
  lcd.print(rPotVal, DEC);     // and tells the LCD what the value is
  lcd.print("  ");
  analogWrite (gPin, gPotVal);
  lcd.setCursor(7,1);
  lcd.print(gPotVal, DEC);
  lcd.print("  ");
  analogWrite (bPin, bPotVal);
  lcd.setCursor(12,1);
  lcd.print(bPotVal, DEC);
  lcd.print("  ");
}
 


putyn

a better way to take a analogRead and convert it for analogWrite would be the map function
Code: [Select]

gPotVal = map(analogRead(gPotPin), 0, 1023, 0, 255);

bebo

Hey that would work better wouldn't it...thanks for the tip

Go Up