Shop OBEX P1 Docs P2 Docs Learn Events
splitting up an Array[s] — Parallax Forums

splitting up an Array[s]

grasshoppergrasshopper Posts: 438
edited 2008-03-08 20:50 in Propeller 1
I am trying to take a number from 0 - 100 and fill an array with it. i.e

long stringdata   <------array 
long X                    <------varable

Pub Start
X = 100
loop: 
  stringdata[noparse][[/noparse]0] := 1
  stringdata := 0
  stringdata := 0
end loop





How do i write a loop using a bit shif?

help me please...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-07 21:29
    1) It's difficult to tell what you want to do. Remember that the forum software uses some things in subscript brackets for formatting, then deletes them, even when you use CODE markers. Put a space after the opening bracket and the forum software will leave it alone.

    2) There are no labels in Spin. Read the description of the REPEAT statement. This combines the FOR / NEXT loop and the DO / LOOP statements in Parallax Basic.
  • grasshoppergrasshopper Posts: 438
    edited 2008-03-07 21:48
    the code i put up was an attempt at Pseudo code. Here is my concept again

    I want to do a repeat so that the variable X is broke up into an array named data
            x =  100
                 |||
    data [noparse][[/noparse] 0 ] <-|||
    data [noparse][[/noparse] 1 ] <--||
    data [noparse][[/noparse] 2 ] <---|
    
    
    


    so that when the "repeat until" is done I will have this --> data [noparse][[/noparse] 0 ] = 1 and data [noparse][[/noparse] 1 ] = 0 and data [noparse][[/noparse] 2 ] = 0

    Post Edited (grasshopper) : 3/7/2008 9:54:12 PM GMT

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-03-07 22:48
    Isn't this exactly whats done by the dec methods in TV_Text, VGA_Text and FullDuplexSerial? The only difference is you're assigning it to an array while the drivers send it to the out and·tx method

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 3/7/2008 11:12:34 PM GMT
  • grasshoppergrasshopper Posts: 438
    edited 2008-03-07 23:11
    Paul i am not sure what you mean. Forgive me i have been trying to program this propeller all day and i guess i need a break.

    All i want to do is put a three digit number like 100 into an array so that i can display this on my LCD screen. Why is it so hard to program the propeller. I figured i could do this but i have no such luck: Thank god you are all here...

      con
      esc = $1B     'Escape code
      cr = $0D      'Carriage Return code                 
      lf = $0A      'Line Feed code
      eos = $FF     'End of string code
      eog = $FF     'End of graphics code
      space = $20   'Space character code
      max_duty = 100
      pwm1pin  = 7
      
    VAR  
       long stringdata[noparse][[/noparse] 7 ]
       long x
       long number
      long stringdata1[noparse][[/noparse] 4 ] 
    obj
      LCD : "LCDDEMO2"
      pwm1  :  "pwmasm"
       
    pub main   
    repeat 
      DISPLAY
      PULS_STIRR
      waitcnt(100_000_000 + cnt) 
         
    PUB DISPLAY | Y
    
      LCD.init                                              'Initialize the display  
      lcd.clS
       
      RETURN  
    
    PUB PULS_STIRR  | Y
    
      pwm1.start(pwm1pin)
      pwm1.SetPeriod(1_000_000)                             'high period will allow for a slow motor
       repeat x from max_duty to 100                         'linearly advance parameter from 0 to 100
          pwm1.SetDuty(x)
          waitcnt(1_000_000 + cnt)                          'wait a little while before next update
          
        repeat y from 0 to 3 
          stringdata1[noparse][[/noparse] Y ] := (x ~> Y - 1)                             ' pack the characters into a long
        lcd.cls
        lcd.writeout(stringdata1[noparse][[/noparse] 0 ])
        lcd.writeout(stringdata1[noparse][[/noparse] 1 ])
        lcd.writeout(stringdata1[noparse][[/noparse] 2 ])  
      
    return
    
    
    
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-03-07 23:21
    What I'm saying is there are 3 objects in your Tool distribution which have sections of code which perform this task, simply pilfer it and adapt it for your own use. Here is the method from VGA_Text:

    PUB dec(value) | i
    '' Print a decimal number
      if value < 0
        -value
        out("-")
      i := 1_000_000_000
      repeat 10
        if value => i
          out(value / i + "0")
          value //= i
          result~~
        elseif result or i == 1
          out("0")
        i /= 10
    
    

    but instead of using the out method, you would assign it to your·data array.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • hippyhippy Posts: 1,981
    edited 2008-03-08 14:41
    To expand on what Paul says, this will convert a three-digit decimal number into three array entries, each holding a value 0 to 9 ...

    yourNumber := 123
    
    divisor := 100
    index := 0
    repeat
      yourArray[noparse][[/noparse] index ] := ( yourNumber / divisor ) % 10
      index := index + 1
      divisor := divisor / 10
    until divisor == 0
     
    ' yourArray[noparse][[/noparse] 0 ] == 1
    ' yourArray[noparse][[/noparse] 1 ] == 2
    ' yourArray[noparse][[/noparse] 2 ] == 3
    
    
    



    Plenty of scope to optimise the code there, but shown longhand for clarity.

    If you want ascii characters in yourArray[noparse]/noparse rather than numbers ...

      yourArray[noparse][[/noparse] index ] := ( ( yourNumber / divisor ) % 10 ) | $30
    
    
    

    Post Edited (hippy) : 3/8/2008 2:48:32 PM GMT
  • grasshoppergrasshopper Posts: 438
    edited 2008-03-08 20:50
    Thanks Hippy and paul for helping me.
    I figured it out using both of your examples .
Sign In or Register to comment.