First Ever Bit of Code! Does it look ok?

sketchy

New Member
Hi again
Thanks for all your anwers in my last post.
I have read the manual and although i still do not understand alot of it i have had a go at wirting my first bit of code.

I do not have the chips set yet so i have just run it on the simulator. It seams to run ok but i just wonderd what you guys thought of what i have writen. Is there an easyer wat of acheving my goal and have i understood the manual.

The following code shoud: When my micro switch connected to pin 1 is pressed and relesed the cuircit should turn on and chase up and down the leds untill the button on pin1 is pressed again and then it should switch off.

The only problems i can see is a delay on the last led before the cuircit returns to the start so it is not as smooth as i hoped.

Is this the best way to turn the cuircit on and off.

Code:
main:
		button 1,1,200,100,b0,1,test
		                        'jump to cont unless pin1 = 0
		
		
		goto main


test:	
		high 0
		low 0
		high 1
		low 1
		high 2
		low 2
		high 3
		low 3
		high 4
		low 4
		high 5
		low 5
		high 4
		low 4
		high 3
		low 3
		high 2
		low 2
		high 1
		low 1
		high 0
		

cont: button 1,1,200,100,b0,1,test1

goto test

test1: end
 
Last edited:

alband

Senior Member
Seems OK. I've run it on mine and it works.
You're using the button command to debounce, yes?
This is fine.
Another option would be:
Code:
main:		if pin1 != 1 then main' waits untill pin1 is high
		pause 10' waits a bit for any bouncing to occur
deb:		if pin1 = 1 then deb' waits untill switch is released
But as you can see this is longer, so if you are confident with the button command then that's great.

"Select, copy and paste this sentence into a new reply ^."

The button above the arrow in that sentence allows you to put code in that greyish box. Doesn't make much difference, but makes it a little clearer where code is. It also lets long code fit in a short space.
:)
What chip are you planning on using?
 

sketchy

New Member
Hi thanks for your reply

I was thinking of using a 28 or two 14s as i want to do a few things.

i dont really understand why there seams to be different ways to to the same thing and how to know which one is better. i also found that switch on switch off works.

If i wanted one button to perform a different task each time it was pressed is this the best way to go about it? for example if it is pressed once it goes to one bit of code then if it is pressed again another bit of code and so on. but i cannot see how to do this if the pin will only see 0 or 1

thanks for the tip on the code box
 
Last edited:

alband

Senior Member
As always there are a number of ways. The shortest an simplest is the one to go for, unless there are other limiting factors.
As your code is right now, that button is doing two things just because of the nature of the code. Using this system however is entirely linear. e.g.
Code:
if button 1 is pressed do lights flash
then if button 1 is pressed do lights on
then if button 1 is pressed do lights off
then if button 1 is pressed go back to the start
In this code it is impossible to jump to the previous step or jump two steps ahead.
This will always be the case (I think) unless you want to do things with timing e.g.
Code:
if button is pressed for 1s then do this
but if it is pressed for 2s do that
Something else you could concider, is the use of interrupts. Check the manual for details but the basic idea is that if the PICAXE senses a certain input combination (e.g. that button) then it will jump to another bit of code no-matter where it is at the time. Using this you could increment a variable from 1-3 to allow the main code to decide what procedure to run.

If you are going to have more than say 5 procedures that you want that one button to control, I'd use an interrupt. Any less than that and it's your choice.

What chip also depends on your needs. As a starting chip the 08M is worth it; get a couple. For any particular application, try to use the smallest chip. In this application you require 6 outputs so a 14M would do (you could use a 08M if you wanted to but a 14M would be easier). A 28X isn't necessary here but makes a good chip when you get onto more advances stuff so you may want to consider getting one now. If you do get one now, you might as well get the resonator it needs at the same time.
 

eclectic

Moderator
@ sketchy.

Can I check?

Is this for your coursework or
is it for REAL?
As in dark / damp / dangerous.

The advice you'll receive here might vary, depending on your answer.

Coursework = All sorts of interesting gadgets.

Real = Simple / Solid / Safe.

E

The photo is one of the prototypes I made last year.
 

Attachments

sketchy

New Member
Hi eclectic

