# Controlling LED chaser speed via "pause" command

#### YeMa

Joined Apr 4, 2017
5
Hello, everyone!
I am currently working on a fairly simple project of making 7 (channel) LED chaser. Nothing special. I am just planning to connect 7 or more LEDs to 7 outputs of PICAXE-20M2 (from B.1 to B.7 respectively) via ULN2003 chip to handle more power. I have written some pretty basic code, part of which I am posting here. After running it using simulator function of PICAXE Editor software everything seems just fine.

My question is: How can I vary the speed at which the LEDs turn on and off by using the built-in "pause" command? Is there a way to change the values of all "pause" commands in the block of code at once via some line of code or using some external signal inputs (like switches, pots etc.)? I know I could just retype all values by hand and reload the program into the IC again, but this is cumbersome and I don't want to use the computer again each time I need to change the switching speed of the LEDs.

symbol LED1 = B.1 ; rename outputB1 LED1
symbol LED2 = B.2 ; rename outputB2 LED2
symbol LED3 = B.3 ; rename outputB3 LED3
symbol LED4 = B.4 ; rename outputB4 LED4
symbol LED5 = B.5 ; rename outputB5 LED5
symbol LED6 = B.6 ; rename outputB6 LED6
symbol LED7 = B.7 ; rename outputB7 LED7
symbol counter = b0 ; define a counter using variable b0

main:
gosub pattern
goto main

pattern: ; all LEDs light up one after another
for counter = 1 to 3 ; start a for...next loop
high LED1 ; turn on LEDs
pause 1000
high LED2
pause 1000
high LED3
pause 1000
high LED4
pause 1000
high LED5
pause 1000
high LED6
pause 1000
high LED7
pause 1000
low LED1 ; turn off LEDs
pause 1000
low LED2
pause 1000
low LED3
pause 1000
low LED4
pause 1000
low LED5
pause 1000
low LED6
pause 1000
low LED7
pause 1000
next counter ; end of for...next loop
end

#### djsfantasi

Joined Apr 11, 2010
7,210
Use a variable for the pause time...

Code:
symbol LED1 = B.1 ; rename outputB1 LED1
symbol LED2 = B.2 ; rename outputB2 LED2
symbol LED3 = B.3 ; rename outputB3 LED3
symbol LED4 = B.4 ; rename outputB4 LED4
symbol LED5 = B.5 ; rename outputB5 LED5
symbol LED6 = B.6 ; rename outputB6 LED6
symbol LED7 = B.7 ; rename outputB7 LED7
symbol counter = b0 ; define a counter using variable b0

main:
gosub pattern
goto main

pattern: ; all LEDs light up one after another

pauseTime=1000

for counter = 1 to 3 ; start a for...next loop
high LED1 ; turn on LEDs
pause pauseTime
high LED2
pause pauseTime
high LED3
pause pauseTime
high LED4
pause pauseTime
high LED5
pause pauseTime
high LED6
pause pauseTime
high LED7
pause pauseTime
low LED1 ; turn off LEDs
pause pauseTime
low LED2
pause pauseTime
low LED3
pause pauseTime
low LED4
pause pauseTime
low LED5
pause pauseTime
low LED6
pause pauseTime
low LED7
pause pauseTime
next counter ; end of for...next loop
end
If you want different on/off pause times, set two variables - onpauseTime and offpauseTime for example.

#### LesJones

Joined Jan 8, 2017
2,837
I don't know the programming language you are using but I would guess that instead of putting a literal value after the pause instruction you could put a symbolic value in it's place. you could then set that symbolic value at the start of the program. If you had an A to D converter on your PICAXE you could set the value of the variable to the output value from the A to D. Then connecting a variable voltage to the A to D input you could change the delay while the program was running.

Les.

#### YeMa

Joined Apr 4, 2017
5
Use a variable for the pause time...

Code:
symbol LED1 = B.1 ; rename outputB1 LED1
symbol LED2 = B.2 ; rename outputB2 LED2
symbol LED3 = B.3 ; rename outputB3 LED3
symbol LED4 = B.4 ; rename outputB4 LED4
symbol LED5 = B.5 ; rename outputB5 LED5
symbol LED6 = B.6 ; rename outputB6 LED6
symbol LED7 = B.7 ; rename outputB7 LED7
symbol counter = b0 ; define a counter using variable b0

main:
gosub pattern
goto main

pattern: ; all LEDs light up one after another

pauseTime=1000
.......
If you want different on/off pause times, set two variables - onpauseTime and offpauseTime for example.
Thank you for your quick response. This is one option I was thinking about. However, it answers only half of my question. I need this pauseTime variable to change over time. E.g. from 1000 up to may be 10000 in increments of 1000. During the first iteration it is 1000, during the second iteration it should become 2000, third iteration - 3000 and so on. May be I should use For ... Next loop? But I have no idea how to build this into this loop. Cheers!

Last edited by a moderator:

#### LesJones

Joined Jan 8, 2017
2,837
Each time round the loop add some fixed constant (Or another variable.) to the variable that defines the delay time. If you want it to go back to the starting value do a test of the value of the first variable and if it exceeds a certain value then set it back to the starting value.

Les.

#### djsfantasi

Joined Apr 11, 2010
7,210
Here is some sample code to show what we’re suggesting!

Code:
// at beginning of program
maxpauseTime = 10000
pauseTime = 0
...
main()
...
while(-1) { // loop forever
pauseTime = pauseTime + 1000
if (pauseTime > maxpauseTime) pauseTime = 1000
// display LEDs
...
}

#### YeMa

Joined Apr 4, 2017
5
I don't know why but I am getting a syntax error when I try to embed your piece of code into my previous code.
Below is a shortened version of the initial code. I am also attaching a screenshot of the error message. :-(

C:
symbol LED1 = B.1        ; rename outputB1 LED1
symbol LED2 = B.2        ; rename outputB2 LED2
symbol LED3 = B.3        ; rename outputB3 LED3
symbol LED4 = B.4        ; rename outputB4 LED4
symbol LED5 = B.5        ; rename outputB5 LED5
symbol counter = b0    ; define a counter using variable b0
symbol maxpauseTime = b1
symbol pauseTime = b2

maxpauseTime = 10000
pauseTime = 0

main:
gosub pattern0
goto main
end
while(-1) { // loop forever
pauseTime = pauseTime + 1000
if (pauseTime > maxpauseTime) pauseTime = 1000
// display LEDs
pattern0:                ; all LEDs light up one after another
for counter = 1 to 3    ; start a for...next loop
high LED1        ; turn on LEDs
pause pauseTime
high LED2
pause pauseTime
high LED3
pause pauseTime
high LED4
pause pauseTime
high LED5
pause pauseTime
low LED1        ; turn off LEDs
pause pauseTime
low LED2
pause pauseTime
low LED3
pause pauseTime
low LED4
pause pauseTime
low LED5
pause pauseTime
next counter        ; end of for...next loop
}

#### Attachments

• 116.6 KB Views: 3
Last edited by a moderator:

#### djsfantasi

Joined Apr 11, 2010
7,210
We may be using different implementations if the language. I’m used to seeing a semicolon after each statement. Your original code doesn’t have that, so I think there are slight differences in language.

I usually look for mismatched braces or parentheses first. But yours seem to be matched.

However where the error message is pointing, a quick check would be to add a space between the while keyword and the opening paren.

Try that?

#### twohats

Joined Oct 28, 2015
177
Try it without the negative (- ) sign.
Good luck..