I just wanna delay loop!

Thread Starter

mikewax

Joined Apr 11, 2016
184
why do i hafta go around in circles? i can't use delay or delay_ms or ellapsedmilis because all three timers are busy and can't be interrupted.
but this:
for (t = 0; t<10000; t++){ for (x = 0; x<10000; x++){} }
shouldn't be a nightmare. but nooooooooo, gcc won't stand for it.
so there's this file, C:\Program Files (x86)\Arduino\hardware\arduino\avr\platform.txt, that supposedly i can edit and tell gcc to getthehelloutamyway!

and it has this paragraph:
# Default "compiler.path" is correct, change only if you want to override the initial value
compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects
compiler.c.elf.flags={compiler.warning_flags} -Os -flto -fuse-linker-plugin -Wl,--gc-sections
compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD
compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0
compiler.elf2hex.flags=-O ihex -R .eeprom

where the -O option appears no less than 5 times. can someone who speaks gcc tell me what to do here?

thanx, CrazyInLA
 

mtonge

Joined Apr 19, 2016
93
What does the compiler say? The nested 'for' loops just use a lot of processor time to create a delay, correct? You should declare your loop counter variables as integers. Perhaps the syntax should be:


for(int t = 0; t<10000; t++){
for(int x = 0; x<10000; x++){
}
}
 
Last edited:

Thread Starter

mikewax

Joined Apr 11, 2016
184
yes they are unsigned integers. the problem is that the compiler is configured to "optimize" the code for faster execution. it sees those statements and goes "he's wasting time. i'll modify the code so that no matter what #s he uses for x or t, i'll minimize the time delay"
and that's what i see. whatever #s i use i still get a delay of about 100ms.
but now i may have a solution. somebody over at http://www.electro-tech-online.com/threads/pwm-problem-cant-set-duty-cycle.149671/#post-1283925 electrotech online said that i could create an interrupt routine that will execute every time the timer overflows that will increment a variable. this will not interfere with the timer and will give me a way to create a delay. unless of course the compiler gives me <snipped language> about that, too.
 
Last edited by a moderator:

Thread Starter

mikewax

Joined Apr 11, 2016
184
How much delay do you need? A 555 timer could trigger an interrupt too.
there's always that. i actually used a 555 on a design last year. they will never go out of style. but i got many delays in my code of different duration.
thanx
 

ErnieM

Joined Apr 24, 2011
8,377
Go have a peek at what the "volatile" keyword does.

Hint: it tells the compiler don't assume you know what value is in that variable.

Hint 2: it is transportable between compilers.
 

Thread Starter

mikewax

Joined Apr 11, 2016
184
Go have a peek at what the "volatile" keyword does.
Hint: it tells the compiler don't assume you know what value is in that variable.
Hint 2: it is transportable between compilers.
hmmm.... i never thoughta that. that's even simpler. thanx i'll try it :)
 
Top