Fading LED

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Fading LED

Post by mimiyum »

I tried the PWM example for fading LED and played around with it. It works fine as it is but I am having trouble fading another LED after the first one.

What I am trying to do is after the end of the Interrupt, I tried to disable it and start another interrupt calling another macro. Flowcode simulation shows it's working but when I download the program to the PIC using the HP-488 pic devt board, only the first LED fades.

Attached is my flowcode program. Please see what is wrong with it.

Thanks!
Attachments
LED Fade aug 31.fcf
(11 KiB) Downloaded 526 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Fading LED

Post by Benj »

Hello

I have made some minor modifications to your program and both LEDs should be working as PWM now. Also the program should give a good indication of how to add more PWM output channels.
Attachments
LED Fade aug 31.fcf
(14.5 KiB) Downloaded 639 times

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Thanks Benj!

I get it now! I understand how it works. I am able to edit it and add more LEDs and make them fade in and out! I just love it!

To make the brightening or fading smoother, I make brightness increase or decrease between 0 and 255 by increments of 15.

One last question...if I were to keep one LED off (steady at 0) and the other one getting brighter (from 0 going up to 255) I noticed that the LED remains dim. Is there a way to keep this totally off?

Thanks again!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Fading LED

Post by Benj »

Hello

Please can you try the attached program. It should allow for the LED to be turned off (PWM of 0) without still glowing slightly. Before the LED was getting switched on at index 0 but now the LED should remain off for index 0 if the preset pwm control is configured to 0.
Attachments
LED Fade 03-Sep.fcf
(14.5 KiB) Downloaded 539 times

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Thanks again Benj!

It's working great!

Sorry for sounding newbie...I really am...I can't figure out how you "preset the PWM control to 0" Where was that done?

Thanks for your patience!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Fading LED

Post by Benj »

Hello

Sorry by presetting the LED brightness I meant giving the LED PWM variables values

Eg
LED_Brightness = 0
LED_Brightness2 = 255

Before the decision in the timer0 interrupt service routine was making this decision.

LED_Count > LED_Brightness //When LED brightness and LED count are 0 the decision returns false which switches on the LED.

It is now making this decision.

LED_Count >= LED_Brightness //When LED brightness and LED count are 0 the decision returns true which switches off the LED.

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

wow Benj, that's really funny! :lol: i searched everywhere coz I didn't notice the "=" sign!

Flowcode customer support is SUPER!

Thanks again!

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Hello!

Back to the continuing saga of the Fading LEDs...

As we go along, I, together with many other members of this forum are learning a lot. With such interest to learn the many uses of the components of flowcode, I have a new idea. :idea:

I am now thinking that maybe there is a way to use data tables as the source of info for the brightness level.

Lets say the data table (in excel) contains the brightness level per LED and length of time it should be at that level, for example:
Step 1: LED1 Brightness = 0 Time = 100 ms; LED2 Brightness = 100 Time = 100ms
Step 2: LED1 Brightness = 10 Time = 150ms; LED2 Brightness = 50 Time = 50 ms
Step 3: LED1 Brightness = 0 Time = 100 ms; LED2 Brightness = 25 Time = 100ms
and so on...

I imagine this would shorten the program if we already have a long sequence of steps to perform.

Can you give us an example on how this could be done?

Thanks!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Fading LED

Post by Benj »

Hello

It would actually be fairly hard to implement what you are suggesting. Basically the program would have to scan through all the delay values for that particular step, find the shortest delay, perform the tasks that all start at once, perform the delay, perform the specific task and then delay for the other tasks that were started. More problems enter the loop if things need to stop and start while other things are still running.

Its probably going to be a lot easier to do it by hand if you are going to have a fixed sequence of steps.

Another possibility is this.

LED0 - 50%
LED1 - 25%
LED2 - 80%
Wait 50ms
LED1 - 18%
Wait 50ms
LED0 - 50%
LED2 - 80%
Wait 350ms
LED2 - 0%
etc

This could be done via a spreadsheet file fairly easily but it could also be done by Flowcode fairly easily so it seems pointless to add the extra steps. I could be wrong here though.

Finally the third and most efficient way would be to create n+1 arrays where n is the number of LEDs. The arrays must be large enouth to handle all states of the LED.

