Pages: [1]   Go Down
Author Topic: limiting max PMW with code  (Read 385 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, I am doing my first real arduino project with my niece, making a RGB color mixing box/projector for her school science fair project.  I am using a duemilanove 168 to drive a 10w smd rgb led using resistors and three TIP122 transistors. Three pots (with rgb painted knobs of course) provide the analog input.  I am desperately trying to learn to code, its one of the few things I haven't been able to learn on my own on the net, it's a little daunting at times.    My problem is this led is way too bright and gets too hot, I don't want it blinding children while melting down in the classroom.  The other problem is I accidently burned out one of the three red led's in the module(9 total) by using too little resistance while testing so red is a little dimmer.  I don't have the time or extra money right now to rebuild so what I want to do is limit the max duty cycle of the pwm of each led output until I find a not so bright cool running temp and all are even.  But I also want the pots to smoothly vary the output from 0%-max(limited) pwm.  I tried to play with the code a little but got some wonky results, I just don't have enough practice to get a good enough understanding on how to modify it although I clearly grasp the concepts.  I have many more arduino projects planed so does anyone have a good site or book they recommend to a programming noob? Thanks in advance for any help, here is the code I'm using:

 
// Init the Pins used for PWM
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
 
// Init the Pins used for 10K pots
const int redPotPin = 0;
const int greenPotPin = 1;
const int bluePotPin = 2;
 
// Init our Vars
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}
 
void loop()
{
// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
  currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
  currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
 
// Write the color to each pin using PWM and the value gathered above
  analogWrite(redPin, currentColorValueRed);
  analogWrite(bluePin, currentColorValueBlue);
  analogWrite(greenPin, currentColorValueGreen);
 
}
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46214
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
The 255 is the maximum value for the PWM pin. If you want a lower maximum value, it should be pretty easy to see what to change...
Logged

Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm open to correction but I think you could reduce the max pwm value in the map where 1023 becomes 255, you could make that less say 150.

Also look at constrain

edit.... on second thoughts reducing the 255 wont work because it gets subtracted from 255 again.... in fact have to increase the 0 side to say 100 so the subtracted value is between 255-100 = 150 or 255-255 =0
« Last Edit: January 22, 2013, 12:29:00 pm by JimboZA » Logged

Retired from Arduino.
PMs are set to ignore all.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2943
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the LED is too bright, you could get a resistor and wire it to the pin from the LED going to ground OR get 3 resistors and wire them to the 3 pins for the colors.

Another alternative would be to use another PWM pin and connect the LED common wire to it and adjust the brightness in the software. No resistors required.
« Last Edit: January 22, 2013, 12:33:11 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey thanks all for the replies.  Yeah the first thing I did before I asked was change the 255 down to 150 and it did some strange things, cant remember but it did 0-100% on the first half of the pot sweep and did some weird stuff after turning it further.  I added a heat sink and adjusted a resistor as well as made a nimh pack to mount in the box, it runs a few volts lower than the SLA I was using to test it.  I would like to find out how to do it with code still though, I have many automotive projects planned that this would be useful for.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46214
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yeah the first thing I did before I asked was change the 255 down to 150 and it did some strange things, cant remember but it did 0-100% on the first half of the pot sweep
Sounds like you did not change both 255's to 150 in the statements.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31534
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
to drive a 10w smd rgb led using resistors and three TIP122 transistors
You can not run such a high power LED with just a resistor to limit the current. The forward volts drop changes with temprature and that is why you burnt out your LED. To drive such high powerd lights you need a constant current supply circuit, one for each of the three colours.
Logged

Pages: [1]   Go Up
Jump to: