First Program Questions (lots of 'em)
Armored Cars
Posts: 172
Okay I wrote my first program today (attatched) and had a few questions.
First are inputs, in all the programs I look in·it doesnt have an INPUTS list.· I didnt know if they were listed under somthing else like VARIABLES, I noticed there were different types of variables, like nib and byte and wasnt sure if you put the pin number after VAR?
My next question is what does nib and byte mean?· I need a·variable·that·stores a number,·in my program it backs up and adds one to a variable each time it loops.
In some programs I noticed that the servo PULSOUT was included in loops, if the command PULSOUT is given to a servo will it hold its position until the next PULSOUT command for that pin?
Also just wanted to verify that when an input·pin has electricity flowing through it,·it reads 0 and when it·has no voltage it·reads 1.
My program has no end since it is constantly looping, is this ok or do I need to tack one on the end of the program?
I think thats all for now, feel free to tell me everything thats wrong with my program (I need it) I can work out the logic, mainly I just need help with getting the first part (variables, ect) done.
First are inputs, in all the programs I look in·it doesnt have an INPUTS list.· I didnt know if they were listed under somthing else like VARIABLES, I noticed there were different types of variables, like nib and byte and wasnt sure if you put the pin number after VAR?
My next question is what does nib and byte mean?· I need a·variable·that·stores a number,·in my program it backs up and adds one to a variable each time it loops.
In some programs I noticed that the servo PULSOUT was included in loops, if the command PULSOUT is given to a servo will it hold its position until the next PULSOUT command for that pin?
Also just wanted to verify that when an input·pin has electricity flowing through it,·it reads 0 and when it·has no voltage it·reads 1.
My program has no end since it is constantly looping, is this ok or do I need to tack one on the end of the program?
I think thats all for now, feel free to tell me everything thats wrong with my program (I need it) I can work out the logic, mainly I just need help with getting the first part (variables, ect) done.
bs2
3K
Comments
Not bad for a first try ))))!
A Byte is the main variable type for the Stamp. It has 8 bits in it and can represent 256 values (0 to 255). Nib is 4 bit and has 16 values, Word is 16 bit and represents 65536 values.
So the 'span' of Nib-Byte-Word goes up from 16 to 65536, but the amount of RAM memory goes up as well !!
By the way, do you have the newest version of PBASIC 2.5 ?? I can recommend it as you will be far more flexible in formatting your program using GOSUB and IF-THEN-ELSE.
Regards,
Klaus
What is the difference between GOTO and GOSUB?
Can multiple tasks be put on one line like this:
IF fpt=1 THEN PULSOUT drive, brake GOTO GoBackPT
or
IF fpt=1 THEN PULSOUT drive, brake GOTO GoBackPT END
or
IF fpt=1 THEN
PULSOUT drive, brake
GOTO GoBackPT
END
(last one is how my calculator does it)
didnt know if I needed commas in the first two or if the ENDS would terminate the program or try to exit the subroutine instead of going to the next command.
GOSUB allows you to RETURN to the next line in the program from where the GOSUB originated.
Usually used in a conditional branch where you want to return and continue where you left off
prior to initiating the GOSUB.
"Can multiple tasks be put on one line..."
You can use multiple tasks on one line, but they must be separated with a ‘:’... (I think)
The last method you use is my personal preferred method, just because it makes things so much easier to read,
however in your example the IF terminator for PBASIC is 'ENDIF'.... You may also want to pursue the "ELSE" as
another type of conditional branch within the IF/THEN statement that you can use.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe - Mask Designer III
National Semiconductor Corporation
(Communication Interface Division)
500 Pinnacle Court, Suite 525
Mail Stop GA1
Norcross,GA 30071
You're really close to having something working.
Servos require pulsout refresh every 20 milliseconds.
In general your code should run fast enough that·the pulsouts will be repeated
fast enough that they will act the way you want. You may have to do a little
fine tuning in how your code flows, but you'll discover that as you learn.
The exception is where you have programmed PAUSE xxxx. Noted below.
As already noted, bits are 1 bit, ie 1 or 0
···· nib is 4 bits and may be addressed as the upper or lower half of a byte.
··········· a nib can be a number from 0 to 15.
···· byte is 8 bits and can be a number from 0 to 255.
···· word is 16 bits and can be a number from 0 to 65535.
Inputs read a 1 if they are > 1.4 volts
Inputs read a 0 if they are <·1.4 volts
GOTO is just what it says, GO TO.
GOSUB "calls" a subroutine that must end with RETURN. Then execution continues at the
next line after the GOSUB.
The "subroutine" is usually just a few lines of instructions and is used when you
want to do the same thing over and over. So instead of writing the same code over
and over, you place it in a subroutine and just "call" it when needed. It saves valuable
space for more program.
I've spotted a few other things and have added comments to your code.
Hope this gets you a little further,
Dennis
'---FORWARD ROUTINES---
· MAIN:
··· PULSOUT drive, brake
··· PULSOUT steer, center
··· PAUSE 1000··················· 'your servo will·go to it's "at rest" position if you don't
······································· ' send pulsouts about every 20 milliseconds, so you will have
······································· ' to do some work here. I understand the reason for the pause,
······································· ' give the steering time to get to position, etc. But if you just
······································· ' do nothing for 1 second, the srvo goes to it's "at rest" position.
· GOFWDUNSTUCK:
··· PULSOUT drive, fwd
· GOTO GoFwd
· GOFWD:
··· IF fpt=1 GOTO GoBackPT
··· IF fsb=1 GOTO GoBackSB
··· IF fenc<encmin GOTO GoFwdStuck
· GOTO GoFwd
'---BACKUP ROUTINES---
· GOBACKPT:
··· backcount=0
· BACKPT:
··· PULSOUT drive, brake
··· PULSOUT steer, port
··· PAUSE 1000············· 'again, you must repeat pulsout at least every 20 milliseconds
'
··'try this in place of the 3 lines above
··cnt var byte
··for cnt=0 to 50············· '1000ms / 20ms = 50
····· PULSOUT drive, brake
····· PULSOUT steer, port
····· PAUSE 20
··next
'
· GOBACKUNSTUCK:
··· PULSOUT drive, revs
· GOTO GoBack
· GOBACKSB:
··· backcount=0
· BACKSB:
··· PULSOUT drive, brake
··· PULSOUT steer, starboard
··· PAUSE 1000························· 'see above, same problem
··· PULSOUT drive, revs
· GOTO GoBack
· GOBACK:
··· IF rall=1 GOTO Main
··· IF fenc<encmin GOTO GoBackStuck
··· backcount+1=backcount········ 'should be backcount = backcount+1 OR backcount+=1
··· IF backcount=1500 GOTO Main· 'clear backcount to zero before going back to main
· GOTO GoBack
'---STUCK ROUTINES---
· GOFWDSTUCK:
··· PULSOUT drive, fwdstuck
· FWDSTUCK:
··· IF flip=1 GOTO GoCrash
··· IF fpt=1 GOTO GoBackPT
··· IF fsb=1 GOTO GoBackSB
··· IF fenc>encmin GOTO GoFwdUnstuck
· GOTO FwdStuck
· GOBACKSTUCK:
··· PULSOUT drive, revstuck
· BACKSTUCK:
··· IF flip=1 GOTO GoCrash·········
··· IF rall=1 GOTO Main
··· IF fenc>encmin GOTO GoBackUnstuck
··· backcount+1=backcount················· 'repeated problem, see above
··· IF backcount=1500 GOTO Main········· 'repeated problem, see above
· GOTO BackStuck
'---MISCELLANIOUS ROUTINES---
· GOCRASH:
··· PULSOUT drive, brake
··· PULSOUT steer, center
··· led=1
· CRASH:
··· IF flip=0 GOTO UnCrash
· GOTO Crash
· UNCRASH:
··· led=0
··· PAUSE 2000··············· 'this is probably ok here since you don't care yet to have
··································· ' steering and speed/direction until main
· GOTO Main
·
What happens though, is that if the servo is hard left and you send a pulse to make it go hard right, it will only go a little bit of the way with each pulse. So it may take 50 or so pulses to get it all the way to hard right.
Each time you send the servo a pulse is tries to go to the position for that pulse, so if you keep sending it pulses and try to move it manual, it will "fight back". If you are not sending pulses, it doesn't "fight back".
Terry
The main question I have now is with the inputs and outputs, are they right? Is there any thing else I need to add for it to recognize pins 1-4 as inputs and 13-15 as outputs?
'---CONSTANTS---
fwd CON 800 'Forward (half throttle)
brake CON 750 'Brake
revs CON 700 'Reverse (half throttle)
port CON 1000 'Steer Port
center CON 750 'Steer Center
starboard CON 500 'Steer Starboard
'---VARIABLES---
bigcount VAR Word
littlecount VAR Byte
'---INPUTS---
fpt CON 1 'Front Port Whiskers
fsb CON 2 'Front Starboard Whiskers
rall CON 3 'Rear Whiskers (RearALL)
flip CON 4 'Flip sensor (tells if car is upside-down)
'---OUTPUTS---
drive CON 15 'ESC, controls drive motor
steer CON 14 'Steering Servo
led CON 13 'LED
'PROGRAM
'---FORWARD ROUTINES---
MAIN:
FOR littlecount=0 TO 75
PULSOUT drive, brake
PULSOUT steer, center
PAUSE 20
NEXT
GOFWD:
PULSOUT drive, fwd
PULSOUT steer, center
IF fpt=1 THEN GOTO GoBackPT
IF fsb=1 THEN GOTO GoBackSB
IF flip=1 THEN GOTO GoCrash
GOTO GoFwd
'---BACKUP ROUTINES---
GOBACKPT:
FOR littlecount=0 TO 75
PULSOUT drive, brake
PULSOUT steer, port
PAUSE 20
NEXT
FOR bigcount=0 TO 1500
PULSOUT drive, revs
PULSOUT steer, port
IF rall=1 THEN GOTO Main
IF flip=1 THEN GOTO GoCrash
NEXT
GOTO Main
GOBACKSB:
FOR littlecount=0 TO 75
PULSOUT drive, brake
PULSOUT steer, starboard
PAUSE 20
NEXT
FOR bigcount=0 TO 1500
PULSOUT drive, revs
PULSOUT steer, starboard
IF rall=1 THEN GOTO Main
IF flip=1 THEN GOTO GoCrash
NEXT
GOTO Main
'---MISCELLANIOUS ROUTINES---
GOCRASH:
HIGH led
FOR littlecount=0 TO 50
PULSOUT drive, brake
PULSOUT steer, center
PAUSE 20
NEXT
CRASH:
IF flip=0 THEN
PAUSE 2000
LOW led
GOTO main
ENDIF
GOTO Crash