LED Flashing - delay problem

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

Moderator: Benj

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

LED Flashing - delay problem

Post by Frank607 »

I’m trying to make a simple program to let flash some LEDs. I have LED flasher with four channels and must do the following: (all channels must start at the same time – power on)

Code: Select all

CH 1: On 1 min -  Off 3 min
CH 2: Off 1 min - On 1 min - Off 2 min
CH 3: Off 2 min - On 1 min flashing and 90 fpm - Off 1 min 
CH 4: Off 3 min - On 1 min
In my flowchart it is not working and I think this has to do with all the delays what I use. I can’t figure out on how to do this is in a other way.

Attached is my flowchart.
TEST LED Flash 01.fcfx
(14.6 KiB) Downloaded 140 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: LED Flashing - delay problem

Post by mnf »

The delays 'block' so - here you'll have one channel run - then the 2nd...

What I would suggest doing is having a timer interrupt incrementing a timer (for the sake of simplicity here lets say at 1s intervals)

Then have in the main loop:

if (tick < 60) Set Channel1 on, Set Channel2 off.
if (tick > 60) SetChannel1 off
If (tick < 120) Set channel3 off.

etc

Reset the 'tick' to 0 at 4 minutes (240s)

This is a (slightly) simplistic overview of what you need - and actually you'll need a higher resolution 'tick' for the flashing LED (so 90fpm = 60/180 = 1/3 second (90 times on and 90 times off)

I can create a simple Flowchart demonstrating this if it helps...

Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Hi Martin,

Yesterday the forum had a problem to access it.

Yes if you would me than I really appreciate that.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: LED Flashing - delay problem

Post by mnf »

Yes, something odd with the forums (fora?) yesterday - powercut or DDOS???

I'll tray and make an example program this evening...

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: LED Flashing - delay problem

Post by mnf »

Okay - a simple demonstration (works okay in SIM and should work better on hardware - timings may vary in SIM depending on your PCs performance)
led test.fcfx
(19.18 KiB) Downloaded 149 times
Note that I've 'simplified' slightly for the sake of clarity - the timer actually runs at 30.518Hz - so the number of ticks in minute would actually be 1832 not the 1800 I've used here.
I think I got the maths right for the flash...

As an aside - this 'sets' or 'clears the pins many times per second (usually not changing the current state) - if this was undesirable, then set a flag for the LED state and just turn on or off if the state has changed.. I would suggest adding a delay to the loop in main too.


Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Hi Martin,

I really appreciate your help. Today I had time to work with this but then I get a phone call for a job. So maybe tomorrow morning I have time.

I’m sorry for this.

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Hi Martin,

I have tested your flowchart in hardware but what I see is this:
LED1 turns on and stays on. It never goes to the next step for LED2.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: LED Flashing - delay problem

Post by mnf »

Hi Frank,

What hardware are you using? - as per chart above or changed from that?

The program relies on ticks being incremented by the interrupt to mark the passage of time.... So if the interrupt isn't working for some reason (or clock is dramatically slower - say MCU running at 4MHz instead of 40) Does a 1s blinkie run correctly?

Martin

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: LED Flashing - delay problem

Post by medelec35 »

I just had a look at the flowchart.
You will need to go into the interrupt properties (Double left-click on the Interrupt icon) and change Clock Source Select from Transition on T0CKI pin to Internal Clock (CLKO)
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: LED Flashing - delay problem

Post by mnf »

Thanks Martin,

Thought I'd gone with the clock interrupt... Interesting that the simulation worked 🤔

Martin

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: LED Flashing - delay problem

Post by medelec35 »

mnf wrote:
Tue Jun 22, 2021 2:22 pm
Interesting that the simulation worked
This is something I have mentioned before.
The simulation will ignore the interrupt clock source and always treat it as internal.
it has been like that from the early days.
It has caught me out as well.
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

I will try every sugestion tomorrow. Thanks !!

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Hi Martin,

I have make the change in the timer interrupt. It looks much more better now.
( I use the PIC12F629)

Only the flashing LED is “out of control” See video on YouTube

https://youtu.be/TgyXV9eJYTw

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: LED Flashing - delay problem

Post by mnf »

Glad it's (sort of) working for you.

The flashing was a bit odd in simulation too - and I thought it was just timing issues there..

I'll have another play later today - but you'll maybe have it working by then.....

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: LED Flashing - delay problem

