Help with an if statement

Hi all,

I’m trying to do something very simple: Add a Speed and Direction slider for a rainbow pattern.

I use this bit to get a 0 or 1 value for direction, and export the var for checking:

export var direction
export function sliderDirection(v) {
direction = floor(v)
}

When I move the slider, I get 0 or 1 in the Vars Watch, all good. Once I introduce an if statement in the export render function, direction value sticks to 0 no matter where the slider is, and as expected the pattern doesn’t change direction. What am I doing wrong?

Code:

export var direction
export function sliderDirection(v) {
direction = floor(v)

}

export var speed
export function sliderSpeed(v) {
speed = ((1-v) / 4) + .002;
}

export function beforeRender(delta) {
t1 = time(speed)

}

export function render(index) {

if (direction = 0) {
h = t1 + index/pixelCount
s = 1
v = 1
hsv(h, s, v)}

else {
h = t1 - index/pixelCount
s = 1
v = 1
hsv(h, s, v)
}

}

Thanks!

Floor of 0…1 is a bit lopsided.

Try v + 0.5 so you flux between 0.5 and 1.5

That was it, I used > .75 and now it switches right in the middle of the slider, thanks!

If you have some time, do have a look at my question here: Multisegment pattern - #7 by Petros, I’m having trouble doing the math properly so that the segments scale properly regardless of strip length.

Thanks again!

Petros, for this sort of thing, the most efficient way of building the if statment is to go backwards from the last segment. Here’s a working example, based on your code:
( to make a code block, just enclose the code in 3 of the ‘backtick’ marks - the single quote that shares the '~" key on most keyboards. These guys – ```)

// controls won't set values until they're actually touched,
// so you need to initialize all your variables in advance.

var hue1 = 0;
var hue2 = 0.333;
var hue3 = 0.6667;

var saturation1 = saturation2 = saturation3 = 1;
var value1 = value2 = value3 = 0.7;

export var depth1 = depth2 = floor(pixelCount/3);

//Colour Picker 3
export function hsvPickerHue3(h, s, v) {
  hue3 = h
  saturation3 = s
  value3 = v
}

export var depth1
export function sliderDepth1(v) {
  depth1 = floor(v * 100 );
}

export var depth2
export function sliderDepth2(v) {
  depth2 = floor(v * 100 );
}

export function render(index) {
  if (index >= depth1 + depth2) {  // everything after depth2
    hue = hue3
    saturation = saturation3
    value = value3
  } 
  else if (index >= depth1) {  // after depth1
    hue = hue2
    saturation = saturation2
    value = value2;
    hsv(hue2, saturation2, value2)
  } 
  else {                     // from 0 to end of depth1
    hue = hue1
    saturation = saturation1
    value = value1
  }
  hsv(hue, saturation, value)
}

Thanks for the reply, it’s quite easy to understand now!