code question

bluejets

Senior Member
New to this so if it has been covered before, please redirect.
Trying to get to the meaning of init:
Wherever I see it in the manuals, it is used in conjunction with an example of another label such as below. I can't seem to find any real explanation. I can only imagine it sets up the internals somehow but is it always necessary?

Also, is there difference for servo and servopos and if so, what?

init: servo 4,75 ‘ initialise servo
main: servopos 4,75 ‘ move servo to one end
pause 2000 ‘ wait 2 seconds
servopos 4,225 ‘ move servo to other end
pause 2000 ‘ wait 2 seconds
goto main ‘ loop back to start
 

westaust55

Moderator
Init: is a frequent abbreviation for Initialise:.
This is the section of code which is typically only run once to set up (initialize) various parameters and get outputs into a pre defines state etc.

By comparison, Main: will typically reflect the start of the main program code that could be a looping section of code used many times.



With respect to Servomotors,

SERVO is the intialisation command that sets up the timers for the pulses at 20ms intervals.
SERVOPOS is a command that allows you to change the Servomotor position without resetting the timer activity (which might cause jitter and other problems). SERVOPOS can only be sued after a corresponding SERVO command has first been given.

Have a close read of PICAXE manual 2 page 176 onwards which describe these commands in more detail
 
Last edited:

slimplynth

Senior Member
Hello Bluejets

Using 'init:' makes it clearer to a human, reading your code that the init section is ran once at bootup, so to speak and not used again until the picaxe is powered off then on again.

you could replace init with 'runonce:' or any other sensible label name (like.. 'on_your_marks:' :)), so long as you don't use a reserved word it will still work but might not be as obvious to anyone else (including your future self) what the intention was originally.

I think it's best to use 'init:' for efficiency and to avoid any possible ambiguity.
 
Top