Need help with writing code using Pbasic
Motorheads United
Posts: 5
Im familiar with parallax stuff, I did the microcontrollers and boe bot a while back, and it was awesome· Its been a couple years ago since ive touched it and I need to start writing my own stuff. I scanned the books again last night and am still having trouble with a few things.
In basic terms Im trying to take 1 input from p14, it causes p1 to·trigger p2 to start counting each pulse from P15. After 4 pulses, p14 triggers p1 to reset and·p15 sends out 4 more pulses to p2. ETC it keep going.
Im stumped on how to write it. I keep getting syntax errors and if i try and fix it it still stalls.
Heres what Im trying to do.
Im using P14 outputs to trigger P1 input as a signal wire. When P1 goes high I want it to start a at the beginning of P2
P2 outputs a signal to pin 3 it turns on·then·goes low······································· 'and moves to pin 4
p2·goes high·again and pin 4 turns on then shuts off when p2 goes low·········· 'and moves to pin 5
p2 goes high and pin 5 turn on and then·goes low············································ 'and moves to pin 6
p2 goes high and pin 6·turns on and then goes low·········································· 'thats the end of the cycle
Then it waits for p14 to send a signal and it starts the count again and loops.
Could someone advise me one where im going wrong here. Or even help me write the code.
I know im missing the p14 stuff as I dont know how to wire that in to turn p1 on, And I dont know where to put that into my code.
'- IGT.BS2
'Runs an 1 wire signal wire in and converts to 4 outputs
' {$STAMP BS2}
' {$PBASIC 2.5}
DO
· LOOP UNTIL IN1 = 1························ 'Triggers Count
··· IF (IN2 = 1) THEN······················· '1 IGT
··· HIGH 3
····· IF (IN2 = 0) THEN
····· LOW 3
········ IF (IN2 = 1) THEN·················· ' 2 IGT
········ HIGH 4
··········· IF (IN2 = 0) THEN
··········· LOW 4
··············· IF (IN2 = 1) THEN··········· ' 3 IGT
··············· HIGH 5
·················· IF (IN2 = 0) THEN
·················· LOW 5
···················· IF (IN2 = 1) THEN······ ' 4 IGT
···················· HIGH 6
······················· IF (IN2 = 0) THEN
······················· LOW 6
LOOP
I hope you get what im trying to do, I could really use the help here.
In basic terms Im trying to take 1 input from p14, it causes p1 to·trigger p2 to start counting each pulse from P15. After 4 pulses, p14 triggers p1 to reset and·p15 sends out 4 more pulses to p2. ETC it keep going.
Im stumped on how to write it. I keep getting syntax errors and if i try and fix it it still stalls.
Heres what Im trying to do.
Im using P14 outputs to trigger P1 input as a signal wire. When P1 goes high I want it to start a at the beginning of P2
P2 outputs a signal to pin 3 it turns on·then·goes low······································· 'and moves to pin 4
p2·goes high·again and pin 4 turns on then shuts off when p2 goes low·········· 'and moves to pin 5
p2 goes high and pin 5 turn on and then·goes low············································ 'and moves to pin 6
p2 goes high and pin 6·turns on and then goes low·········································· 'thats the end of the cycle
Then it waits for p14 to send a signal and it starts the count again and loops.
Could someone advise me one where im going wrong here. Or even help me write the code.
I know im missing the p14 stuff as I dont know how to wire that in to turn p1 on, And I dont know where to put that into my code.
'- IGT.BS2
'Runs an 1 wire signal wire in and converts to 4 outputs
' {$STAMP BS2}
' {$PBASIC 2.5}
DO
· LOOP UNTIL IN1 = 1························ 'Triggers Count
··· IF (IN2 = 1) THEN······················· '1 IGT
··· HIGH 3
····· IF (IN2 = 0) THEN
····· LOW 3
········ IF (IN2 = 1) THEN·················· ' 2 IGT
········ HIGH 4
··········· IF (IN2 = 0) THEN
··········· LOW 4
··············· IF (IN2 = 1) THEN··········· ' 3 IGT
··············· HIGH 5
·················· IF (IN2 = 0) THEN
·················· LOW 5
···················· IF (IN2 = 1) THEN······ ' 4 IGT
···················· HIGH 6
······················· IF (IN2 = 0) THEN
······················· LOW 6
LOOP
I hope you get what im trying to do, I could really use the help here.
Comments
You need to re-read the chapter on the DO / LOOP statement since you've got a DO, then a LOOP, then a bunch of stuff, then another LOOP. That doesn't make any sense and, without more information about what you want the program to do, it's difficult to advise you.
You also need to re-read the chapter on the IF statement. Note that there are two forms of the IF statement, one is a single line and the other is multi-line with IF ... THEN and ENDIF, possibly with some ELSEIF and ELSE statements in between. If you want the single line statement, the controlled statement has to come on the same line as the IF and THEN like:
IF IN2 = 1 THEN HIGH 3
So i have 2 inputs, a cam position sensor, and a spark signal wire, these are external from BS2
The cam position sensor only triggers once to reset the count. This is right before TDC on cylinder 1.
the signal wire shoots 4 pulses 1 for each cylinders spark.
I want to intercept the signals off of that wire, and run them to 4 different outputs.
I have tried editing this so it makes more sense, but im not that great with coding. Heres my best shot at the timing so far, but its giving me an error when i run the syntax and i dont know what it means. Expected a binary operator or '('
' {$STAMP BS2}
' {$PBASIC 2.5}
Counter VAR Byte
DO
DO
LOOP UNTIL (IN1 = 1)
FOR Counter = 1 TO 4
NEXT
IF (IN1 = 1) THEN Counter = 1
IF Counter = 1 AND (IN2 = 1) THEN HIGH 3 counter ' having a problem with it hits counter (the last word)
IF Counter = 2 AND (IN2 = 1) THEN HIGH 4 counter
IF Counter = 3 AND (IN2 = 1) THEN HIGH 5 counter
IF Counter = 4 AND (IN2 = 1) THEN HIGH 6 counter
LOOP
Again, thanks for the help, im dumb when it comes to this stuff, I will learn over time. If you could help me out with finding a better way to control my project, or better easier coding rather than if then statements i would appreciate it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
I was looking at pulsout, pulsin, and maybe I could go that route.
Post Edited (Motorheads United) : 6/4/2010 3:45:23 AM GMT
If you have 4000 rpm and 4 cylinders you have 16,000 pulses per second. The BS2 runs about 2000 lines of code per second and does not have an external interrupt input.
That said here is how i would go about solving the problem at hand.
First write some psudo code to get the logic then fill in the logic with code.
Psudo code:
start
wait for reset count signal
zero counter
mainloop:
wait for pulse to go high
increment counter
route signal
goto mainloop
Since you have just 4 conditions I would use the ON Goto statement since you are also in need of the fastest code as well assuming 4000 rpm.
Since I don't know the timing, how long the reset counter pulse will be for example you can send the fourth pulse routing signals code to go back to start and wait for the counter 0 signal.
I would start at 0 and go to 3 instead of from 1 to 4 since 0 is a perfectly good number.
So code could be:
basically you get a high signal, rout it but don't forget you are running 2000 lines of code per second so you have to wait for the signal to turn off (the waitlow loop) you may also have to de bounce signals
so you don't end up firing cylinders with erratic data for example.
So by using on goto for the return points you can automatically reset the timer based on the cycle counter signal. I am also allowing the loops to fall through to code that executes when the if then is true instead of the other way around, if x = y then do something, using if x=y do nothing until it is equal then falls through to code that does something.
Now i leave it up to you to reset those pins 3,4,5 and 6 as i don't know how long you want them high.
Setting all those pins low just after counter=counter+1 would allow those pins to echo the length of the input pulse minus the time the code takes to execute. Actually the turn on time is delayed as well as the turn off time so it works out pretty good. To keep one pin on sll the time you could reset all pins at each point, s1,s2,s3,s4 for example.
Another processor like an AVR or PIC programmed with C or assembler could use a hardware timer interrupt to time the signals by turning the bit on setting a timer and the timer interrupt turns it off automatically in microsecond accuracy.
have fun and don't get frustrated too much, sleeping on it is sometimes the only way to allow your brain to absorb the information you read, give it time to sink in. it will, it takes time though.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Post Edited (metron9) : 6/4/2010 5:27:26 AM GMT
Anyway, Put the reset pins to 0 at the line just before the on counter goto in the waitloop. That way all branches get reset. I was in error in the previous post.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Brad.
My name is GUTI, I am doing a basic code to program a pic 10f220.
The idea is to use a ir emitter and receiver from an old ball mouse, so I can count objects that break the ir barrier.
But I am having dificulties, since I am just begining with pics and the basic language.
I want the code to be able to count 3 events and then make low a pin for 1s.
At the moment I am setting things like
count var byte
for count 1to3
gp0 high
next
gpo low
pause 1000
go to loop
but it i dont know how to run it, i think is because the pins are not correctly set up in the memory banks.
I dont know....
Any help?..much appreciated.
Thanks a lot.
Best regards.
GUTI.
1) In general with forums it's not a good idea to add a question to an existing conversation that's only marginally related to the existing conversation. Start your own thread. You're more likely to get answers because people will notice.
2) This is a Parallax forum specifically for support of Parallax's Basic Stamps. We really can't help you with programming a PIC10F220. For one, it's the competition. More importantly, you're having problems with things that are specific to the PIC10F220 and the PICs in general and we (myself and most of the people who frequent these forums) just don't have the documentation and the specific development software needed. Microchip has support forums for their products, please try them for help.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Before you criticize someone, walk a mile in his shoes. That way if he gets angry, he'll be a mile away and barefoot. - unknown
Regards.