If loop not working as intended

Hey guys. I’ve run into a roadblock. The if loop within the draw function is not working as it’s supposed to. Please help.
The loop runs only once, but it’s supposed to keep running as long as the condition remains true.

-- Test

-- Use this function to perform your initial setup
function setup()
    temperatureGauge = {}
    parameter.watch("temperatureGauge.green")
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    temperatureGauge.green = 246
        
    if temperatureGauge.green > 12 then
        temperatureGauge.green = temperatureGauge.green - 6
    end

    if temperatureGauge.green <= 12 then
        temperatureGauge.green = temperatureGauge.green + 6
    end
end

A while loop or repeat/until runs for as long as the condition is true. But the draw function is meant to execute 60 times a second, so it’s not the place for a loop like that. I’m not sure what you’re trying to do with your code, but you set the temperatureGauge.green variable back to 246 each frame. Try moving that line to setup and see if that makes a difference.

You’re resetting the value of temperatureGauge.green to 246 every frame, so the first if statement will always execute and at the end of the function the value will always be 240.

Try moving the initialisation line into setup() instead.

@yojimbo2000, @TechDojo - thanks guys. Don’t know how I didn’t see it.