how to use single switch for intensity or color control of RGB led

Status
Not open for further replies.

prashanttyagi85

Advanced Member level 4
Joined
May 5, 2011
Messages
106
Helped
33
Reputation
66
Reaction score
33
Trophy points
1,308
Location
india
Activity points
1,982
dear all
please help me
i am working on 3W RGB LED controller. i want to use a single switch for changing the color sequence.
like i used switch statement for different color. but how to change the color using single switch with multiple press
please help me
its urgent!
 

You need some PWM code to set the color. It will run by itself (or under interrupt) if you use the PWM capabilities
on your microcontroller. You also need some software switch debounce. In your main code, When the switch is pressed,
change the PWM value settings and then wait for your debounce period. Loop. Simple.
 

thanks a lot
may i get some sample code for switch bounce
 

Personally I just implement a delay loop (approx 20msec) whenever the switch reads as pressed.
After the 20msec, check the input again. If it still reads as pressed, then assume it is
settled as pressed and perform the action. If it reads as unpressed, then ignore it.
After you've finished the action that the switch-press is supposed to invoke, then check to see
that the switch is unpressed by now. If it is still pressed, loop (preferably delay) until it is unpressed.
Optionally wait 20msec and confirm it is still unpressed.
There are more exotic methods, but they are not warranted for your application.
 
The following attached code demonstrates the use of PWM to control an RGB LED.

The source code is in C, written for the LPC1769 ARM, however you should be able to port it to other MCUs.

project will allow the user to cycle or select a
RGB color to be displayed on the RGB LED on the EA base board.
 

Attachments

  • RGB LED PWM.zip
    49.7 KB · Views: 115
thanks for valuable suggestion. i'll implement it & then discuss

- - - Updated - - -

thats fine but suppose that i have to change my color sequence from red, green, blue, white etc, by one by one press of switch. let by default red color is glowing, know i press switch one time then it will convert to green. or if i'll press switch two times then blue or three times then white & in the same pattern respectively the color should change.
so what should i do for it
 
You can do that by having an array (say)
Code:
#define REDCHAN 0
#define GREENCHAN 1
#define BLUECHAN 2
unsigned char color[N][3]={ {0xff, 0, 0},
                   {0, 0xff, 0},
                   {0, 0, 0xff},
                   {0xff, 0xff, 0}.....};

if you wanted to have N colors. Have as many as you want. The values are the PWM settings for the
red, green and blue channels, e.g. 0xff may be brightest.

Then, just have a variable called desired_col, which you initially set to zero, and increment each time the button is pressed.
Whatever value is in desired_col, use that to set your three PWM channels whenever the button is pressed. i.e.
Code:
pwm_red=color[desired_col][REDCHAN];
pwm_green=color[desired_col][GREENCHAN];
pwm_blue=color[desired_col][BLUECHAN];
There are probably other ways, this seems easiest.
 
dear sir,
i wrote a code here only for using a button switch i connected a switch to PC3 & connect 3 LEDs
to PC1, PD0 & PB6. i am just pressing the switch & wants to make change the LEDs one by one after each press. i know that my code is heavy but i am not getting the exact output whatever i want. please correct my code & help me


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define buttonport PORTC 
#define buttonportpin PINC
#define buttonddr DDRC
#define ledport1 PORTC
#define ledport2 PORTD
#define ledport3 PORTB
#define ledddr1 DDRC
#define ledddr2 DDRD
#define ledddr3 DDRB
#define buttonpin 0
#define ledpin1 1   //d4
#define ledpin2 0   //d2
#define ledpin3 6   //d1
void main()
{
    int i=0,j;
    buttonddr = buttonddr & ~(1<<buttonpin);
    ledddr1 = ledddr1 | (1<<ledpin1);
    ledddr2 = ledddr2 | (1<<ledpin2);
    ledddr3 = ledddr3 | (1<<ledpin3);
    
    while(1)
    {
        ledport1 = ledport1 | (0<<ledpin1);
        ledport2 = ledport2 | (0<<ledpin2);
        ledport3 = ledport3 | (0<<ledpin3);
        if((buttonportpin & (1<<buttonpin))==0)
        {
            _delay_ms(30);
            
            if((buttonportpin & (1<<buttonpin))==0)
                {
                    while (!(buttonportpin & (1<<buttonpin)))
                    {
                        
                        i=1;
                        
                    }
                }
            if(!((buttonportpin & (1<<buttonpin))==0))
            {   
                _delay_ms(1000);
                
                    if((buttonportpin & (1<<buttonpin))==0)
                    {
                        _delay_ms(30);
                        if((buttonportpin & (1<<buttonpin))==0)
 
                        {
                            while (!(buttonportpin & (1<<buttonpin)))
                            {
                                i=i+1;
                                while((buttonportpin & (1<<buttonpin))==0);
                                
                                
                            }
                        }
                    }
                    j=i%4;
                switch(j)
                {
                    case 1:
                    {
                        ledport1 = ledport1 | (1<<1);
                        _delay_ms(3000);
                        ledport1 = ledport1 & (0<<1);
                        break;
                    }
                    case 2:
                    {
                        ledport2 = ledport2 | (1<<0);
                        _delay_ms(3000);
                        ledport2 = ledport2 & (0<<0);
                        break;
                    }
                    case 3:
                    {
                        ledport3 = ledport3 | (1<<6);
                        _delay_ms(3000);
                        ledport3 = ledport3 & (0<<6);
                        break;
                    }
                }           
            }           
        }       
    }
    return 0;
}