I am afraid i am a bit to old for college.
This is for dark / damp / dangerous ride to work
i know nothing about electronics but after doing some google searches and finding this site i have become facinated. If i can do this then there are so many thing i could try. I am a maintenace worker and have always avoided electronics as i did not understand it but after reading this it seams quite logical for the most part. i could really get in to this.

Thanks for all your replys so far you are all so helpful.

Joe
 

sketchy

New Member
Hi Alband

Sorry if this is a stupid question but should the code you have written work if i copy it and use the simulator.

I tryed to run it after writing lables and actions for lights flash, lights on, lights off but it says there is a syntax error in the first line of code "if button 1 is pressed do lights flash"

Code:
if button 1 is pressed do lights flash
then if button 1 is pressed do lights on
then if button 1 is pressed do lights off
then if button 1 is pressed go back to the start

Start:

goto start

lights flash: high 1
              Low 1
              goto lights flash
              
lights on:    high 2

Lights off:   low 2
              low 1
i am not sure if i can use the code you wrote of ir it was just an example.

Thanks

Joe.
 
Last edited:

alband

Senior Member
Sorry!
That was just normal language, not PICAXE code.
Code:
if button 1 is pressed do lights flash
then if button 1 is pressed do lights on
then if button 1 is pressed do lights off
then if button 1 is pressed go back to the start
Would become:
Code:
main:       button 1,1,200,100,b0,1,flash
goto main
flash:      (whatever code is for the lights flashing)
button 1,1,200,100,b0,1,on
goto flash
on:         (whatever code is for lights on or "%")
button 1,1,200,100,b0,1,off
goto on
off:         (whatever code is for lights off or "%")
button 1,1,200,100,b0,1,main
goto off
Where I've put the "%" sign. Currently, I assume since you're new to PICAXE, you wont have come across the "pins=" command. Look it up and see if you can work out how it would be useful where "%" are marked in the code.
 

eclectic

Moderator
Yet more alternatives.

I've pinched some code from the RevEd samples .

All I've done is added a few lines for the Interrupt bits on pin1.
It looks very pretty, both in the simulator and for real.

Code:
'led_test.bas
'Generate pretty LED patterns.
'Author unknown, modified by CPS May '98.

'This is the test program downloaded to
'Revolution's Stamp Controller to test the
'pcb for correct operation before despatch.

'Load some pretty patterns into EEPROM memory.
eeprom ($0,$0F,$F0,$3C, $C3,$18,$3C,$7E, $FF,$E7,$C3,$81)
eeprom ($0,$03,$06,$0C, $30,$60,$C0,$60, $30,$0C,$06,$03, 0)

'Define symbols
symbol i = b0
symbol j = b1
symbol k = b2

'Define time delays
symbol const1 = 50
symbol const2 = 150

'Main program

setint %00000010 , %00000010 '************ SWITCH on pin1

looper:   For i = 0 To 7          'bar climbing
           pause const1
           high i
        Next i

        For i = 0 To 7          'and disappearing...
           pause const1
           low i
        Next i

        Let j = 1               'single light moving up
        For i = 0 To 7
           Let pins = j
           pause const1
           Let j = j + j
        Next i

        Let j = 64              'and rolling back
        GoSub roll_back
        
        Let j = 3               'bar climbing up
        For i = 0 To 6
           Let pins = j
           pause const1
           j=j+j|1
        Next i

        j = 127                 'and rolling back
        GoSub roll_back

        For i = 0 To 24         'some more effects
           read i, j
           Let pins = j
           pause const2
        Next i

        goto looper               'looper forever!

'sub-procedures
roll_back:
        For i = 0 To 6
           Let pins = j
           pause const1
           j = j / 2
        Next i
        Return




Interrupt:
pins = %11111111

Brakeon:
if pin1 = 1 then brakeon

setint %00000010 , %00000010
return
OK sketchy, it's now up to you.

Keeping in mind that you'll need a serial/USB cable,
connectors, etc etc,

Wallet time! :)

e
 

sketchy

New Member
Thats great

Thanks for all your help.
Its very useful to see different ways code are written so you can play about with it and see what effect it has.

inerupt is very handy as is let pins =

Lots of playing to do now.

Thanks again

ps. What is the eeprom bit at the top for. if i delet it the program runs the same.
 

manuka

