Try to make an HPWM signal with Arduino Uno.
In the example program the signal runs from 0 to 255 and then again from 255 to 0.
But he doesn't carry out the last assignment? So going from 255 to 0 doesn't seem to work. I see the LED slowly increasing after which it should actually slowly decrease again.
Just for the sake of completeness, the LED goes up and at the maximum the LED goes out and then goes up again.
What could be the problem? I don't see any error in the code so far.
#chip mega328p, 16'#1'Set the CLOCK Source as AVRTC0 relates to PWM2#define AVRTC0'#2'Set the AVR PWM Channel as enabled by defining this constants. AVRCHAN2 equates OC0B#define AVRCHAN2'#3'Set the PWM pin to output mode. OC0B on assigned to portD.0DIRPORTD.5outDimBrightasbyte'Main code'freq 40khz'variable duty from 0% to 100%do'Turn up brightness over 2.5 secondsForBright=0to255'#4 - Call the method with the correct PWM channelHPWM2,40,Brightwait15msnextwait1s'Turn down brightness over 2.5 secondsForBright=255to0'#4 - Call the method with the correct PWM channelHPWM2,40,Brightwait15msnextwait1sloopend
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OK, V99.01 not handling the for/next correctly for dimming led .
The endpoint of "0" not being compared, but instead "255" :-(!
So have to wait till rollover to repeat first loop.
P.S. just a guess that the compiler is grabbing wrong parameter or excluded from using "0"?
Last edit: kent_twt4 2022-02-17
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I don't want to add define for all my dozens and dozens of programs. Why not make it so
'#Define USENEWFORNEXT' So I don't have to go back and change them all???
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
For newbees the for-next is expected to work, and, it is expected to work as programmed (it was not in the past)
Newbees may not know about that do loop.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
True, for-next is more common, do loop was not in zx basic or anything else at the time , when did it become a basic command? It's not used in any 80's machines that ran basic as the os.
Thing is, is it faster than do-loop? I guess no-one tested.
How to. A for next inside an another for next and the same for a do loop inside another do loop and use the millis function at the start and end?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Try to make an HPWM signal with Arduino Uno.
In the example program the signal runs from 0 to 255 and then again from 255 to 0.
But he doesn't carry out the last assignment? So going from 255 to 0 doesn't seem to work. I see the LED slowly increasing after which it should actually slowly decrease again.
Just for the sake of completeness, the LED goes up and at the maximum the LED goes out and then goes up again.
What could be the problem? I don't see any error in the code so far.
Please use the For Next loop as follows:
For Bright = 255 to 0 step -1
This is the resolution.
What version of the compiler? open the asm file, look at the first line and post to this chat.
Copied program, tried out with UNO, and works as intended. Compiled with 98.07 rc19
P.S. If you could post and attach the .asm file, I could check against rc19 for differences.
Last edit: kent_twt4 2022-02-17
@kent_twt4 Can you try the latest build ?
Version 0.99.01 1/29/2022 from downloads?
OK, V99.01 not handling the for/next correctly for dimming led .
The endpoint of "0" not being compared, but instead "255" :-(!
So have to wait till rollover to repeat first loop.
P.S. just a guess that the compiler is grabbing wrong parameter or excluded from using "0"?
Last edit: kent_twt4 2022-02-17
Who tested the for-next loop.... :-)
Try adding
#Define USELEGACYFORNEXT
to the code - does this resolve? It is a workaround NOT a fix.I don't want to add define for all my dozens and dozens of programs. Why not make it so
'#Define USENEWFORNEXT' So I don't have to go back and change them all???
The reason to fix for-next was to resolve the huge number of issues with the existing code. It was riddled with bugs and caused many issues.
This issue came about because of the lack of testing. I tested 1000s of variants and somehow this condition was not tested. My error I guess.
Here are the asm files for V99.01 and V98.07 rc19 respectively
EDIT : yes the legacy define for next works. The attach asm file is pre-define.
Last edit: kent_twt4 2022-02-17
Use the workaround to prove resolves the issue - so, this works. Good.
Someone needs to fix the compiler.
I am sorry. The code is doing as expected.
For Bright = 255 to 0
This is POSTIVE increment of 1, as BRIGHT is a byte it will be assgned 255 then 0 and exit.The code is working as expected.
The legacy is INCORRECT and should be relied on.
Use
For Bright = 255 to 0 step -1
if this is the intended action.OK back on me, step -1 it is.
Somehow the old for next was letting me get away with stuff. Need to change Help example. Step -1 confirmed correct on hardware with 99.01
Thank you.
I have just updated all the Help. There were many places where STEP -1 was missing. It is now updated and published online.
I have also updated many, many demo files that did not have the correct usage to
step -integer
. I did not update last year.. clearly.Okay the solution with "step -1 " works fine and is the solution for me.
Thank you!
@winch - thank you for spotting. The demos and Help has not been corrected to reflect the improvements in the compiler.
:-)
What's the difference between a for-next loop and a do-loop in speed and memory usage please?
You figure it out.....
For newbees the for-next is expected to work, and, it is expected to work as programmed (it was not in the past)
Newbees may not know about that do loop.
True, for-next is more common, do loop was not in zx basic or anything else at the time , when did it become a basic command? It's not used in any 80's machines that ran basic as the os.
Thing is, is it faster than do-loop? I guess no-one tested.
How to. A for next inside an another for next and the same for a do loop inside another do loop and use the millis function at the start and end?
Hijacking a thread Stan... create a new thread on the performance question