Shop OBEX P1 Docs P2 Docs Learn Events
Issues with repeat function — Parallax Forums

Issues with repeat function

ElkinElkin Posts: 58
edited 2011-08-17 00:13 in Propeller 1
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.
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

  • Miner_with_a_PICMiner_with_a_PIC Posts: 123
    edited 2011-08-16 03:43
    Perhaps the := in the line repeat while X := 1 should be replaced with ==. Can you try this and determine if it helps?
  • ElkinElkin Posts: 58
    edited 2011-08-16 03:47
    I did think about that too, but that doesnt seem to make any difference either. It just runs through all of the filenumbers 1 cycle at a time (number of records logged in each file is only 1).
  • Miner_with_a_PICMiner_with_a_PIC Posts: 123
    edited 2011-08-16 04:00
    The read_adc call might not be setting the variable X as expected, you should investigate that portion of the code deeper. If you post that code perhaps someone in the forum might spot the culprit. While debugging set the := previously discussed to == as the := is an assignment operator so no matter how the read_adc function might alter the variable X the repeat will reset it back to 1 each time.
  • ElkinElkin Posts: 58
    edited 2011-08-16 04:22
    This code is pretty straight forward and doesnt really have anything to do with the calling of X. The second post of code is what is setting the variable.
    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)
    
  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-08-16 05:00
    Is the block that sets the X variable in another cog?
    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)
  • ElkinElkin Posts: 58
    edited 2011-08-16 05:20
    The Block that sets the X variable is in COG 1 (this is the block that executes the main block and call the other COGS). The PUB block that executes the function of the ADC read and SD card functions is called by COG 1 and operates on another cog. I was thinking that this could be a problem but in the code above. I have the terminal listing the value of X as "term.dec(X)" and the value does change. This idea about the COGS may be correct though because I have to define X as a local variable in each PUB block.

    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)
    
  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-08-16 15:00
    Glad I could help.
    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!)
  • ElkinElkin Posts: 58
    edited 2011-08-17 00:13
    I know what you mean. So much usually does go wrong but it is such a nice feeling when it finally runs the way it should! I have basically the same thing with 4 cogs running repeat loops and the other steering them...
Sign In or Register to comment.