Shop OBEX P1 Docs P2 Docs Learn Events
Changing variable value over time — Parallax Forums

Changing variable value over time

JaspJasp Posts: 7
edited 2012-12-26 09:21 in Propeller 1
Hi everybody,

I'm working on a project in which I control a VCA with the prop. I'm trying to introduce an attack- and releasetime in which the prop changes the control voltage (for the vca).

Here's how my code looks now:
PRI Expander

  ADYN := 1                                                                                                               
  if EHOLD > 0                                                          
    waitcnt ((EHOLD * (Clkfreq / 1000)) + cnt)                                  
  Repeat                                              
    CV := 3093 + (OutCon * EGAIN) + (OutCon * (((RMSdB - ETRHD) * ERTIO) - (RMSdB - ETRHD)))
    ADin := AD.in(1)                                                     
    RMSdB := ((ADin - 3280) / InCon)                                        
    REDdB := (-(3093 - CV) / OutCon)                  
    if (ADin > ((InCon * ETRHD) + 3280)) and (ADYN == 1)    
      ADYN := 4                                        
      Return
    if (ADin =< ((InCon * GTRHD) + 3280)) and (ADYN == 1) and (GBP == 1)       
      Gate
      Return                                         
    if (EBP == 0) and (ADYN == 1)
      ADYN := 4                                        
      Return       

CV is the variable which is written to the adc and controls the VCA. It's standard value is 3093. What I need is a little piece of code which changes the CV-value from 3093 to a calculated value (which is already calculated with the formula in the code above) over a time between 1ms and 1000ms (the releasetime, the releasetime-value is written in the variable RTIME). Afterwards, if the first if-condition which leads to a return (not the last two) is true, CV has to change back from the calculated value to 3093 in a time between 1ms and 200ms (the attacktime, ATIME). The calculated value from CV can be between 0 and 4095.

I tried a lot of things already but it just won't work... Does anybody has any hints on how to program the attack- and/or releasetime in this code?

Thanks,

Jasper

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2012-12-26 06:34
    Hi Jasper,

    I found it difficult to follow the logic of your code.
    The reasons are
    - you didn't posted your complete code
    - the variablenames are not selfexplaining

    So I wrote some code from scratch wich does what you described in your post.

    You need to diffentiate on two cases
    difference between start and endvalue is bigger / smaller than the time in which the endvalue should be reached.

    below working code that shows a demo on Parallax Serial Terminal (F12)
    that gives you feedback what values the content of variable ActualValue has in each loop


    just as a first hint
    CON
         _clkmode = xtal1 + pll16x      
         _xinfreq = 5_000_000
    
    
    OBJ
      PST : "Parallax Serial Terminal"
    
      
    VAR
      long StartValue
      long EndValue
      long ActualValue
      
      long Diff
      long unsignedDiff
      long SignOfDiff
      long DeltaDiff
      long DeltaTime
    
      long ChangeTime
      long WaitTime
    
      byte MyStr[10]
    
           
    PUB Main
    
      Pst.Start(115_200)
    
      repeat
        ChangeTime  := 1000 'milliseconds
        StartValue  := 3000
        EndValue    := 3200
    
        ActualValue  := StartValue
        Diff         := EndValue - StartValue
        unsignedDiff := ||Diff
    
        TaggedVal(string("Start: "),StartValue,true)
        TaggedVal(string("End: "),EndValue,true)
        TaggedVal(string("Diff: "),Diff,true)
        TaggedVal(string("unsignedDiff: "),unsignedDiff,true)
    
        IncDecVal
    
    
    
        ChangeTime  := 1000 'milliseconds
        StartValue  := 3200
        EndValue    := 3000
    
        ActualValue  := StartValue
        Diff         := EndValue - StartValue
        unsignedDiff := ||Diff
    
        TaggedVal(string("Start: "),StartValue,true)
        TaggedVal(string("End: "),EndValue,true)
        TaggedVal(string("Diff: "),Diff,true)
        TaggedVal(string("unsignedDiff: "),unsignedDiff,true)
    
        IncDecVal
    
    
    
        ChangeTime  := 100 'milliseconds
        StartValue  := 400
        EndValue    := 200
    
        ActualValue  := StartValue
        Diff         := EndValue - StartValue
        unsignedDiff := ||Diff
    
        TaggedVal(string("Start: "),StartValue,true)
        TaggedVal(string("End: "),EndValue,true)
        TaggedVal(string("Diff: "),Diff,true)
        TaggedVal(string("unsignedDiff: "),unsignedDiff,true)
    
        IncDecVal
    
    
    PUB IncDecVal  
      'if unsignedDiff is smaller than Changetime the time to increment / decrement the value
                                                 'is bigger than 1 millisecond  
      if unsignedDiff < ChangeTime
        DeltaTime   := ChangeTime / unsignedDiff 'So the waittime must be set to the apropriate time
        if Diff > 0    
          DeltaDiff   := 1                       'the increment per loop must be set to 1
        else  
          DeltaDiff   := -1                      'the decrement per loop must be set to 1
    
        
       'if unsignedDiff is bigger than Changetime the time to increment / decrement the value
                                                 'is 1 and the inc /dec is bigger than 1  
      else
        DeltaTime := 1                   'waittime is one millisecond
        DeltaDiff := Diff / ChangeTime   'increment per loop must be set to apropriate value 
    
      PST.Str(string("DeltaTime: "))
      PST.Dec(DeltaTime)
      PST.CHar(13)  
    
      PST.Str(string("DeltaDiff: "))
      PST.Dec(DeltaDiff)
      PST.CHar(13)  
    
      PST.Str(string("Press Key to continue"))
      PST.StrIn(@MyStr)
      
      repeat (Diff / DeltaDiff)
        WaitCnt( (CLkFreq / 1000) * DeltaTime + cnt )
        ActualValue := ActualValue + DeltaDiff
        PST.Dec(ActualValue)
        PST.CHar(13)  
    
    
    PUB TaggedVal(StrPointer,Value,LineFeed)
      PST.Str(StrPointer)
      PST.Dec(Value)
      if LineFeed
        PST.CHar(13)                
    

    best regards
    Stefan
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-12-26 09:21
    Hi Jasp,

    you should not have started a new thread! The old one gives a lot more informations about your problem. Especially the part where I told you that it does not make sense to increase CV more often than your other COG can send the value to the DAC!
Sign In or Register to comment.