Shop OBEX P1 Docs P2 Docs Learn Events
Need help with writing code using Pbasic — Parallax Forums

Need help with writing code using Pbasic

edited 2010-07-11 14:38 in BASIC Stamp
Im familiar with parallax stuff, I did the microcontrollers and boe bot a while back, and it was awesomeburger.gif· 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.cool.gif

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-06-03 18:30
    You're missing some basic understanding of the PBasic statements.

    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
  • edited 2010-06-04 01:27
    Im playing with spark control. Im making a module that takes a cam position sensor, and a spark signal, and trying to get the signal wire to break down into 4 separate channels that BS2 can read from 1 wire as it pulses 4 times for every revolution from the ECU. its the trigger wire for spark. So after the cam position sensor sends a signal, i want the counter to reset. when i get the first cylinder spark signal, i want the BS2 to interpret that and reroute that signal to say pin 3, the counter would add 1. The next signal that comes in off the same wire i want to go to pin 4 the counter would go to 2. etc. and so on. Until the count goes past the counter and resets.

    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.
  • edited 2010-06-04 01:46
    Heres an image of my main goals.
    1280 x 960 - 305K
    960 x 1280 - 93K
  • FranklinFranklin Posts: 4,747
    edited 2010-06-04 01:50
    You should read "what's a microcontroller" before you go much further. Your code still will not work as written. You need to be able to run your code on a test setup to make sure it does what you want.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • edited 2010-06-04 03:39
    ive read its before, twice its not helping me much. Im not picking up the information as its overload. im sure one of you could figure out the coding in 5 minutes. maybe if i did subroutines it would help with this. I dont know right now. It cant be that complicated. PM me with some code snippets that may help me. Or help me visualize how to break it down into chunks. whats a microcontroller really only covers the basics and i can barely understand that. Should I be using DO ..Loops, and If....THENS or should I be focusing more on subroutines?

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2010-06-04 04:02
    You need to read "What's a Microcontroller?" and you have to work through the activities, not just skim it. You need to pick up basic skills, then build your project piece by piece. If you want someone else to write your code for you, you're asking for a consultant that you'll have to pay for their time and effort.
  • metron9metron9 Posts: 1,100
    edited 2010-06-04 05:07
    How many pulses per second maximum will you have?

    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:

    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM1}
    
    counter VAR Nib
    
    
    start:
    IF IN1=0 THEN start
    counter=0
    mainloop:
    IF IN2=0 THEN mainloop
    counter=counter+1
    ON counter GOTO s1,s2,s3,s4
    'error if it falls through here but counter will be 0,1,2, or 3 so it won't
    
    s1:
    HIGH 3
    GOTO waitlow
    
    s2:
    HIGH 4
    GOTO waitlow
    
    s3:
    HIGH 5
    GOTO waitlow
    
    s4:
    HIGH 6
    
    waitlow:
    IF IN2=1 THEN waitlow
    
    ON counter GOTO mainloop,mainloop,mainloop,start
    
    
    



    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
  • metron9metron9 Posts: 1,100
    edited 2010-06-05 03:03
    oops, I thought of it just before I fell asleep last night. (I encourage everyone to sleep on it as they say when you have a problem, the solutions come mostly when you are doing something mundane.)

    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!
  • edited 2010-06-05 04:36
    Thanks! This is extremely helpful, I am grateful and will be able to use this in my code!

    Brad.
  • gutiguti Posts: 2
    edited 2010-07-10 09:32
    Dear All,

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-10 13:39
    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.
  • sumdawgysumdawgy Posts: 167
    edited 2010-07-10 23:40
    metron9 said...
    oops, I thought of it just before I fell asleep last night. (I encourage everyone to sleep on it as they say when you have a problem, the solutions come mostly when you are doing something mundane.)



    I feel your pain on a personal level yeah.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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
  • gutiguti Posts: 2
    edited 2010-07-11 14:38
    thanks mike i will do as you say. Sorry for the trouble.
    Regards.
Sign In or Register to comment.