Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Coding help - how should I attempt to code this rgb system?

Status
Not open for further replies.

Spadez

New Member
Hi.

I've got an rgb led. I also have a load of switched attached to my PIC. Each switch has a colour associated with it.

The colours will be:

Red. : 100% Red, 0% green, 0% blue

Green : 0% Red, 100% green, 0% blue

Blue : 0% Red, 0% green, 100% blue

Yellow : 50% Red, 50% green, 0% blue

Pink : 50% Red, 0% green, 50% blue

Turquoise : 0% Red, 50% green, 50% blue

When I turn on the red switch i want the rgb led to shine red. Then when i turn off the red switch, and turn on the blue switch I want it to shine blue. Not tricky right?

Here's where it gets tricky, if i turn on the red switch and the blue switch, I want it to shine pink because both of this colours are effectively "On". If i were to turn on the green switch then I should get a white light since the red green and blue switches are on.

Later on this project will get even mire complicated where there will be switches for other colours like turquoise.

The basic principle is that the rgb displays and mixes light depending on what colour switches are turned on.

I understand that PWM will be used to change the colour of the lights. However I'm stuck on two things:

1. How can I work out what colour should be displayed based on which colour switches are on.
2. How can i change this colour into an RGB percentage, therefore what percentage should red be on, what percentage should green be on and what percentage should blue be on.

Sorry for the long post but I don't know where to start with this.

James
 
For the mix colours, i.e. purple, turquoise and yellow - (let's take purple, it's not pink, as an example) you won't really be doing 50% red and 50% blue, will you? It'll be better to just set the RED pin and the BLUE pin high... then you get purple, and the brightest purple to boot - not 50% red + 50% blue, but 100% red and 100% blue.

The same for yellow and... cyan.

And white too... all three colour pins (read from switches if you want) set simply to high.

It's only when you get to the other colours... your pinks, oranges and all those in-between that you will need to do PWM, as then you definitely will be setting the duty-cycle of your individual LEDs as you can't make an orange with binary low / high pins.

I've done stuff like this in JAL (not with the switches, I'm currently at a stage where I'm reading a potentiometer to control the speed of programmatic fades).

Working out what colour is to be displayed based on switch positions sounds like some simply if then elsif logic to me - it'd certainly get you there. Software PWM procedures can be pretty straightforward (in JAL)... I can't claim any knowledge of other languages.

Not being assumptive, but if you're not yet confident in coding, JAL might well be a good place to start. I can't see what language you intend to use, nor what PIC you have, but I have had success doing this kind of stuff with both the 16F628a and 12F683.

JAL pseudocode (using aliases, if you were just talking about the three switch pins controlling the three LED pins would be as simple as):

Code:
forever loop
  RedLED = RedLEDSwitch
  BlueLED = BlueLEDSwitch
  GreenLED = GreenLEDSwitch
end loop
 
Last edited:
Hi,

Thank you for both your inputs. I'm constrained to a 16f628A and assembly code.

The reason I wanted to do 50% red and 50% blue for purple is so that it was the same brightness as red, which would be 100% red. I thought if I have two LEDs on 100% it would be twice as bright as one LED on 100%.

If i look at means:

Red switch:
100% Red, 0% green, 0% blue

Pink switch:
50% Red, 0% green, 50% blue

R = (100% + 50%) / 2 = 75%
G = (0% + 0%) / 2 = 0%
B = (0% + 50%) / 2 = 25%

Would something like this work? Im not sure if it will work for more than 2 switches.
 
Last edited:
Ok. I'm now having a hard time working out how I can convert this percentage to a pwm. It would be better to not have a set routine for each combination of pwm since there could be a huge number of them.
 
Build up a truth table:
Code:
Input    Output
R G B   R   G   B
0 0 0   0   0   0
0 0 1   0   0   1
0 1 0   0   1   0
0 1 1   0  1/2 1/2
1 0 0   1   0   0
1 0 1  1/2  0  1/2
1 1 0  1/2 1/2  0
1 1 1
Whenever you have one switch closed, the specified output is 100%. Whenever you have two switches closed, the output is 50%. The intensity is the parity of the number of closed switches. What do you do with three switches closed?
 
I would most likely have 50% on red, green and blue when the input is 111. I will have to check how the intensity varied between the different options as I want to keep them fairly equal.

Once I have built up a truth table, how should I proceed from there? Would it be a case of querying the intputs using BTF and creating a kind tree of routes. I can see this creating quite bulky code but it looks like its the only way to do this.
 
It shouldn't be too difficult, just 8 states. scan the switches use the switch state as a offset to jump to a location that loads the 3 values into your PWM registers. Then jump to the PWM output routine. after a few loops through that routine jump back to the switch scan routine.

Would it be a case of querying the intputs using BTF
I don't know what BTF is ?
 
Last edited:
Byte Managed look up

I think u ought to have a bit more options in terms of intensity for better color mix options or possible fade between colors.

That being so here's one way:

1) Allocate 1 byte of 8 bits to dictate 0%, 25%,50% or 100% of the primary R,G,B colors.
2) That requires 2 bits per primary color = 6 bits.
3) Allocate the last 2 bits as an overall RGB 'dimmer' intensity of 0,25,50, or 100% intensity.

......DIM |Red |Green|Blue
bits 7 ,6 |5 ,4 |3, 2 |1, 0
......0 ,0 |0 ,0 |0, 0 |0, 0 => 0% PWM DC
......0 ,1 |0 ,1 |0, 1 |0, 1 =>25% PWM DC
......1 ,0 |1 ,0 |1, 0 |1, 0 =>50% PWM DC
......1 ,1 |1 ,1 |1, 1 |1, 1 =>100% PWM DC

Now using bitmasking or integer math u can individually control each color & Intensity .
eg: If u set the byte to max intensity => set its 6,7 the value = 128 +64 = 192.
Now this still won't show anything until u define the intensity of a Primary color above 0.
So now u can run a slow loop incrementing the byte by 1 from the base 192 value, when the byte reaches 255 u start decrementing the byte back to 192. This will give a nice effect of the colors brightening and blending one into the other.

eg #2. If u want to 'just" have simple color selection and no blending effects. All u need to do is have a lookup table of byte values for the particular color mix and intensity u want. Your table will have a maximum of 256 Values for all possible combinations that can be achieved.
 
Last edited:
I have attempted to make a flowchart with how the system will work.

I have put particular attention on the colour mixing based on the RFID tags.

Would anyone be willing to look at it and see if they agree with the route I am taking with it?

As spoken of previously, the system will work by allocating a % colour to a tag, for example the red tag is 100% Red, 0% green, 0 % blue and the yellow tag is 50% red, 50% green, 0% blue. The mean average for all of these are taken the read tags and then this is turned into a PWM signal.

James
 

Attachments

  • electronics.pdf
    159.4 KB · Views: 154
I have attempted to make a flowchart with how the system will work.

I have put particular attention on the colour mixing based on the RFID tags.

Would anyone be willing to look at it and see if they agree with the route I am taking with it?

As spoken of previously, the system will work by allocating a % colour to a tag, for example the red tag is 100% Red, 0% green, 0 % blue and the yellow tag is 50% red, 50% green, 0% blue. The mean average for all of these are taken the read tags and then this is turned into a PWM signal.

James

What RFID tags? I've read through the entire thread and haven't found any mention of RFID tags until now.
 
Appologies, I left them out of the original post in order to simplify the question.

The colours will be simply toggled on and off via RFID tags (instead of the switches mentioned previously)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top