Page 1 of 1

Mapping function issue

Posted: Tue Mar 18, 2025 8:20 am
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.

Re: Mapping function issue

Posted: Tue Mar 18, 2025 8:31 am
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.

Re: Mapping function issue

Posted: Wed Mar 19, 2025 1:34 pm
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.

Re: Mapping function issue

Posted: Wed Mar 19, 2025 4:00 pm
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.

Re: Mapping function issue

Posted: Fri Mar 21, 2025 5:30 pm
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.

Re: Mapping function issue

Posted: Fri Mar 21, 2025 6:53 pm
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

Re: Mapping function issue

Posted: Fri Mar 21, 2025 8:59 pm
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 2143 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 2140 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.

Re: Mapping function issue

Posted: Tue Apr 01, 2025 8:02 am
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

Re: Mapping function issue

Posted: Fri Apr 18, 2025 4:32 pm
by kramtlas
Hi Martin,
Great Thanks
Do you know when the SIM will be implemented?
Regards