Mapping function issue

For general Flowcode discussion that does not belong in the other sections.
Post Reply
kramtlas
Posts: 21
http://meble-kuchenne.info.pl
Joined: Mon Dec 14, 2020 7:22 am
Has thanked: 5 times
Been thanked: 1 time

Mapping function issue

Post by kramtlas »

Hi, I am trying to use the "Map function" to control a PWM output from analog input ( pot ).
There is an example in the Wiki which does this successfully by mapping a pot ( 0,255 ) to a PWM output which also has the range of 255 although the output of the range set is modified to ( 50,150 ).
However, if I use the range of 255 for the PWM Output in my code it does not function correctly! I changed the scaling of the PWM output to reflect a 3.3V ref instead of a 5V ref although in the pot settings it is set to 5V and it appears to work ok.
Although in my "IF" statement I had to revert to a scaling of 255.
I noticed in your example the ref is VDD and in my code it is AVCC with no option for VDD.
I am using an Arduino Uno R3 SMD and the latest version of 10.
I have attached a screenshot of the code and the Flowcode version and the code for you to try.
I would welcome any assistance to resolve this issue.
Attachments
Wheel speed sim motor control.fcfx
(15.41 KiB) Downloaded 12 times
2025-03-18_07h46_56.jpg
2025-03-18_07h46_56.jpg (30.14 KiB) Viewed 2708 times
2025-03-18_07h32_35.jpg
2025-03-18_07h32_35.jpg (104.75 KiB) Viewed 2708 times

medelec35
Matrix Staff
Posts: 1913
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 619 times
Been thanked: 644 times

Re: Mapping function issue

Post by medelec35 »

Hello.
You are using the Potentiometer GetByte() function , which only outputs 0 to 255
However, within the Map function for the in values are using 0 to 1023.
Since the maximum value of in will only be 255, the output will only be a quarter of the expected value.
You will need to use MapInt() instead.
Martin

kramtlas
Posts: 21
Joined: Mon Dec 14, 2020 7:22 am
Has thanked: 5 times
Been thanked: 1 time

Re: Mapping function issue

Post by kramtlas »

Hi Martin,
Thanks, I have now modified the code to use ( INT ) instead of ( Byte ) to make use of the full resolution ( 1023 ) for the pot.
In SIM mode the PWM Duty is displaying 100% Duty @ position 1 on the pot instead of 50% Duty @ position 2.5 on the pot and the Freq is set to 5Khz.

But, when the outputs are measured using a scope the PWM Duty is correct @ 50% Duty when the pot is turned halfway but, the Freq is measured @ 491Hz.
I adjusted the Freq to 100Hz and the measured value was 30.69Hz.
Can you explain this discrepancy between SIM and real values?

I have attached screenshots and code for reference.
I hope you can help me resolve this issue.
Attachments
2025-03-19_12h54_02.jpg
2025-03-19_12h54_02.jpg (127.67 KiB) Viewed 2566 times
Wheel speed sim motor control_Forum_minus correction.fcfx
(15.63 KiB) Downloaded 11 times
2025-03-19_13h24_47.jpg
2025-03-19_13h24_47.jpg (90.1 KiB) Viewed 2566 times

medelec35
Matrix Staff
Posts: 1913
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 619 times
Been thanked: 644 times

Re: Mapping function issue

Post by medelec35 »

Hello.
If you look at the Period Overflow within the PWM properties that is the value when PWM will be at 100% when using the SetDutyCycle function.
Therefore 50% duty = SetDuty cycle of 49/2 = 24

If you want more of a range then you need to use SetDutyCycle10Bit, then 100% duty is SetDutyCycle10Bit of about 49*4 = 196.
50% duty is about (49*4)/2 = 98.

However, I can see what the cause of the confusion is.
There is a bug within the SetDutyCycle10Bit simulation only as it was acting like it was the SetDutyCycle function.
I have fixed it, but there is still an issue of embedded does not match simulation as you correctly mentioned the frequency
I will get it fixed ASAP.
Martin

medelec35
Matrix Staff
Posts: 1913
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 619 times
Been thanked: 644 times

Re: Mapping function issue

Post by medelec35 »

Hello.
I have had a go at redesigning the PWM for AVR.
The code I have allows you to directly set the frequency (PB0 to PB5 = 0.24Hz to 158KHz, PD0 to PD7 = 244Hz to 158KHz) and duty cycle from 0 to 100% in 1% steps.
My Plan is to implement the code within the PWM component.
If you require PWM urgently I can upload a demo file with working PWM.
Simulation won't work until code is added to the PWM CAL.
Martin

kramtlas
Posts: 21
Joined: Mon Dec 14, 2020 7:22 am
Has thanked: 5 times
Been thanked: 1 time

Re: Mapping function issue

Post by kramtlas »

Hi Martin,
Thanks again for your reply.
Could I have the demo file please and when do you think the changes will be implemented for update?
Regards

medelec35
Matrix Staff
Posts: 1913
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 619 times
Been thanked: 644 times

Re: Mapping function issue

Post by medelec35 »

Attached is a demo for PWM frequency of 120KHz and duty can be 0 to 100.
Its very easy to change the frequency and duty on the fly, as I set up the c code as functions.
PWM Uno1.png
PWM Uno1.png (24.4 KiB) Viewed 2142 times
The pin used is B2, that can easily be changed in the C code.
The Code is annotated, so you can lean how it works.
For example:
PWM Uno2.png
PWM Uno2.png (41.7 KiB) Viewed 2139 times
Can you let me know if you like the PWM functionality or not.
I'm referring to the duty direct set from 0 to 100
Note: Simulation won't work as that will be done at a later date.
I plan to start on the changes next week.

I have moved this topic from the Projects forum, as that section is intended for members to share their completed, working projects with the community.
Attachments
Uno PWM example..fcfx
(19.34 KiB) Downloaded 23 times
Martin

kramtlas
Posts: 21
Joined: Mon Dec 14, 2020 7:22 am
Has thanked: 5 times
Been thanked: 1 time

Re: Mapping function issue

Post by kramtlas »

Hi Martin,
Thanks for your detailed response.
I haven't had time to play with the code yet but, I will later in the week.
Regards

kramtlas
Posts: 21
Joined: Mon Dec 14, 2020 7:22 am
Has thanked: 5 times
Been thanked: 1 time

Re: Mapping function issue

Post by kramtlas »

Hi Martin,
Great Thanks
Do you know when the SIM will be implemented?
Regards

Post Reply