Loop delay

Hey everyone. I am trying to make a strobe light effect. I have figured out how to change the colors of the circle on screen from white to black use a switch, but I want to make a loop so when I turn the switch it does a strobe effect. The only problem is that the colors change to fast for me to see anything and I was wondering on how to make a delay in the loop.
Thanks.

function setup()
    --other code
    timer=0
    strobeTime=1/4 -- of a second
end

function draw()
    timer=timer+DeltaTime
    if timer>strobeTime then 
        --include code to switch colours here, 
        --then reset timer
        timer=timer-strobeTime
    end
   --other code 
end

Try this, you can add more colours to it easily:

-- Strobe light
function setup()
    parameter.number("delay",1/60,1)--the delay between each color change in seconds
    timer=0
    cols={color(255),color(0)}--table of colors to loop through add more colors if you want
    num=1
end

function draw()
    timer = timer + DeltaTime--increase timer by the time since last frame
    if timer>=delay then--if timer has reached delay
        timer=0--reset color
        num = num + 1--goto next color
        if num>#cols then
            num=1
        end
    end
    background(cols[num])
end

Edit:posted about a second after Ignatz

Well @SkyTheCoder this is the program I made.

function setup()
    timer = 0
    strobeTime = 1/4
    parameter.number("stroke1", 20, 2000, 25)
    parameter.boolean("switchChange" , true , switchChange)
    end
    function onoff()
    if switchChange == true then
        print("on")
        else
        print("off")
    end
end
function switchChange(value)
    onoff()
    end
function draw()
    
    
            on = {{fill(255, 255, 255, 255)},
            {ellipse(400,400,stroke1)},
    }
            
        

    --[[    while switchChange == false do
             timer=timer+DeltaTime
    if timer>strobeTime then 
            off = {{fill(0, 0, 0, 255)},
            {ellipse(400, 400 ,stroke1)},
         }
    
    
        
           on = {{fill(255, 255, 255, 255)},
            {ellipse(400,400,stroke1)},
            }
            
            off = {{fill(0, 0, 0, 255)},
            {ellipse(400, 400 ,stroke1)},
         }
        timer = 0
            timer=timer-strobeTime 
        end
    end
    end  ]] --

The area where this is double brackets is where I am trying to write a strobe effect. As you can see I made a parameter that will make a switch that will change the colors of the ellipses if I flip the switch. What I want to do is make so when I flip the switch, instead of changing color the ellipse will do a strobe effect, but instead when I flip the switch, codea freezes up and I have to close the app and reboot it to get it working again.

@Progrmr235 Try this. WARNING : rapidly flashing white light.


displayMode(FULLSCREEN)

function setup()
    tmr=0
end

function draw()
    tmr=tmr+1
    if tmr>8 then
        fill(255)
        tmr=0
    end
    ellipse(WIDTH/2,HEIGHT/2,WIDTH)
    fill(0)
end

How do you get codea to not freeze up when you run an infinate loop? This repeatedly happened to me when tried the strobe effect.

@Progrmr235 You add code so there is a way out of the infinite loop. What’s an example of your infinite loop.


function setup()
    --other code
    timer=0
    strobeTime=1/4 -- of a second
end

function draw()
    timer=timer+DeltaTime
    if timer>strobeTime then 
        --include code to switch colours here, 
        --then reset timer
        timer=timer-strobeTime
    end
   --other code 
end

Here’s an example

@Progrmr235 There isn’t an infinite loop there. The function draw() executes 60 times per second. Every 1/4 second, the code inside the “if” statement gets executed and the timer gets reset.

@Progrmr235 That’s @Ignatz’ code…

@SkyTheCoder. ( @Progrmr235 added an extra line at the end).

I wouldn’t expect that code to cause a freeze. It works fine for me.

But how do I get it not to freeze, regardless if it isn’t an infinate loop, I still can’t get it to work.

@Progrmr235 How is it freezing if it’s not an infinite loop? (Ignatz’ code isn’t an infinite loop and doesn’t freeze)

@Progrmr235 - this is completely different to my code that you said was freezing Codea!

@Progrmr235 All right, I think I fixed everything. I’m not going to go over most of the stuff, but you don’t need a function that just calls another function, you don’t want a function named the same as a parameter, and you don’t do an infinite while loop in draw. Codea doesn’t have any break between running lines of code, as some other versions of Lua do. So if you draw three multicolored ellipses on top of eachother, only the last one will be visible. And if you put a long while loop in the draw function, no frame will be visible, and Codea gets no break from trying to run your code.

function setup()
    timer = 0
    strobeTime = 1/4
    parameter.number("stroke1", 20, 2000, 25)
    parameter.boolean("switchChange" , true , onoff)
    strobing = 0
end

function onoff(val)
    if val then
        print("on")
    else
        print("off")
    end
end

function draw()
    background(0)
    
    fill(255)
    ellipse(400,400,stroke1)
    
    if not switchChange then
        timer=timer+DeltaTime
        if timer>strobeTime then
            strobing = strobing + 1
            if strobing == 1 then
                fill(0)
                ellipse(400, 400 ,stroke1)
            elseif strobing == 2 then
                fill(255)
                ellipse(400,400,stroke1)
            elseif strobing == 3 then
                fill(0, 0, 0, 255)
                ellipse(400, 400 ,stroke1)
                strobing = 0
                timer = 0
            end
        end
    end
end

@Progrmr235 Here’s a version that allows you to vary the strobe speed from 1/30 to 1/2 second.


function setup()
    timer = 0
    parameter.integer("speed",2,30,10)
    parameter.integer("size",200,WIDTH*2,WIDTH)
    parameter.boolean("strobe_off_on",true)
end

function draw()
    background(0)
    fill(255)
    if strobe_off_on then
        timer=timer+1
        if timer>speed then
            ellipse(WIDTH/2,HEIGHT/2,size)
            timer=0
        end
    else
        ellipse(WIDTH/2,HEIGHT/2,size)
    end
end