Senior Member
Sketchy- you're doing so well. that at this rate mastery looks in your grasp by Xmas! Take your time & learn as you go however-most of us have put in many 100s of hands on hours with PICAXEs. Fast learners, such as nameless UK forum regulars, have burnt the midnight oil over BUTTON commands & the like. Even for us clear thinking colonials, insights into EEPROM (storage in the non volatile PICAXE RAM) can take up much of a wet afternoon!

My own infamous "downunder" approach revolves around ideas shown => www.picaxe.orconhosting.co.nz Have you run across David Lincoln's PICAXE book? Stan
 

alband

Senior Member
ps. What is the eeprom bit at the top for. if i delet it the program runs the same.
I don't know. I would think clectic does but I'm gonna have a go.
Page 40 in manual 2 say:
eeprom (data)
Syntax:
DATA {location},(data,data...)
EEPROM {location},(data,data...)
- Location is an optional constant (0-255) which specifies where to begin
storing the data in the eeprom. If no location is specified, storage continues
from where it last left off. If no location was initially specified, storage begins
at 0.

- Data are constants (0-255) which will be stored in the eeprom.
Function:
Preload EEPROM data memory. If no EEPROM command is used the values are
automatically cleared to the value 0. The keywords DATA and EEPROM have
identical functions and either can be used.
But, that code has no {location}, so currently, I think it does nothing. :confused:

Edit::eek: Oh, huh, whops. I've just added the bold. Now I need to work out what the stuff you added means...
 
Last edited:

alband

Senior Member
Code:
eeprom ($0,$0F,$F0,$3C, $C3,$18,$3C,$7E, $FF,$E7,$C3,$81)
eeprom ($0,$03,$06,$0C, $30,$60,$C0,$60, $30,$0C,$06,$03, 0)
Right, using word, I've managed to translate it into this:
Code:
 ,*,ð,<, Ã,*,<,~, ÿ,ç,Ã,*
 ,*,*,*, 0,`,À,`, 0,*,*,*, 0
Every * is a symbol I couldn't get on word.

eclectic: what does this mean?

Edit: the bottom one is symmetrical.
 
Last edited:

statch

New Member
The eeprom data looks to just be bit patterns used at the end of the main loop (look for the "read" command) to light up certain LED's. Convert the hex to binary and a "1" means the led is on.

Daniel
 

lbenson

Senior Member
The hex is just an easy way to set the pattern of bits which is on. It is clearer if you look at the binary which the hex represents: $0 = &0000000 = no leds on;$0F = %00001111 = lower 4 leds on; $F0 = %11110000 = top 4 leds on; $AA = %10101010 = every other led on.
 

westaust55

Moderator
Sketchy,

Here is a small program I wrote as one of my first to drive a set of 8 LED's with one LED on at a time in a back and forth pattern using a shift register.

By changing the out-595 subroutine to directly act upon the PICAXE pins and setting the delaytime value to whatever you want you can use it however you want.

For less LED's change the line
loop until pattern=%10000000​

Use what you want and have fun . . .

Code:
; test program to drive 8 LED via a 74HC595
; program uses the shiftout command instead of pushing bits out one at a time
; lit LED starts at the LSB (right) side and steps to the MSB (left) side
; then steps back again to the LSB side. Continues back and forth forever . . .

symbol clock = 3          ; clock on Output 3
symbol serdata = 5        ; data output on Output 5
symbol latch = 4          ; latch to 595 outputs with high pulse on Output 4
symbol bitz = 8           ; number of bits to send
symbol msb_1st = 1        ; 1 = MSB first and idle state is low
symbol pattern = b20      ; variable to hold LED pattern
symbol delaytime = b21    ; delay between 'stepping' to next LED pattern


init:     low latch
	  low clock
         pattern = %00000001
         delaytime = 500

main:
	DO
		GOSUB out_595
		pause delaytime
		pattern = pattern <<1
	loop until pattern=%10000000

	DO
		GOSUB out_595
		pause delaytime
		pattern = pattern >>1
	loop until pattern=%00000001
	goto main

out_595:
	shiftout clock,serdata,msb_1st,(pattern/bitz)
	pulsout latch,5
	return
 
Last edited:

Wrenow

Senior Member
Hi thanks for your reply

