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

Status
Not open for further replies.
ok
if i want to get someone's complete code then why i took so much time for this project?
when i got the link at earlier phase !!
i will try it again & agian .
& only if i get the error then i'll post the error

thank you very much
 

dear sir
i have completed the switch using interrupt. its working fine with one switch.
but know i am facing one new problem. initial when i power on the device it doesn't start from last location. say suppose that i switch off the power at red sequence then again when i power on it, it would not start from red sequence.
what should i do for start it from last power off situation ??
 

It depends on what you mean by "switch off the power". If you mean physically disconnect the power supply, then yes, you would expect the circuit to reset. You didn't say it was a requirement. There is no non-volatile memory in use.
If you mean keeping the switch pressed to set the LED index to zero, then the code could be modified; you need to look at where the code sets the index to zero, and modify appropriately. Hint: just turn the LED off
at that point, and keep index where it is. It should be a trivial change.
 

thanks
i was waiting for you
i mean by switch off the power, as physically disconnect the power supply.
i have seen a product same as we are trying, in that this facility was available.
as we switch off the supply and make it on again then it starts from that sequence at which it made off!!
 

Typically there are a few ways this can be implemented. One way is to have some battery backed-up RAM ic. Another is to write the state to EEPROM (either internal to the microcontroller if it has it, or to an external EEPROM ic). You'd need to write a function to read/write to whichever device you use. I think you'll find lots of threads on EEPROM if you search for them. You may find it was actually not intended behavior to maintain the color state (it sounds a very useless function to me, for the scenario that someone disconnects power). There may just be sufficient charge on the power supply capacitor to maintain the power to the microcontroller for a period of time (e.g. a few tens of seconds, or maybe much longer). If that's all you want, then just use a large capacitor on the power supply rail.
 

ok
i'll find it & try to get it
but if not then again coming back to you

- - - Updated - - -

i am not getting any thread which can explain how to use eeprom
 

dear all
this is my code for controlling rgb led with the help of atmega16 using interrupt (INT0).


Code:
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>



#define read_eeprom_byte(address) eeprom_read_byte ((const uint8_t*)address)
#define write_eeprom_byte(address,value) eeprom_write_byte ((uint8_t*)address,(uint8_t)value)
unsigned char l=0,m=1;
unsigned char i,j,k;
uint8_t EEMEM eeprombyte=1;
int main()

{
	
	
	DDRB = 0x08;
	DDRD = 0x30;
	
		OCR0 = 255;   //blue  pb3
 		OCR1AH = 00;
		OCR1AL = 255;  //red  pd5
		OCR1BH = 00;
		OCR1BL = 255;  //green  pd4
		TCCR0 = 0x63;
		TCCR1B = 0x03;
		TCCR1A = 0xA1;
	


	PORTD = 0x08;	//make the PD3 input pin
	GICR = (1<<INT0);
	sei();
	

	
	while(1);
	return 0;
}

ISR(INT0_vect)
{
	_delay_ms(100);
	if((PIND & (1<<3))==0)	//check that the intrupt is activated or not
	{
	l=read_eeprom_byte(eeprombyte);	//store the eeprom data into variable l
	l++;
	write_eeprom_byte(eeprombyte,l);
	if(l==4)	//as we have 4 cases so when we press key again then it will 
							//go to case 1 autometically 
			write_eeprom_byte(eeprombyte,1);    // save 1 to eeprom variable
	switch(l)
	{
		case 1:
		{			
			{
				blue();
				_delay_ms(200);				
			}
			break;
		}
		case 2:
		{		
			{				
				green();
				_delay_ms(200);
			}
			break;
		}
		case 3:
		{			
			{				
				red();
				_delay_ms(200);	
			}
			break;
		}
		case 4:
		{			
			{				
				white();
				_delay_ms(200);							
			}
			break;
		}
	}
								
	}
}

white(void)
{
	char i=0;
	OCR0 = i;
	OCR1AL = i;
	OCR1BL = i;
}

red(void)
{
	char i=0;
	OCR0 = 255;
	OCR1AL = i;
	OCR1BL = 255;	
}

blue(void)
{
	char i=20;
	OCR0 = i;
	OCR1AL = 255;
	OCR1BL = 255;
}

