relative Sine wave

hi all
i made a realy complicated (at least for me) led Sequencer. (on arduino DUE)
and for now everything i made seems to work perfect.

i store the sequence on a SD card.
and i can read the data as i like it to be.

the data i get are no of steps, fadetime, scenetime and outputchannel, currentvalue.

i store and compare the fadetime and scenetime with currentvalue, prevvalue on sram.
this way i can increase or decrease the output value with scenetime and fadetime useing micros.

then i try to change the output with some math.
like :
Master dimmer.
individual dimmer per Sequence (i run i can run more then one at the same time)
individual tap sync ( i can change it on the fly)
individual Fadetime ( i can change it on the fly)
individual constrain function ( i can change it on the fly, this make the saw wave smaller or bigger at both sides) example normal the Sequence = 0 to 255 after this fuction you get 10 to 245
all seems to work perfect.

but now i try to add a Sine wave to it.
this work ok with all the functions you see above but when i do the Sine with the Contrain everything goed wrong, i just cant seem to let it work.

and to get a idea how i do the math i show you some code i made.

this line constrains the value, makes a avr for Scenetime, fadetime, tapsync, currentvalue and prevalue

currentval[z] = avrmainpot[num] * currentval[z]; // contrain value with potvalue
    _Arv[z] = (avrfadetime[num] * avrtap[num] * ((uint32_t)_Fade[num] * 1000.00)) / abs(currentval[z] - _Prevval[z]);
// Arv[channel] = float * float * long / abs(byte - byte)

now how do i play it back

if (currentval[z] > _Prevval[z] && currentval[z] > _value[z]) { // Do i need to Increase Value?
        _value[z] = _Prevval[z] + (micros() - _PrevMillis[z]) / _Arv[z]; // Yeah!!! lets increase the value with fade time
      }
      else if (currentval[z] < _Prevval[z] &&  currentval[z] < _value[z]) { // Do i need to Decrease Value?
        _value[z] = _Prevval[z] - (micros() - _PrevMillis[z]) / _Arv[z]; // Yeah!!! lets Decrease the Value with fade time
      }

i get _prevmillis when i fetch new data from the sdcard.

now _value[z] is a nice looking sawtooth wave.

and when i do

output = sin((_value[channel] / 81.184) + 4.712) * 127.5  + 127.5;

i get a nice looking sinewave.

now i like to constrain the sinewave and i only get strange result, and cant get i head around it.
so maby someone knows a solution?

Did you try the constrain() function?

i do see what you mean but the issue is.
when the seqeunce on the sd card is 34 to 165.

then the SINE code

output = sin((_value[channel] / 81.184) + 4.712) * 127.5  + 127.5;

gets wrong results because the range isnt 127.5 and the offset isnt 127.5 any more.
and i just cant get my head around these values. but maby i dont know, the 81.184 also need to change

Do you want to Constrain the sinewave?
AND mostly important, what do you mean by Constrain?

hi sorry i have been unclear.

but when i do a Sequence that goes like this 255, 0, 0

output = sin((_value[channel] / 81.184) + 4.712) * 127.5  + 127.5;

this code works Perfect, the Sine look realy nice.

i made a Image how i like to see it.
on the image you see a Sequence i made that goes like this 255,0,0,191,63,63 so its 6 steps long.
but i cant seem to get it work with the Sin function.

The value of sin() can range up to exactly 1, so I'd be inclined to add a tiny fudge factor:

output = sin((_value[channel] / 81.184) + 4.712) * (127.5-1e-4)  + 127.5;

As to your problem:

You say that you get odd values when you "constrain" the results of this. _value[] alters the frequency of your sine wave. At a guess, constrain is pushing the value to very large numbers, so the frequency of the sin() is so high that you are effectively sampling it. The wave form you get will depend on how the frequency at which it is sampled.

oke sorry, never mind the constrain.

i'm just running the Sequence you see above the 255,0,0,191,63,63

look at the new image so you understand what i mean.
and what you see is with the -1e-4. and it doesnt seem to do anything

spirit:
look at the new image so you understand what i mean.
and what you see is with the -1e-4. and it doesnt seem to do anything

most of the time it won't. 1e-4 is the same as 0.0001 . By making the range of the sine just a tiny bit smaller than 127.5, we guarrantee that it will never hit exactly 1 at sin(PI) and overflow things.

Speaking of which - are you aware that sin() expects an angle measured in radians, not in degrees?