Shop OBEX P1 Docs P2 Docs Learn Events
PBASIC help — Parallax Forums

PBASIC help

TechnikFIPTechnikFIP Posts: 9
edited 2012-02-02 16:46 in BASIC Stamp
I have to write a programm with PBASIC for my school.


The program should have 2 light barriers. They are planted in a decided distance and know how far the distance between them is. When a thing (car) breakthrough the first barrier, the barrier sent a signal to the controller. When the signal arrived at the controller, the controller should stop the time until the second light barrier is broken. Then the controller should calculate the speed of the thing (car) and show it to me on a display

i don't know much about PBASIC and so i need help.

can someone help me pls?

thank you :)
«1

Comments

  • davejamesdavejames Posts: 4,045
    edited 2012-01-13 08:10
    Hello TechnikFIP, and welcome to the forum.

    I can point you to a really good resource link:

    http://www.parallax.com/tabid/440/Default.aspx

    Here you'll find product information, programming references, and all sorts of stuff to help get you started.

    Again, welcome, and have fun!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-13 08:51
    I'd also like to welcome you to the forum.

    Besides the link James provided, I really like the Robotics with the Boe-Bot text. Chapter 6 (page 169) is about using light sensors. Hopefully that will help you learn what you need to know.

    If you get stuck, post the code you have so far and discribe what you want the code to do and what the code is doing (what the ouput of the program is etc.).

    This sounds like a fun assignment.

    Edit: I hadn't seen erco post when I posted this. What a great link.
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-13 18:46
    TechnikFIP,

    Welcome... you have taken your first step into the world of Parallax microcontrollers!

    Before we can help you, we need to know a few things.

    Which Pstamp do you have?

    Do you have a Homework Board or a Board of Education (BOE)?

    Have you downloaded the PBasic software yet?

    Do you have any programming experiance with a BASIC or any other programming Language?

    We cannot do your work for you, but we can guide you to become successful. You need to show us what you have so far for a design. Your description is fine and it appears to be a simple program. Do you know what you are using for the "light barriers"?

    Are you having a problem with PBasic or with the electronics that will connect to it?

    Once you answer these simple questions, we can help you get up to speed in PBasic; it is a very easy language to learn. We can also help you design circuits and learn to become self sufficient for future projects.

    Paul
  • KievlaninKievlanin Posts: 55
    edited 2012-01-15 11:16
    Basic Stamp 1
    7-segment test code:
    Just for fun or maybe someone find it usefull...
    I am a beginner.
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    '
    '
    '7-segment LED display test
    'connections made:
    'P7-A
    'P6-B
    'P5-F
    'P4-G
    'P3-DP
    'P2-C
    'P1-D
    'P0-E
    
    SYMBOL pinCounter = B1
    Main:
    DEBUG CLS
    DEBUG "I/O Pin", CR, CR
    
    FOR pinCounter = 0 TO 7
    DEBUG #B1, CR
      HIGH pinCounter
      PAUSE 1000
      LOW pinCounter
    NEXT
    GOTO Main 
    

    IMG_5074.jpg
    IMG_5075.jpg
    1024 x 768 - 89K
    1024 x 768 - 98K
  • KievlaninKievlanin Posts: 55
    edited 2012-01-15 12:46
    Next lesson of mine:
    This will use the same set-up as above, but will scroll thru digits.
    Again this program is designed for Stamp 1 module
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    '____________________________
    'Connections:
    '7-segment LED display test
    'connections made:
    'P7-A
    'P6-B
    'P5-F
    'P4-G
    'P3-DP
    'P2-C
    'P1-D
    'P0-E
    'This program will scroll thru 0-9 and will show "dimple"
    '_______________________________
    
    Setup:
    PINS = %00000000
    DIRS = %11111111
    Main:
    DEBUG CLS, "Digits shown", CR
    PINS = %11101111
    DEBUG "0", CR
    PAUSE 1000
    PINS = %10001100
    DEBUG "1", CR
    PAUSE 1000
    PINS = %11011011
    DEBUG "2", CR
    PAUSE 1000
    PINS = %11011110
    DEBUG "3", CR
    PAUSE 1000
    PINS = %10111100
    DEBUG "4", CR
    PAUSE 1000
    PINS = %01111110
    DEBUG "5", CR
    PAUSE 1000
    PINS = %01111111
    DEBUG "6", CR
    PAUSE 1000
    PINS = %11001100
    DEBUG "7", CR
    PAUSE 1000
    PINS = %11111111
    DEBUG "8", CR
    PAUSE 1000
    PINS = %11111110
    DEBUG "9", CR
    PAUSE 1000
    GOTO Main 
    


    IMG_5076.jpg
    IMG_5077.jpg
    IMG_5078.jpg
    1024 x 768 - 96K
    1024 x 768 - 94K
    1024 x 768 - 102K
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-15 19:12
    Kievlanin,

    Are you working with TechnikFIP on this project?

    I will look at your code and get back to you tomorrow.

    Paul
  • KievlaninKievlanin Posts: 55
    edited 2012-01-16 16:41
    Kievlanin,

    Are you working with TechnikFIP on this project?

    I will look at your code and get back to you tomorrow.

    Paul

    Paul, I am just like TechnikFIP learning... And I am going thru different simple projects. Sometime converting codes from BS2 to BS1.

    As for real project that I have is Solar Tracker:
    Mechanical part is currently being built. Waiting now for some machining parts. Maybe I will have them this week.
    Anyway, to cut short...
    I am not expecting any problem with mechanical part.
    I do not have electronics ready, and I do not have unlimited budget for electronics.
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-16 18:38
    Kievlanin,

    From what I see for your code you are doing great!

    I made an Infrared Tracker using 4 IR Phototransistor and a cross baffle. It was used to control rocket fins -a very simple design and it worked.

    Let me know if you need any help with circuits or code.

    In the mean time, try making an electronic die... push a button and random numbers display very quickly, press the button again and it slows down and
    stops on a random number. It takes 7 IO for the LED display and 1 IO for the switch.

    Keiv... as in Russia?

    Romsky is short from Romanovsky. Sir Dr. Pavel "Biff" Axelrod Romanovsky Jr. PhD

    Paul
  • TechnikFIPTechnikFIP Posts: 9
    edited 2012-01-20 04:49
    Hey davejames

    Thank you verry much. I hope that the link will help me.
  • TechnikFIPTechnikFIP Posts: 9
    edited 2012-01-20 05:22
    Hello Paul,

    thank you and all the others for the nice welcome here.

    I am trying to answer your question as best i can.
    I'm not sure, but on my board is: "Stamp 1 project Board".
    Yes I've downloaded the PBasic software a week ago.
    Last year i wrote programms with the languages c+ and with a special dialect of Basic for Cius.. I also programmed Java and Lego mindstorm.
    But i never programmed BASIC before.
    I don't want you to do my work for me, but i'm asking you tu help me. :) Up to now i haven't written anything for my program.
    First we want to use "light barriers" from lego mindstorm but now we know that they aren't compatible. Now we want to use a very simple system. We only need a laser and a LDR. when the laser beam is broken, the time should be measured.

    Up to now we haven't any problems with PBasic or the electronics.

    Thank you.

    Finn
  • KievlaninKievlanin Posts: 55
    edited 2012-01-20 05:47
    Kievlanin,

    From what I see for your code you are doing great!

    I made an Infrared Tracker using 4 IR Phototransistor and a cross baffle. It was used to control rocket fins -a very simple design and it worked.

    Let me know if you need any help with circuits or code.

    In the mean time, try making an electronic die... push a button and random numbers display very quickly, press the button again and it slows down and
    stops on a random number. It takes 7 IO for the LED display and 1 IO for the switch.

    Keiv... as in Russia?

    Romsky is short from Romanovsky. Sir Dr. Pavel "Biff" Axelrod Romanovsky Jr. PhD

    Paul

    Yes, Sir!
    I am from Kiev. However, it is used to be in USSR, not Russia...
    Ukraine, Ukraine...
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-21 07:25
    Keivlanin,

    I stand corrected... Ukraine... thank you.

    I have not been to Ukraine yet, but I hope to visit someday. The closest that I have been is to Minsk Belarus.

    How is your project coming?

    Paul
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-21 07:33
    Finn,

    Ok! you should have no problem with PBasic. The reason we say "P"Basic is that Parallax trimmed it down from traditional BASIC to fit in the BS1 and BS2 series. However, PBasic adds some features that are specific to the microcontroller that regular BASIC cannot provide.

    The most challenging (and fun) thing about PStamps is the small EEPROM/RAM space and getting your programs to not only run but to fit.

    Have you thought about a simple low cost Infrared (IR) LED and matching sensor? Digi-key has parts that are very affordable and I am sure they ship to Europe. When something breaks the IR beam the measurement starts, and when it breaks the second beam the measurment stops.

    This might be easier than a LASER. If you want, I might have an old IR LED pair that I could send you - no charge.

    Let me know,
    Paul
  • TechnikFIPTechnikFIP Posts: 9
    edited 2012-01-21 08:48
    Hello Paul,

    Today i worked at my projekt and now i have a problem. I need a commando that counts the time between the first breach and the second one, but i don't find a matching commando.
    Could you or someone else say me a counting commando, please?

    Your offer sounds very good, i'll talk with my partner about it.
    Thank you very much for your supply, i am going to think about it but first i want to finish the programm code for my project.

    Greetings Finn
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-21 11:58
    Finn,

    Sure, Let me code up something to get you started. It will be a couple of hours becuse my wife has "Honey Dos" for me today.

    Paul
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-21 13:00
    Gentlemen,

    Attached is some code that demonstrates one way to measure the time between the breach (broken beam) of the 1st and 2nd beam.

    There are many ways to do this, but there is not a single command that does what you need.

    Take a look at my code and see if it makes any sense to you.

    You may have to change it to work with your LASERs, but the concept should be clear to see. The Counter value displayed will relate to speed once you figure out a few things (see the comments in the code).

    Paul
  • TechnikFIPTechnikFIP Posts: 9
    edited 2012-01-21 14:59
    Thank you very much Paul.

    In Germany it is 24 o'clock now so i will have a look tomorrow.
    I think that everything will be clear but if i have a question i will ask you.

    thank you, thank you, thank you

    I will write tomorrow again.

    Greeting Finn
  • KievlaninKievlanin Posts: 55
    edited 2012-01-22 15:09
    Gentlemen,

    Attached is some code that demonstrates one way to measure the time between the breach (broken beam) of the 1st and 2nd beam.

    There are many ways to do this, but there is not a single command that does what you need.

    Take a look at my code and see if it makes any sense to you.

    You may have to change it to work with your LASERs, but the concept should be clear to see. The Counter value displayed will relate to speed once you figure out a few things (see the comments in the code).

    Paul


    Paul, I will try to build a test for your program. Using just two push buttons in place of PIN1 & PIN2
  • KievlaninKievlanin Posts: 55
    edited 2012-01-22 16:06
    IMG_5168.jpg
    IMG_5166.jpg
    IMG_5167.jpg

    Gentlemen,

    Attached is some code that demonstrates one way to measure the time between the breach (broken beam) of the 1st and 2nd beam.

    There are many ways to do this, but there is not a single command that does what you need.

    Take a look at my code and see if it makes any sense to you.

    You may have to change it to work with your LASERs, but the concept should be clear to see. The Counter value displayed will relate to speed once you figure out a few things (see the comments in the code).

    Paul


    Works great! I was trying to post pictures, but something is going wrong iether with my computer or website.
    Only one note:
    From your description, I can get it clear... LED negative should go to Pin3? I did this at first. It will not go on.
    When I reversed, everything went fine

    Well.... while I was typing I got to my mistake......
    Below is the new images:


    IMG_5169.jpg
    IMG_5170.jpg
    IMG_5171.jpg
    1024 x 768 - 65K
    1024 x 768 - 88K
    1024 x 768 - 93K
    1024 x 768 - 96K
    1024 x 768 - 97K
    1024 x 768 - 93K
  • KievlaninKievlanin Posts: 55
    edited 2012-01-22 16:24
    Keivlanin,

    I stand corrected... Ukraine... thank you.

    I have not been to Ukraine yet, but I hope to visit someday. The closest that I have been is to Minsk Belarus.

    How is your project coming?

    Paul

    My Solar Tracker is going. Major parts are all done. I am waiting now for parts from machine shop.
    Did not ordered stepper motors yet.
    I desided to wait until structure will be standing. And there may be some changes in what kind of motor... Bi-polar or Uni-polar.
    Electronics are well behind...!
    I am expecting by the end of February to show pictures to everyone.

    About Kiev:
    Definitely try to visit. One of the most beautiful city in the Europe! Government all screwed up, but this may change in a few years... I hope.
    Take car and drive from Kiev to Odessa.
    Odessa is another city you must see. I was there several times.
    Try to make trip in the second half of May or first half of June.
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-26 09:55
    Kievlanin,

    Thanks for the info on Kiev!

    Once you get all the parts and pieces together I am sure your project will come togther quickly. We are always here if you need any help.

    Paul
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-26 10:26
    Kievlanin wrote: »
    IMG_5168.jpg
    IMG_5166.jpg
    IMG_5167.jpg





    Works great! I was trying to post pictures, but something is going wrong iether with my computer or website.
    Only one note:
    From your description, I can get it clear... LED negative should go to Pin3? I did this at first. It will not go on.
    When I reversed, everything went fine

    Well.... while I was typing I got to my mistake......
    Below is the new images:


    IMG_5169.jpg
    IMG_5170.jpg
    IMG_5171.jpg

    Kievlanin,

    In electronics, it is traditional to name your signals that acutally mean something (like "LED1" instead of something like "XrytWe")... and you are doing this as I can see, Good!

    Because some signals (pins) are active when they go HIGH - Positive Logic (logic '1' or VCC if you will), we usually just keep the name as it is. But if the signal is an active LOW or Negative Logic (logic '0' or GND if you will), then we have a few ways to identify that in the signal name....

    We call this: "Stars and Bars"

    Example:

    LED1 ' LED #1, Read as "LED 1", Active High: 1 = On, 0 = Off

    LED3* ' LED #3, Read as "LED 3 Not", Active Low: 0 = On, 1 = Off
    ' Some compilers don't allow you to use an asterisk (Star) in the signal name, so this is not seen much anymore
    _____
    LED5 ' LED #5, Read as "LED 5 Not", Active Low: 0 = On, 1 = Off
    ' Back in the days when schematics were drawn by hand, it was very common to place a viculum (Bar) over the
    ' the signal name to indicate active low (Negative Logic).

    LED2_N ' LED #2, Read as "LED 2 Not", Active Low 0 = On, 1 = Off
    ' Placing an undersore (_) and then a "N" after the signal name is the preferred way to indicate active low signals.
    ' This is done because most compilers can handle this syntax.
    ' Some people like to use (_L) instead: LED2_L (Read as LED 2 Low). This is ok, but if you ever learn Boolean Logic,
    ' then you will understand why I prefer the _N method.

    There are other ways to indicate a NOT signal but these are the most common.

    Another method in signal naming is "Magnatude Decimal Point"

    Lets say you have a clock signal that runs at 12.452 MHz (Mega Hertz, that is 12.452 million cycles per second).

    You could call your signal: CLK

    But how about: CLK12.425MHZ

    The latter would be better, but again, a lot of compilers don't allow the use of decimal points (dot or comma) in signal names. So,
    the follwing convention was developed:

    CLK12M425 '12.425 MHz Clock: Read as "Clock 12.425 Mega Hertz" (you can add the MHZ but it is normally understood).

    I prefer to write it as: CLK12_425MHZ (makes more sense to the casual reader).

    What would you call a clock signal that runs (oscillates) at 1.25 KHz?

    Answer: _______________

    Also, it is best to keep all of you signal names in capital letters (just like constants). This is a tradition for readability in schematics.

    I have a strict rule that programmable devices' (like the PStamps) pin names match EXACTLY to the names on my schematic!

    LED1_N connects to the signal LED1_N on my schematic. This is a courtesy to your audiance, thay have enough to learn reading your code and schematics, don't make it more complicated by changing signal names.

    Paul
  • KievlaninKievlanin Posts: 55
    edited 2012-01-28 10:34
    Thanks, Paul.

    Answer: _______________ CLK1_25KHz


    My Solar Tracker assembly is on hold. Machinist did not made my parts... Hopefully by the end of next week.
    Electronics are on hold too.
    I am reading a lot about programming in Stamp. Not much success... Still hard to understand electronics in my "mechanical" brains.
    I think I should go and change oil in my Prius!
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-01-28 15:48
    Kievlanin,

    Correct! CLK1_25KHz is a 1.25KHz clock. But try to use all caps if you can: CLK1_25KHZ.

    Try not to understand the "Electronics" parts of the PBasic help files, Focus on the PBasic language itself and just learn how to get you code to compile (tokenize). Remember, comment all that you can, it does not take up space inside the PStamp device.

    I will help you learn the Electronics.

    Paul
  • TechnikFIPTechnikFIP Posts: 9
    edited 2012-01-31 01:41
    Hello dear comunity
    the last days i try your programm and it works very good. I used two simple switcher in place of PIN 1 & PIN 2.
    I understand the code completely but this : "' Pins 76543210 0 = Input, 1 = Output
    DIRS = %00001000"
    Could you explain it to me please?

    Well I'm searching after a code that calculate the speed yet. I use the "help" option but i have less hope that I'll find some helpfull article there. So I would ask you Paul to help me again. I have less time to finish the Programm and the "area" (street, cars,...) so I need help once again.

    Finn
  • average joeaverage joe Posts: 795
    edited 2012-01-31 02:33
    DIRS is the Direction Register for each pin. % is binary. each of the 16 - 0 or 1's represent if a pin is input (0) or output (1). the code make pin 3 an output and the rest inputs.
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-02-01 10:43
    average joe,

    Very succinctly put! Thanks!

    TechnikFip,

    Let me expand on this a little more....

    For Example:

    '......Pins 76543210
    DIRS = %00000100 ' 0 = Input, 1 = Output

    This is how I set the directions for the IO pins for a BS1. Rather than doing a bunch of commands (see below) the above code does it all at one time and saves some EEPROM space. Notice how I comment the code with the "Pins 76543210" text above the binary bits in the DIRS = assignment (this is to help the reader... you). The above code clearly shows that the DIRS bit for IO pin 2 is set to a '1', this makes that pin an Output. All of the other bits are '0', so they are Inputs.

    The hard way, instead of the DIRS method:

    INPUT 0
    INPUT 1
    OUTPUT 2
    INPUT 3
    INPUT 4
    INPUT 5
    INPUT 6
    INPUT 7

    In PBasic:
    % is the prefix for a number that is to be interpreted as Binary (1's and 0's) or Bits (Bit stands for Binary Digit).
    $ is the prefix for a number that is to be interpreted as Hexidecimal (0 to F) or simply Hex for short.
    a number without a prefix charcter is to be interpreted as Decimal (0 to 9) the way we humans normally think of numbers.

    In other software languages they may be different, for example: In the C programming language, 0x or 0X is the prefix for Hex.
    C: 0x12 Read as: 12 (hex)
    PBasic: $12 Read as: 12 (hex)

    Both equal 18 (decimal) Read as 18 (or 18 Decimal)

    Example
    %1101 Read as 1 1 0 1 (Binary)
    $D Read as D (Hex)
    13 Read as 13 (Decimal)
    All are the same value! They are just expressed in different "bases" of numbering schemes. You choose the best method to help you understand the value better: Binary for the computer, Decimal for the Human, or Hex sort of in between.

    See the Variables section of the PBasic help for details. Read and understand this section before going any further. In comupting you will need to master Binary, Hex, Decimal, and even Octal (base 8: 0 to 7, not used in PBasic) numbering systems. When you look at a Binary number compared to Hex and Octal, you will see why Hex is often used by programmers.

    I will create a slide for this and post it tonight.

    Paul
  • Paul RomskyPaul Romsky Posts: 66
    edited 2012-02-01 10:51
    TechnickFip.

    I need the distance between the 2 beams in mm (the distance betwwen the first beam and the seconds beam). Both beam must be parallael to each other.

    After you tell me this, I will show you how to calculate the speed.

    Paul
  • TechnikFIPTechnikFIP Posts: 9
    edited 2012-02-01 11:06
    Thank you average Joe and thank you Paul I think I got it.
    Well Paul...all I could say is, you are great and that the distance between the "barriers" should be 10 cm. :D thank you
    What are you thinking of the idea to use two external electric circuit for the "barriers"? each circuit will be outfitted with one LDR and these LDRs will be irradiated by one laser. If the LDR is "broken" it will be like a switcher that is switched off or on.

    Finn
Sign In or Register to comment.