Declare an Array in picaxe basic?

I want to make several led's blink in one direction the the other, i figured the best way to do this would be to have the output pins in an array then use a 'for next loop' to loop thru them.

But the manual dosen't say how to declare an array, so how do you declare an array?

Using Picaxe 28x1

 

table, eeprom (or scratchpad)

You can use the table command for this (see picaxe manual 2). The table command use the program memory so if you use it, your maximum program size will be 256 bytes less.

This example initializes some values in the picaxe memory and loops through them sending them to the debug screen:

table 0,(10,20,30,40,50,60,70,80)

main:

for b0=0 to 8

readtable b0,b1

pause 1000

debug

next b0

 

if you need those 256 bytes of program space you can also use EEPROM:

EEPROM 0,(10,20,30,40,50,60,70)

main:

for b0=0 to 8

read b0,b1

pause 1000

debug

next b0


What no array’s ?

Mmmm. thats not good, having arrays would be so much better.

I’ll have to give the ‘table’ command a try.

So i’m thinking something like this to cycle thru pins 0-4 to get them to blink forwards.

main:
table 0,(10,20,30,40,)

for b0 = 0 to 4
high b0
pause 500
low b0
next b0

My next question would be how do i get them to blink backwards?(like a cylon, fowards then backwards).

maybe ?

b1 = 4 to 0

high b1

pause 500

low b1

next b1

goto main

???

You might want to try:FOR b1

You might want to try:

FOR b1 = 0 to 4
HIGH PORTC b1
PAUSE 500
LOW PORTC b1
NEXT b1

main:table 0,(10,20)for b0 =

main:
table 0,(10,20)

for b0 = 0 to 2
high b0
pause 500
low b0
next b0

for b1 = 0 to 2
high portc b1
pause 500
low portc b1
next b1

goto main

For simplicitiy sake i made it only 3 led’s for testing.

For now it only goes forward, still stumped on how to make it go backwards.

I had this same issue with a led “array” :slight_smile:
I ended up using a I2C I/o expander. The “writes” to the I2C line were done in binary --this made it easier.

Ok I gotta ask, whats a I2C
Ok I gotta ask, whats a I2C I/O expander ?

It’s a type of serial <->

It’s a type of serial <-> parallel converter that operates on the I²C serial protocol.
When running in parallel output mode it’s got two inputs, one for the clock signal and the other for data, so it normally takes up two of your microcontroller digital output pins. The I/O expander itself has a number of output pins, often 8 or more. When the microcontroller sends data to the I/O expander, it tells the expander which pins to turn on or off. The I/O expander usually switches all the outputs at once (we’d say they’re acting as ‘latched’ outputs), so the overall effect is that for the cost of two microcontroller output pins you can have a theoretically infinite number of extra digital outputs. This is obviously pretty handy, but the downsides are:
• You have to buy an extra part (not very expensive usually).
• Takes up more space on your circuit board.
• The more outputs you add, the longer it takes to send the serial data to control them, so the maximum speed of the outputs is reduced.
Often the disadvantages are pretty insignificant though, but it’s good to keep them in mind.

If you only need a few extra output pins you can use a simple serial to parallel converter or even a 3-8 decoder/demultiplexer, which for a generic 74 series IC only costs around $0.5US each.

EDIT: If you’re only using 8 LEDs, and they’re only turning on one at a time, take a look at the 74HC138 3-8 decoder. It can output enough current to power an LED directly, and all you have to do is output a 3 bit signal to choose which output pin to turn on.

no table

if the pins you are using are 0-8 without any gaps, than you dont need an array, table or eeprom.

to go backwards try:

 

for b1=3 to 0 step -1

do blink stuff …

next b1

Mmm, they still only go

Mmm, they still only go forward, here’s my code so far.

main:
table 0,(10,20)

for b0 = 0 to 2
high b0
pause 500
low b0
next b0


for b1 = 2 to 0 step -1
high portc b1
pause 500
low portc b1
next b1

goto main

portc?

Why do you use PORTC in the second loop. high portc 1 sets a different pin than high 1. I’d expect your code to flash LED 0 then 1 then 2, then waits for a bit because the second loop doesn’t do anything and then it starts over: correct? Remove the portc from the second loop.

[EDIT] Also: you can remove the table command as well (unless you’re using it elsewhere in the program)