a couple questions... let and pulsin

jobarrco

New Member
if i want to define a variable, do i have to write
let w0 = 15

or can i just type

w0 = 15

and i want to build a tachometer for a single cylinder engine (for now)...
but it's going to be a while. for now i'm playing with the pulsin command.
i'm using a 555 timer to provide the pulse and debug to view.
my question is if i have pulsin 1,0,w0, it reads the time that pin 1 is low between pulses, correct? and writes that time in units of 10 us to w0 (4 Mhz). Does it "refresh" w0 each time the program loops? as in if there is no pulse will w0 go back to 0?

thanks for the help, i appreciate it!
this is all new to me... i'm studying mech. engr. but wish it was mechatronics. so much more fun. :D
 

Andrew Cowan

Senior Member
if i want to define a variable, do i have to write
let w0 = 15
or can i just type
w0 = 15


Try it! (Or incase you can't be bothered, you can type w0=15).

and i want to build a tachometer for a single cylinder engine (for now)...
but it's going to be a while. for now i'm playing with the pulsin command.
i'm using a 555 timer to provide the pulse and debug to view.
my question is if i have pulsin 1,0,w0, it reads the time that pin 1 is low between pulses, correct?


It starts the timer at a high to low transition, and stops it at a low to high transition. So yes.

and writes that time in units of 10 us to w0 (4 Mhz). Does it "refresh" w0 each time the program loops? as in if there is no pulse will w0 go back to 0?

Correct

Andrew
 

BeanieBots

Moderator
The "Let" statement is optional.
So simply writing:-
w0=15
will be fine.

You do not need to reset the variable each time.
If pulsin does not see a pulse within ~0.65seconds, then it will return zero.

EDIT: Crossed with Andrew's reply.
 

jobarrco

New Member
ok one more question...

let's say i overclock it to 8mhz and use the pulsin command.
i debug w0 and see, let's say 375. how many seconds is that?
is it 375*5 us? so .00185 s...?

thanks!
 

westaust55

Moderator
PULSIN (and other commands)

@Jobarrco,

For your further information on effect of changing the clock speed, have a look at the table atatched to post 15 here:
http://www.picaxeforum.co.uk/showthread.php?&p=86562


and, for those who dare to read the PICAXE manual 2 (Rev6.7), at page 100 it clearly states:
Function:
Perform variable manipulation (wordsize-to-wordsize).
Maths is performed strictly from left to right.
The ‘let’ keyword is optional.
 
Last edited:

Dippy

Moderator
Hey, westy;
A) Do you ever sleep? I thought hippy was the only on-Forum-24hours/day man.
B) Have you written tables for your missus so she knows what to cook on certain days?
:) :)
 

westaust55

Moderator
Hey, westy;
A) Do you ever sleep? I thought hippy was the only on-Forum-24hours/day man.
B) Have you written tables for your missus so she knows what to cook on certain days?
A. sleeping is over rated :rolleyes: - a complete waste of time :eek: - nothing productive gets done :cool:

B. Tables not required :)


Now about to go an do gardening for a while (there is life/work besides PICAXE) and then assemble an 08M protoboard ready for 433MHz radio link experiemnts soon . . . . .
 
Last edited:

tiscando

Senior Member
The "Let" statement is optional.
So simply writing:-
w0=15
will be fine.
I found that I need to have the LET after THEN e.g.
if pin1=1 then let b5=b5 max 100 else b5=b5 min 150 endif
b1=b1*2
...
if an equation b5=??? is straight after THEN, the complier would think it is an address, and comes up with an error.
 
Last edited:

westaust55

Moderator
I found that I need to have the LET after THEN e.g.
if pin1=1 then let b5=b5 max 100 else b5=b5 min 150
b1=b1*2
...
if an equation b5=??? is straight after THEN, the complier would think it is an address, and comes up with an error.

!ERROR - ROORE!
It will not do anything until you add ENDIF after that line.
if pin1=1 then let b5=b5 max 100 else b5=b5 min 150 : ENDIF

and this will work without the LET keyword

if pin1=1 then : b5=b5 max 100 : else b5=b5 min 150 : endif
b1=b1*2



conclusion: READ THE MANUALS
 

tiscando

Senior Member
Oops!
Forgot the ENDIF.
(I often keep forgetting ENDIFs until now - I would always realize this when an error message pops up saying "IF without ENDIF")
I'll edit the post above.
 
Last edited:

tiscando

Senior Member
and this will work without the LET keyword
I meant that if there is no 'let' (or ':' as westaust suggested also) between 'then' and 'b5=...', an error comes up saying "expected a label not a variable".

I usually do a singular IF sentence like this (i.e. one command after THEN and ELSE), which is correct to the complier:
Code:
if pin1=1 then let b5=b5 max 100 else b5=b5 min 150 endif
if pin2=1 then high 1 else low 1 endif
 

Dippy

Moderator
Well, thats something to be remembered then.

Doing all this "If.. then... else.. endif" on one line is not the usual way.
If you keep to the-

IF .... Then
{indented Code}
Elseif ... Then
{indented Code}
Else
{indented Code}
Endif


-structure then life is easier and clearer.
That is also the way exampled in the manual.

You should be proud of your discovery and it something for all to remember.
But if you keep to this 'recommended' (i.e. normal/usual/pro/universal/traditional/everyone else does it) method then all is fine.
AND you will thank yourself in a few months time when re-reading your code, or expect others to read it on this Forum ;)
 

hippy

Ex-Staff (retired)
As with most compilers, they do their best to second guess what the program is meant to do, and "IF-THEN LET b0=0" is enough to help the compiler get it right when "IF-THEN b0=0" is not.

The best practice is as Dippy suggests to use IF-THEN-ENDIF statements over separate lines with appropriate indentation, and when a single line IF-THEN-ENDIF is more desirable to split the parts using colons ...

If pin3=1 Then : b0=1 : Else : b0=0 : EndIf
 
Top