Issues with repeat function
I am having trouble getting the repeat function to count up for me. either it doesnt ever close the first file, or it goes through them all without logging any data.
I want to insert a function into this code where the (term.rxcheck <> -1) is so that this condition will change within the main block of code (toggling a variable, or a pin)
I have tried a conditional repeat below (repeat while X := 1) and have the variable (X), incrementing and decrementing in my main block, but this wont work.
The goal is to have this block start and stop by command of the main block so that I do not have to hit a key on the computer every time I want to open a new file.
The ZeroPad program is a counting program that can change the number in the string so that the files will not overwrite each other in the SD card (thanks Duane!).
Thanks.
I want to insert a function into this code where the (term.rxcheck <> -1) is so that this condition will change within the main block of code (toggling a variable, or a pin)
I have tried a conditional repeat below (repeat while X := 1) and have the variable (X), incrementing and decrementing in my main block, but this wont work.
The goal is to have this block start and stop by command of the main block so that I do not have to hit a key on the computer every time I want to open a new file.
The ZeroPad program is a counting program that can change the number in the string so that the files will not overwrite each other in the SD card (thanks Duane!).
Thanks.
repeat FileNumber from 0001 to 9999
ZeroPad(FileNumber, 4, @logfile + 4)
term.str(@logfile)
term.tx(13)
check := \sdcard.popen(@logfile, "w") ' attempt to create
if (check == 0)
term.str(string("-- log file created; logging started", CR))
\sdcard.pputs(@header) ' column header in CSV
else
term.str(string("-- ERROR: could not create log file", CR))
repeat
waitcnt(0)
term.rxflush
sync := cnt
count := 1
repeat while X := 1
read_adc(@ch0) ' get data
' write to log file
\sdcard.pputs(dec2str(count, @sbuf)) ' write count
\sdcard.pputc(",") ' separate
\sdcard.pputs(dec2str(ch0, @sbuf)) ' write ch0 value
\sdcard.pputs(@crlf) ' terminate line
'if (term.rxcheck <> -1) ' if key pressed
' quit ' abort
waitcnt(sync += clkfreq/10) ' update every 100 milliseconds
count += 1 ' update readings count
\sdcard.pclose

Comments
pub read_adc (pntr0) | ch0 long[pntr0] := ADC.read (ch0, ADC#SE) pub read(ch, mode) | mux '' Read MCP3202 channel '' -- ch is channel, 0 to 1 '' -- mode is 0 for single-ended, 1 for differential mux := %1100 | ((mode & 1) << 2) | ((ch & %1) << 1) ' create mux bits return readx(mux)Block := 1 X := 0 term.str(string("--program started", CR)) Repeat OUTA [P0] := 0 Syringe.Signal waitcnt(time += 800_000_000) '10 sec OUTA [P1] := 1 ++X term.dec(X) repeat 24 waitcnt(time += 800_000_000) '240 sec Syringe.Signal OUTA [P1] := 0 repeat 36 waitcnt(time += 800_000_000) '360 sec OUTA [P1] := 1 --X term.dec(X) term.str(string("-- file end", CR)) waitcnt(time += 800_000_000) '10 sec OUTA [P2] := 1 OUTA [P0] := 1 Syringe.Signal repeat 36 waitcnt(time += 80_000_000) '36 sec OUTA [P2] := 0 repeat 64 waitcnt(time += 80_000_000) '64 sec Block += 1 ' update readings count term.dec(Block)Have you put in a term function to print out the value of X to ensure it is 1? (and I mean in your main loop not the one that adjusts the value of X)
Okay, I got it. I went and defined X as a global variable in the VAR block and that took care of the problem. Everything else is the same.
Thanks for the help!
Code now reads:
VAR word X repeat FileNumber from 0001 to 9999 ZeroPad(FileNumber, 4, @logfile + 4) term.str(@logfile) term.tx(13) repeat until X==1 if X==1 quit check := \sdcard.popen(@logfile, "w") ' attempt to create if (check == 0) term.str(string("-- log file created; logging started", CR)) \sdcard.pputs(@header) ' column header in CSV else term.str(string("-- ERROR: could not create log file", CR)) repeat waitcnt(0) term.rxflush sync := cnt count := 1 repeat while X == 1 read_adc(@ch0) ' get data ' write to log file \sdcard.pputs(dec2str(count, @sbuf)) ' write count \sdcard.pputc(",") ' separate \sdcard.pputs(dec2str(ch0, @sbuf)) ' write ch0 value \sdcard.pputs(@crlf) ' terminate line 'if (term.rxcheck <> -1) ' if key pressed ' quit ' abort waitcnt(sync += clkfreq/10) ' update every 100 milliseconds count += 1 ' update readings count \sdcard.pclose term.str(string("-- log file closed", CR)) term.str(string("-- records logged: ")) term.dec(count) term.tx(13)I had a similar problem with a variable in an object I wrote for a keypad.
My next object I am currently writing runs on 4 cogs with three of the cogs running continuously in repeat statements and taking their instructions from the values of different variables set by other cogs..... (So much could go wrong -- It will be fantastic when it runs smoothly!)