green(void)
{
	char i=20;
	OCR0 = 255;
	OCR1AL = 255;
	OCR1BL = i;
}
voilet(void)
{
	char i=0;
	OCR0 = i;
	OCR1AL = i;
	OCR1BL = 255;
}
here i made some functions for different colors.
i call them using switch-case.
i just want that let initially first time i press interrupt button the value of l will go to 1, blue will glow.
again press then l will go to 2 & also save this value to eeprom, green will glow. and so on...
i used eeprom here for saving the current value of variable "l", as if power has gone then it should again start from that location at which it gone to off condition.

but when i run this program,
it goes upto l=4 very well.
ialso made the power off for checking it & it was working ok.
but when it reach at l=4, i. e. at white color & then i press the intrupt button but it is not changing the state from l=4 to 1 i.e. white to blue color
please help me to investigate the actual error.
 
ut it is not changing the state from l=4 to 1
I couldn't see anywhere in the code where you are setting l to 1 or zero after reaching 4. I don't think you've implemented any code
to do that. Maybe I missed it, but I couldn't see it.

Also, (something you'll learn from experience over time), always think twice before you test for an exact value. e.g.
if (l==4). You should always consider the case that someone may change the code badly in future (if you
forget what you have done a year from now for example) and l may be a higher value.
So, it's better to use if (l>=4) as a general rule.
 
ok thanks for increasing my knowledge.
i had given the bold statement for it in ISR.
please view it & suggest me about my problem's solution

Code:
ISR(INT0_vect)
{
	_delay_ms(100);
	if((PIND & (1<<3))==0)	//check that the intrupt is activated or not
	{
	l=read_eeprom_byte(eeprombyte);	//store the eeprom data into variable l
	l++;
	write_eeprom_byte(eeprombyte,l);
	[B]if(l>=4)	                  //as we have 4 cases so when we 
                                          //press key again then it will 
	write_eeprom_byte(eeprombyte,1); 	//go to case 1 autometically 
			                                      // save 1 to eeprom variable
[/B]

- - - Updated - - -

i have changed (l==4) to (l>=4) in this part of code
 
I'm guessing write_eeprom_byte is writing a byte to eeprom of course, but that won't reset your variable l to zero or 1.
If you want l to loop back to 1, then you're missing the line to set it to 1 (i.e. a line saying l=1 within your if statement).
 

dear sky_123 sir
with the help & valuable suggestions of you people, i have completed it with using functions
but it is a very long program.
an example of a function is given as


Code:
void yellow(char val)
{
	char b,r,g;
	b=255;
	r=g= 255-50*val;
	OCR0 = b;
	OCR1AL = r;
	OCR1BL = g;

}

Code:
yellow(value);
						if(((PIND & (1<<2))==0))
					{
						_delay_ms(20);
						goto sp;
					}
						_delay_ms(speed);
						if(((PIND & (1<<3))==0))
						{
							_delay_ms(20);
							break;
						}
						_delay_ms(speed);

Code:
sp:									//  speed
		{	
			while((PIND & 0x04) == 0)
			{
				_delay_ms(30);
				if((PIND & 0x04) == 0)
				{
					a=a+1;
				
					
					if(a>=10)
						a=0;
					
					write_eeprom_byte(&temp1,a);
					break;
				}

			}
			a=read_eeprom_byte(&temp1);
			speed=a*100;
			speedrainbow=a*5;
		}
in above code 'value' variable is for intensity control
while 'speed' variable is used for controlling the delay time for it
in this code i am changing the speed of changing the colors, intensity of light & mode. here are three modes
one for setting a single color (red or green or white etc)(in it we can change the intensity of light)
other for each color will change with some delay time(it can be change with one button)
last mode is rainbow mode (in it we can control speed of changing the color)

as you suggest me in starting for using array
so please tell me that how can use array in place of function in it?
& can vary these all things (mode, speed & intensity)
is it possible ?
if so then please tell me how it have to be done?


- - - Updated - - -
 

I'm not sure what you're asking of me. You say the program works but it is very long? Is that a requirement?
If it is, then yes, you should be using arrays with colors, rather than specific functions like 'yellow()' which was
something you did on your own enterprise (I did say to use arrays).
Learn to reuse code with arrays, functions and parameters, and then look to see in your software where you can optimize.
I unfortunately don't have the time to inspect your snippets of code to see where you can further optimize, but
as a tip be prepared to spot areas in your code where you could optimize for size by rewriting code so that it is reusable
in your software.
 

thanks for your reply as you are so busy.
but i can't get that what you want to say.
i'll try to make it after reading array.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…