eg
a[0] = 50
b[0] = 20
c[0] = 6
delay[0] = 100

a[1] = 50
b[1] = 24
c[1] = 26
delay[1] = 500

then you could fill up the arrays at the start of your program, or define them in a C file etc. then just have a main loop to step through the arrays and then wait the correct amount of time.

C code array initialisation -

Code: Select all

char a[] = {0,5,35,240,45,...,0};

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Thank you Ben,

Still sound Greek to me but let me digest it first so I can really understand your reply.

Thanks again!

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Hello!

Still another question while I'm still digesting your message...

I realize that no matter what kind of setting I use on the pic16f88, the LEDs still do not fade smoothly because it is set on RC. When I program it with target chip as 16f88 lvp-xtal, I use the following settings on the PIC devt board:
S2- XTAL
Jumpers are on LVP

I am using USB Power supply.

All this works well when the progam is run on the pic devt board.

When I transfer the IC to my circuit following the PIC16f88 data sheet using a 18.432 MHz Crystal Oscillator, 15pF on both C1 and C2, 2.2M Rs, the LEDs don't even light up.

Did I miss something here? :oops:

proton
Flowcode V4 User
Posts: 50
Joined: Sun Feb 03, 2008 10:14 pm
Has thanked: 1 time
Contact:

Re: Fading LED

Post by proton »

Have you taken MCLR (pin 4) high

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Yes. Pin 4 has a 1k to Pin 5 (+)

Isn't this correct?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Fading LED

Post by Benj »

Hello

Check the settings in Flowcode under Chip -> Configure. you will need to change the oscillator type to XTAL (simple mode) or HS (expert mode).

1K resistor should be fine for your MCLR pin. Ive also had problems with non ceramic caps on the oscillator in the past.

Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

Re: Fading LED

Post by Mark »

I suggest writing a simple test routine to flash an LED or something until you are sure the oscillator is working OK. At high frequencies the length of the leads to the crystal can be enough to give the required vcapacitance and you can sometimes get an oscillator that is not working, working by omitting the capacitors. There is a Microchip Application note on the capacitance values and in practice the devices seem tolerant to over/under driving, as I think the issue that capacitors are supposed to cure is termed.

Regards,

Mark
Go with the Flow.

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Thanks. I'll look into those. A while ago, we discovered that the oscillator is not working at all. We still don't know why. I'm having that checked. Will get back to you once I've checked on those.

Regards!

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Fading LED

Post by mimiyum »

Hello!

We tried different crystals and finally got the 4MHz and 8MHz to work. We have to find the right combination. We noticed that the 4MHz causes the LEDs to flicker more than the 8MHz. I guess if we could make the 18MHz or 19MHz crystals work we would finally lose that flicker of the LEDs. The flicker usually occurs at the lower end when the LEDs are made to fade out towards 0.

I will update you on further experiments.

Regards
Mimiyum

Stanga
Posts: 35
Joined: Sat Aug 07, 2010 10:17 am
Been thanked: 2 times
Contact:

Re: Fading LED

Post by Stanga »

Is anyone able to convert the LED Fade 3 example to Flowcode 4? I would like to open and have a look at the code routine, but don't have V3 to do that. ANy help would be appreciated.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Fading LED

Post by Spanish_dude »

You should be able to open Flowcode v3 programs with Flowcode v4.

Stanga
Posts: 35
Joined: Sat Aug 07, 2010 10:17 am
Been thanked: 2 times
Contact:

Re: Fading LED

Post by Stanga »

It won't open in 4.5 for me, which is why I asked.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Fading LED

Post by medelec35 »

Hi Stanga,
The reason why it wont open in V4.5 is because the target device file with fcd extension was created in Flowcode3 to set configuration for LVP (Low Voltage Programming).
The same fcd has not made it into FC4, Hence reason for the error that you can see.
Here is the same flowchart converted to V4.

Martin
Attachments
LED Fade 03-Sep FCV4.fcf
(16 KiB) Downloaded 310 times
Martin

Stanga
Posts: 35
Joined: Sat Aug 07, 2010 10:17 am
Been thanked: 2 times
Contact:

Re: Fading LED

Post by Stanga »

Thanks Martin :) .

Post Reply