Shop OBEX P1 Docs P2 Docs Learn Events
problem with a variable — Parallax Forums

problem with a variable

stilgarstilgar Posts: 47
edited 2012-05-09 22:21 in Propeller 1
Hello I am having trouble. I am working on a program block (please note, this is only a small bit in a larger program) that uses the led_pwm.spin.
What I am trying to do is get the variable "maxbright" to start at 0 and increment to 64 and stop. after this block is completed "maxbright" will remain at 64 for all the rest of code that follows.

everything I have tried don't work at all or don't compile.

thanks

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-06 09:50
    maxbright is a constant not a variable.

    If you use a variable (I used "result") you can change its value from zero to maxbright like this.
    repeat result from 0 to maxbright
        repeat x from 7 to _numleds-8
          leds[x] := result 
          waitcnt(clkfreq / (rtime1)+ cnt)              ' center LEDs slowly go from 0 to maxbright 64
    
    

    This will only change the values of leds[7] and leds[8] though.

    This will delay after each element of the two "leds" elements are set. If you want to delay after both elements are set you'd want to change the indentation on the waitcnt to this.
    repeat result from 0 to maxbright
        repeat x from 7 to _numleds-8
          leds[x] := result 
        waitcnt(clkfreq / (rtime1)+ cnt)              ' center LEDs slowly go from 0 to maxbright 64
    
    

    If you explain in more detail what you want to do, I (or others) may be able to help more.
  • Mike GMike G Posts: 2,702
    edited 2012-05-06 11:39
    Give this concept a try
    con
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      MAX_BRIGTH = 64
      MS_DELAY   = 100
    
    VAR
      byte luminosity
      byte isMaxBright 
    
    OBJ
      pst           : "Parallax Serial Terminal"
    
    PUB Main
      pst.Start(57600)
      pause(500)
    
      luminosity~
      isMaxBright := false
      
      repeat
      
        repeat MAX_BRIGTH
          ifnot(isMaxBright)
            luminosity := (++luminosity) // 65
          pst.dec(luminosity)
          pst.char(13)
          pause(MS_DELAY)
          if(luminosity == MAX_BRIGTH)
            isMaxBright := true
    
    PRI pause(ms)  
      waitcnt(((clkfreq / 1_000 * ms - 3932) #> 381) + cnt)
      return
    
    
  • kwinnkwinn Posts: 8,697
    edited 2012-05-06 12:23
    Sounds like you are trying to have the led intensity increase slowly to imitate waking up and opening eyes slowly. You can do this by making luminosity a variable and increasing it from 0 to maxbright. The rate at which it increases can be set by the number scans between each step in brightness.
  • stilgarstilgar Posts: 47
    edited 2012-05-06 14:15
    kwinn wrote: »
    Sounds like you are trying to have the led intensity increase slowly to imitate waking up and opening eyes slowly. You can do this by making luminosity a variable and increasing it from 0 to maxbright. The rate at which it increases can be set by the number scans between each step in brightness.

    Yes that whats I am trying to do. Many thanks to Duane Degn for the code it works great.
    Now a new problem I need to fix is when the LEDs fad on, they seem to flicker. I think its a timing issue, but not sure.

    I have attached all the program code I have done so far. The methood called "Death" , is not finished yet. this block is to slow the sweep speed to stop then fade the LEDs off.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-06 14:22
    I think if you change the line:
    delay   long  1200   ' Actual PWM carrier, period will be delay * wavemax   orig..600 1200 fer 16
    

    In led_pwm_1.spin to:
    delay   long  [B]100[/B]   ' Actual PWM carrier, period will be delay * wavemax   orig..600 1200 fer 16
    

    You should get less flicker.

    You might want to try several different values for "delay" to see what looks best.
  • stilgarstilgar Posts: 47
    edited 2012-05-06 16:13
    I changed the delay to 100 and froze the program. So I went the other way and 5400 seems to work, still minor flicker but if I go any higher the persistence variable goes wacky
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-06 16:15
    I would think a smaller delay would give less ficker. Did you try 300 or 600?
  • kuronekokuroneko Posts: 3,623
    edited 2012-05-06 16:30
    Duane Degn wrote: »
    I would think a smaller delay would give less ficker. Did you try 300 or 600?
    That's not enough time for 16 LEDs (1200 is kind of a safe value, IIRC it could be slightly less). With decay enabled you'd get a [thread=139419]waitcnt lockup[/thread].
  • stilgarstilgar Posts: 47
    edited 2012-05-09 22:21
    Thanks, everyone
Sign In or Register to comment.