- - - Updated - - -

i just want to know the operation of switch(button) how it will convert different stages
 
Last edited by a moderator:
I can't look at code easily when it is unformatted, because I can't mentally keep track of curly bracket count.
Anyway, you need to use PWM, and from a quick look at it, I can't see that you are using PWM.
Your while loop looks like it just sets the outputs all low.
Depending on the microcontroller you're using, you should be able to either directly use
a PWM integrated peripheral in the IC, or at least just the timer. (Basically, read up on PWM, interrupts and
timers in the microcontroller data sheet).
If you can edit the post to use the '#' tag around the code (so that indents are visible), it will be easier to
follow the code, to see if it can be made to work.
 

actually i think that i was unable to send my problem to you till now
i had written my code for RGB colors using PWM with the help of timers. i used timer 0 & timer 1 for generating different combination of three colors. i wrote code for rainbow style, & also for different colors using various functions. i am calling them after 2 or 3 sec each. so my LED is glowing with different colors after each 2 seconds.
but now i want to control it by using a switch (button). as i press that button first time then first function's color should glow if i press it second time then second function's color should glow & so on. my above code is just only for button's working & not for PWM.
 
If you can edit the post to use the '#' tag around the code (so that indents are visible), it will be easier to
follow the code, to see if it can be made to work.

I went ahead and added C syntax tags to prashanttyagi85 code and sent him a link with a tutorial on how to use CODE and SYNTAX tags.

BigDog
 

i can't get your view of # tag around the code
please tell me what should i have to do for it
thanks

- - - Updated - - -

sorry & thanks sir for helping me
 

I've added SYNTAX tags which provided a nicer format then the CODE tags. You can click on [expand] and the code will be displayed in its entirety rather than using the scroll bars.

Take a look at the link of the tutorial on tags use.

BigDog
 
Not clear how you're turning off a pin with the | (OR) command.
Personally, I do this (ignore the actual names, this is just an example from some code):
Code:
// Port A
#define CHARGE_EN 0x04
#define VIDEO_EN 0x08
#define DRV_EN 0x10
#define EXTLED 0x40
#define BOARDLED 0x80

And then use defines to make the code readable:
Code:
// outputs
#define BOARDLED_ON PORTA |= BOARDLED
#define BOARDLED_OFF PORTA &= ~BOARDLED
#define DRV_ON PORTA |= DRV_EN
#define DRV_OFF PORTA &= ~DRV_EN
#define CHARGE_ON PORTA |= CHARGE_EN
#define CHARGE_OFF PORTA &= ~CHARGE_EN

Then, in my code, I can do something like this (just an example - better to use a timer):
Code:
while(1)
{
  BOARDLED_ON;
  delay_ms(100);
  BOARDLED_OFF;
  delay_ms(100);
}
It makes it really clear and readable.
 

Are you testing the design using physical hardware or simulation?

What is the model number of the RGB LED? Do you have a datasheet for it, if so can you upload it?

BigDog
 

i am testing it on hardware only but it is not working properly when i press the particular button it reacts randomly

- - - Updated - - -

please don't consider the switch statements. just only consider the button' s working & how should code be written for working button properly
 

First off, you need to change how you're switching pins off. You can't do that with an '|' (OR) command, as I mentioned.
That may be part of the problem.
Also, your while() loop is not doing anything except this:
Code:
while(1)
    {
        ledport1 = ledport1 | (0<<ledpin1);
        ledport2 = ledport2 | (0<<ledpin2);
        ledport3 = ledport3 | (0<<ledpin3);
if no button is pressed.
Can you make some changes to the code and try again? Use the method I described to turn off pins. It also makes
your code more legible.
 

prashant bhai try kro
* individual leg for each colour _____easy ok
* every pin need pwm for individual brightness __________tricky (interrupt with only one timer) google for this techniqqq
* also make x variable increment to see what the time for next throttle action_______(secret open)
* check colours for your code
* do what u prefer _______have to enjoy
*******maine aisey hi 25 effect ka code bnaya tha 6 months b4 for a client atmega8 k liye
 

thanks rajat bhai
i have written the code for RGB led using pwm. each leg is connected with individual pin of timer 0 & 1 (using ocr0, ocr1a, ocr1b). & it is working ok. but i just want to change the color sequence using a button switch. i have written code for each color using diffrent functions. now i just want to press the button & change the color. i just want to know that how a switch will react after each press. please send me a code for working of switch
thanks
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…