i dont really understand why there seams to be different ways to to the same thing and how to know which one is better...
Ah, the eternal question. One of the wonders of the Picaxe is that it is so flexible, you can often do a particular task numerous ways. A wonderful plethora of choices. What is "best" is often a mater of the circumstances surrounding that task, and often it is a matter of "style". As already noted, simplest and fewest steps is often considered "best", but self-documenting and easy to understand is also an important step to "best" to most (and you will be glad you did when you try to figure out, six months from now, what you were thinking at the time you wrote it).

Think of getting a banana for your cereal. You have a lot of choices to choose from. First - go to the store to pick one up, or order one, or grow your own. Each of those leads to other choices (which store, jog, or ride, bike or car, etc.) with different costs/benefits. What is the best way to get a banana for your cereal? And is the best, for you, in your circumstances (the grocer is next door, so you choose walk, good choice) the same as for the guy in Puerto Rico, who has a banana tree in his back yard, or the guy in Alaska who lives 50 miles from the nearest store?

One of these decisions, in you case, is whether incorporating particular aspects of your project into one unit or multiple units is better (headlight and turn signals and scanner all in one unit, or headlight a separate unit, or headlight, turn signals, and scanner all discrete but linked, or unlinked). Some decisions will simplify your construction, others complicate it. Some may simplify in one area and complicate in another at the same time.

A good thing to remember - to a man with a new hammer, every problem begins to look distinctly nail-like. Sometimes a screwdriver is a better choice.

End of philosophical discussion/digression, I now return you to your regular channel,:)

Cheers,

Wreno
 
Last edited:

manuka

Senior Member
Wrenow: Good comparisons-how about extending those bananas & hammers to include Texans as well!

Programming "style" is perhaps akin to composing music, but possibilities are somewhat constrained in our field by the low(ish) speed & tight memory of most PICAXEs. Although traditional approaches should initially be followed,all manner of code bashing styles can be viable - just as there's arguably no single best way to strum a guitar, beat a drum or even play a piano. Code innovation is often applauded- much in the musical style of cannons in Tchaikovsky's "1812 Overture", or violins in Beatles songs such as "Yesterday".

Extra: Expect scorn from old hands if you play a bum note however!
 
Last edited:

Wrenow

Senior Member
Hi Stan,

Of course, we Texans are bananas (well, some of us are, anyway), but I don't think that is what you meant.

By "extending," are you asking me to include how a Texan would get a banana (one choice, there... we drive to the store, even if the grocer is down the block, of course) and hammers (all problems ARE nails - if your hammer doesn't fix it, you just need a bigger hammer).

By the way, in the decision tree, I left out fly. For many in Alaska, they have to fly to the grocery store (although he could fly to visit the guy in Puerto Rico with the banana tree in his back yard, that is not terribly practical).:D

Or are you suggesting a decision tree that tells you wat to do about/with Texans (if you figure that one out, let me know - got millions of 'em 'round hereabouts - including the one that stares at me from that mirror when I brush my teeth every morning)? ;)

I agree with you that a nice, tight, innovative piece of elegant code is a work of art to be appreciated (and here it seems to be) and perhaps even celebrated (as oft noted, I am easily amused).

But, writing a program is a lot like writing a story. you have to get the grammar right, and use the right words and parts of speech and syntax and punctuation to convey the message, but there are still lots of ways to write a story.

Speaking of easily amused, anyone hear when the 28X2 module pictured on the Picase store site is going to be available ans/or cost? I have a future project that this might be just the ticket for. Looks to be a handy-dandy shortcut to some additional prototyping possibilities.

Cheers,

Wreno
 

westaust55

Moderator
The eeprom data looks to just be bit patterns used at the end of the main loop (look for the "read" command) to light up certain LED's. Convert the hex to binary and a "1" means the led is on.

Daniel
Spot on there for the EEPROM program statements.

Have a look at this table which shows the conversions between decimal, hex, binary numbers and ASCII character codes.
http://www.picaxeforum.co.uk/showthread.php?t=10893

If you look at the hex value then read of the binary number you will see what the LED pattern will look like.

I had back in Oct suggested to Rev Ed that they add such a table into their manuals but latest rev (6.6) was recently published but no such table included.
 
Top