Post by mnf »

I think the problem is - the program might execute the main loop multiple times per increment of the timer... (this is less of problem in simulation as it runs a lot slower than the MCU)

One solution is to keep a record of where the 'change' time occurs and only change once...
led test 2.fcfx
(19.58 KiB) Downloaded 84 times
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Hi Martin,

Thanks. But it seems that the modification didn’t help. See video:
https://youtu.be/wnuEeJsAlrU

Frank

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: LED Flashing - delay problem

Post by Benj »

Hello,

How about this, just a few slight changes to simplify the calculations and to change the clock source of the timer interrupt.
led test 2.fcfx
(22.95 KiB) Downloaded 128 times

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Hi Martin,

Thanks. I think there is no space any more in this PIC12629.

Code: Select all

using updated 32-bit floating-point libraries; improved accuracy might increase code size
led test 2.c: 498: (1360) no space for auto/param main@FCL_T_CHANGE
(908) exit status = 1
(908) exit status = 1

Error returned from [xc8.exe]
I will change this SMD chip for a PIC12F1840. (this one I have)

I will do this tomorrow.

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Hi Martin,

I have changed the chip into a PIC12F1840.

The program was running very slow, so I decide to add an OSCCON value in the flowcode.

OSCCON=0x70 is going too fast.
OSCCON=0x60 is going too slow. (One minute is two minutes)

What can I chance more ?
led test 3.fcfx
(19.86 KiB) Downloaded 113 times

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: LED Flashing - delay problem

Post by medelec35 »

Hi Frank.
I have modified your code to make timing more accurate.
First I set the internal OSC speed to 16MHz which is

Code: Select all

OSCCON=0x78;
Note. The oscillator speed of 12F1840 can go up to 32MHz.
If you use the component search and enter intosc, the Internal oscillator helper will be shown and can then be added to your panel.
Within properties of Internal oscillator helper, select the osc speed you want your microcontroller to run at.
After selecting 16000000 the results are:
Int osc.png
Int osc.png (14.5 KiB) Viewed 11183 times
You can just select, copy and paste the code section.
The next step is getting the timing more accurate.
For that, you will require the lowest whole number for the interrupt.
Timer 2 is very handy for that purpose.
If you download the spreadsheet from here
The quickest way to get a result is to enter the correct osc frequency of 16000000.
For the Req Val, if that is not known just enter any random number e.g 50
Select the drop-down arrow for whole No and unselect Blanks
Scroll down until you see the prescaler that matches the highest prescaler of your microcontroller.
I.e
Prescaler.png
Prescaler.png (7.65 KiB) Viewed 11183 times
.
These the result to enter into timer 2 interrupt properties:
Timer2 settings.png
Timer2 settings.png (5.68 KiB) Viewed 11183 times
100 is the lowest whole number which means higher accuracy.
Timer 2 interrupt.png
Timer 2 interrupt.png (38.96 KiB) Viewed 11183 times
I have attached a flowchart for you to try.
Last edited by medelec35 on Mon Jul 12, 2021 11:56 pm, edited 1 time in total.
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Martin,

Thanks for the explanation. You have set the timer to 100Hz, that is the slowest what is possible if I understand it very well. Also in the excel sheet I don’t see any lower value than 100Hz with a Prescaler Rate of 16.

I ask this because the whole programs runs now in some three seconds. So, too fast.

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: LED Flashing - delay problem

Post by medelec35 »

Hi Frank,
Sorry my mistake.
I got distracted and thought I had finished the flowchart when I had not.
Attached is a completed version.
Attachments
led test 4.fcfx
(21.41 KiB) Downloaded 74 times
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: LED Flashing - delay problem

Post by Frank607 »

Ok, this looks beter :-)

The flash is also better but still has some errors.

See this movie:

https://youtu.be/M4aWtjW6UZU

Thanks.

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: LED Flashing - delay problem

Post by medelec35 »

Hi Frank,
I have not modified that apart of the code from adjusting some numbers.
I could have a look in to that part.
I was just concentrating on the timing using Timer2.
How was the timing in minutes?
Martin

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: LED Flashing - delay problem

Post by medelec35 »

Hi, Frank.
I have now modified the code to flash at 90FPM = 1.5Hz
Attachments
led test 5.fcfx
(20.75 KiB) Downloaded 78 times
Martin

Post Reply