Pages: [1]   Go Down
 Author Topic: limiting max PMW with code  (Read 349 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 3
 « on: January 22, 2013, 12:20:09 pm » Bigger Smaller 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
Online
Brattain Member
Karma: 517
Posts: 44532
Seattle, WA USA
 « Reply #1 on: January 22, 2013, 12:22:48 pm » Bigger Smaller 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

Johannesburg UTC+2
Offline
Karma: 60
Posts: 3241
 « Reply #2 on: January 22, 2013, 12:25:34 pm » Bigger Smaller 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

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Queens, New York
Offline
Karma: 59
Posts: 2732
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #3 on: January 22, 2013, 12:30:06 pm » Bigger Smaller 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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline
Newbie
Karma: 0
Posts: 3
 « Reply #4 on: January 22, 2013, 10:24:02 pm » Bigger Smaller 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
Online
Brattain Member
Karma: 517
Posts: 44532
Seattle, WA USA
 « Reply #5 on: January 23, 2013, 02:39:26 am » Bigger Smaller 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
Brattain Member
Karma: 484
Posts: 30676
Solder is electric glue
 « Reply #6 on: January 23, 2013, 02:46:47 am » Bigger Smaller 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