Shop OBEX P1 Docs P2 Docs Learn Events
Ultrasonic Sensor and 7-seg display — Parallax Forums

Ultrasonic Sensor and 7-seg display

wvp32wvp32 Posts: 22
edited 2009-03-29 05:05 in Accessories
I am using the following code to measure distance using the PING Ultrasonic Sensor. How do I get the distance to display on my 7-segment display. I am having trouble writing the code. Any help would be great.

' Smart Sensors and Applications - PingMeasureCmAndIn.bs2
' Measure distance with Ping))) sensor and display in both in & cm
' {$STAMP BS2pe}
' {$PBASIC 2.5}
' Conversion constants for measurements.
CmConstant CON 2260
cmDistance VAR Word
time VAR Word
DO
·PULSOUT 15, 5
·PULSIN 15, 1, time
·cmDistance = cmConstant ** time
·DEBUG HOME, DEC3 cmDistance, " cm"
·PAUSE 100
LOOP
«1

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-08 20:14
    It's impossible to tell you without information about the 7-segment display, what it is, how it's wired, how it's controlled from the Stamp, etc.
  • wvp32wvp32 Posts: 22
    edited 2009-03-08 20:42
    I am using the Parallax BS2pe MoBo and the Parallax 7Seg-DB Daughterboard (Master Unit). The 7Seg-DB is plugged into the MoBo daughterboard socket B.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-08 21:06
    Did you download and read all the info on the 7Seg-DB product page? There are also some sample programs there to try. To use them you will have to load the 7Seg driver program (7SEG1.HEX) from that page into the AVR for socket B, using the LoadAVR program, available from the MoBoStamp-pe product page.

    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-08 21:28
    Once you do what Phil mentioned, you should be able to use the 7Seg-DB much the same as your DEBUG statement as shown in the examples.
  • wvp32wvp32 Posts: 22
    edited 2009-03-08 22:11
    Yeah I read all of that. I just didn't know if I could just load 7SEG1.HEX and then just write code to straight up display the value cmDistance which the sensor program already calculates. I thought the following code might achieve this.


    Owio CON 6

    PAUSE 10 'Wait for AVR to come out of reset.

    OWOUT Owio, 0, [noparse][[/noparse]DEC3 cmDistance] 'Display cm value.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-08 22:16
    I don't have one of these so I just glanced through the documentation. What you wrote should work. You may have to add some control characters to position the value at the right spot on the display. The documentation goes into plenty of detail on making the display look the way you want it to look.

    Since you have the unit, why not just try it. That's generally a good rule ... Look through the documentation to see if there are any warnings about things you have to do or things you can't do without getting into trouble, then just try it. In the case of this display, it's mostly a matter of setting up the AVR co-processor and waiting long enough for it to start properly.
  • wvp32wvp32 Posts: 22
    edited 2009-03-10 01:09
    I have adjusted the code by adding the Owio constant and Owio display command.
    I also modified the Pulse commands from pin 15 to pin A2, as the sensor is connected to that pin (A2).

    ' Smart Sensors and Applications - PingMeasureCmAndIn.bs2
    ' Measure distance with Ping))) sensor and display in both in & cm
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' Conversion constants for measurements.
    Owio CON 6
    CmConstant CON 2260
    cmDistance VAR Word
    time VAR Word
    DO
    PULSOUT A2, 5
    PULSIN A2, 1, time
    cmDistance = cmConstant ** time
    DEBUG HOME, DEC3 cmDistance, " cm"
    PAUSE 100
    LOOP

    PAUSE 10 'Wait for AVR to come out of reset.
    OWOUT Owio, 0, [noparse][[/noparse]DEC3 cmDistance] 'Display cm value.
  • wvp32wvp32 Posts: 22
    edited 2009-03-10 01:10
    I am currently testing the above code. I will post my results later.
  • wvp32wvp32 Posts: 22
    edited 2009-03-19 21:41
    I have the PING sensor connected to pin A2 of the parallax PWR I/O DB via an LCD extension cable. The PWR I/O DB is plugged into DB port A on the MoBo BS2pe. When I run the revised edition of my program it tells me A2 is undefined. How do I define A2 to be that pin, so my pulse commands are off pin A2 of the PWR I/O DB. Why in the first program don't you have to define 15 when you say PULSOUT 15, 5
    ·································································································································· PULSIN 15, 1, time
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-19 22:08
    The names A0-A3 refer to coprocessor pins, not Stamp pins. But if you look in the MoBoStamp-pe documentation (page 5), you will see that A2 is also connected to pin 11 of the BASIC Stamp. (If the PWR-I/O-DB had been plugged into socket "B", you'd use B2, which is Stamp pin 7.)

    When you use pins that are shared between the Stamp and one of its coprocessors, you have to make sure that the coprocessor will not interfere with the Stamp. The simplest way to do this is to reload GPIO3.hex into the affected coprocessor, which defaults its pins to inputs.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-19 22:38
    Can I load the A coprocessor with GPIO3.hex and B with 7SEG1.hex since the PWR I/O DB is plugged into socket A and the 7seg DB is plugged into socket B. I want the distance to display on the 7-seg display. Or can I only load a file in 1 coprocessor. Do I load both, or just 1. If so what do I do because I need to use the PWR I/O DB plugged into socket A and the 7seg DB plugged into socket B.

    Also you said A2 is connected to pin 11. So If I load coprocessor A with GPIO3.hex do I say PULSOUT 11, 5
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-20 00:07
    wvp32 said...
    Can I load the A coprocessor with GPIO3.hex and B with 7SEG1.hex since the PWR I/O DB is plugged into socket A and the 7seg DB is plugged into socket B.
    Yes, absolutely! The two coprocessors are completely independent.
    wvp32 said...
    So If I load coprocessor A with GPIO3.hex do I say PULSOUT 11, 5
    Yes. 'Same for PULSIN.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-26 00:19
    Owio CON 6
    cmDistance VAR Word
    time VAR Word
    DO
    · PULSOUT 11, 5
    · PULSIN 11, 1, time
    · cmDistance = 2260 ** time
    · DEBUG HOME, DEC3 cmDistance, " cm"
    · PAUSE 100
    · OWOUT Owio, 0, [noparse][[/noparse]DEC cmDistance] 'Display cm value.
    LOOP

    Here is my code so far. I loaded the both coprocessors with the correct hex files, A with GPIO3.hex and B with 7SEG1.hex. The·sensor works great and displays centimeters in the debug window. However, the cm value does not display correctly on the 7-segment display. It always shows the last digit value.·i.e. If the value is 02cm the display shows 2222, for the value 04cm·the display shows 4444. How do I make the correct number show i.e. for 04cm show 04 on the display, for 128cm show 128 on the display.

    Also, the cm value for the sensor is constantly updated. Howdo I get the 7-segment display to behave this way, i.e. if the sensor moves that number would immediately show on the 7-segment display without me having to run the program all over again.·
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-26 00:44
    The reason you're seeing this is that each new reading is getting appended to the end of the display, after the one before it. You have to tell the display to restart at the beginning of the line, using the HOME (= $01) command. Do this instead:

    OWOUT Owio, 0, [noparse][[/noparse]HOME, 'z', 3, DEC4 cmDistance] 'Display cm value.
    
    
    


    This accomplishes a couple things: 1) it resets the cursor to the beginning of the display, and 2) it tells the display to supress up to three leading zeroes of the following number. This will display your distance, right-justified in a field four digits wide.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-27 15:09
    Ok, Now the 7-seg display constantly updates the second I move the sensor. However, the value is still incorrect. If the debug values are 007, 008, and 009 the 7-seg display still shows 7777, 8888, and 9999. When the debug window has 007, 008, 009, 010 I want the 7-seg display to show 7, 8, 9, 10. How do I do this. I've tried everything I can possibly think of.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-27 17:19
    Can you list your entire program here, please? Be sure to enclose it in a &#091code] &#091/code] block.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-27 17:45
    ' Smart Sensors and Applications - PingMeasureCmAndIn.bs2
    ' Measure distance with Ping))) sensor and display in cm
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' Conversion constants for measurements.

    Owio CON 6
    cmDistance VAR Word
    time VAR Word

    DO
    PULSOUT 11, 5
    PULSIN 11, 1, time
    cmDistance = 2260 ** time
    DEBUG HOME, DEC3 cmDistance, "cm"
    PAUSE 10
    OWOUT Owio, 0, [noparse][[/noparse]HOME, "z", 3, DEC4 cmDistance] 'Display cm value.
    LOOP
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-27 18:39
    Are you sure the program you've listed is the one loaded into your system? When I test it, I get the correct results: a distance right-justified in the four-digit display, not the 4444, etc., that you're seeing.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-27 20:42
    Is it possible that my previous program is somehow still stored in the hardware? Should I delete everything I downloaded from parallax and take apart the circuit? The redownload files and rebuild the circuit. Or do I need to somehow clear my program?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-27 20:53
    I'm almost certain that your old program is still loaded in the MoBo. All you should need to do is upload the program you displayed here to the MoBo, and it should work fine.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-28 02:55
    OK. Just to be on the safe side I deleted all the files I have downloaded from parallax, and the codes I have written. I then dismantled the circuit. I downloaded the AVR loader, GPIO3.hex, 7SEG1.hex, and the device FTDI driver. I loaded the both coprocessors with the correct hex files, A with GPIO3.hex and B with 7SEG1.hex. I took the following program below copied it from this forum and pasted it into the Basic Stamp Editor and on the menu in the Basic Stamp Editor and selected run (assuming that is what you meant when you said loading the program into my system). I still get 4444 etc. When you speak of loading programs you just mean putting them in the Basic Stamp Editor and clicking run, correct?

    ' Smart Sensors and Applications - PingMeasureCmAndIn.bs2
    ' Measure distance with Ping))) sensor and display in cm
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' Conversion constants for measurements.

    Owio CON 6
    cmDistance VAR Word
    time VAR Word

    DO
    PULSOUT 11, 5
    PULSIN 11, 1, time
    cmDistance = 2260 ** time
    DEBUG HOME, DEC3 cmDistance, "cm"
    PAUSE 10
    OWOUT Owio, 0, [noparse][[/noparse]HOME, "z", 3, DEC4 cmDistance] 'Display cm value.
    LOOP
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-28 05:02
    wvp32 said...
    When you speak of loading programs you just mean putting them in the Basic Stamp Editor and clicking run, correct?
    Yes.

    Try running this program, and tell me what you see on the 7Seg display:

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    Owio CON 6
    PAUSE 10
    
    DO
      OWOUT Owio, 0, [noparse][[/noparse]HOME, "09"]
    LOOP
    
    
    



    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 3/28/2009 5:07:29 AM GMT
  • wvp32wvp32 Posts: 22
    edited 2009-03-28 14:41
    I had the same hex files loaded in the coprocessors as before. I copied your program and pasted it into the Editor. I then clicked run. The 7Seg dislay shows 8888.
  • wvp32wvp32 Posts: 22
    edited 2009-03-28 15:41
    I even tried running this and got 8.8.8.8.

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}

    Owio CON 6

    PAUSE 10
    OWOUT Owio, 0, [noparse][[/noparse]"AB.CD"]
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-28 15:44
    Okay, before I conclude that the 7Seg board is defective or fried, please try one more thing: load the 7seg hex file into coprocessor A, and try the above program with Owio set to 10 instead of 6.

    Also, what is Vdd set to on your Mobo? 5V or 3.3V? If it's 3.3V, retry the above on 5V.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-28 16:09
    I have also tried.

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}

    Owio CON 6

    PAUSE 10
    OWOUT Owio, 0, [noparse][[/noparse]"00"]

    Each time I changed the number in quotes these are the results:
    "00" - 0000
    "01" - 0000
    "02" - 8888
    "03" - 0000
    "04" - 0000
    "05" - 8888
    "06" - 0000
    "07" - 0000
    "08" - 8888
    "09" - 8888
    "10" - 0000
    "11" - 1111
    "12" - 6666 ( but they're all backwards)
    "13" - 3333
    "14" - 4444
    "15" - 9999
    "16" - 8888
    "17" - 7777
    "18" - 8888
    "19" - 9999
    "20" - 8888
    "21" - 6666 ( but they're all backwards)
    "22" - 2222
    "23" - 6666 ( but they're all backwards)
    "24" - 8888
    "25" - 8888
    "26" - 8888

    If I switch the number in first and second digits I get the same display. i.e. 17 and 71 are both 7777, and 23 and 32 are both 6666 ( but they're all backwards), etc.
    Didn't know if any of this would be helpful or not.
  • wvp32wvp32 Posts: 22
    edited 2009-03-28 16:16
    Phil Pilgrim (PhiPi) said...
    Okay, before I conclude that the 7Seg board is defective or fried, please try one more thing: load the 7seg hex file into coprocessor A, and try the above program with Owio set to 10 instead of 6.

    Also, what is Vdd set to on your Mobo? 5V or 3.3V? If it's 3.3V, retry the above on 5V.

    -Phil
    Did you mean do the following program and change Owio CON 6 to Owio CON 10

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    Owio CON 6
    PAUSE 10
    DO
    · OWOUT Owio, 0, [noparse][[/noparse]HOME, "09"]
    LOOP
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-28 16:20
    Yes, it confirms my suspicion that there's a problem associated with the "digits" shift register. I'm pretty sure it's a hardware issue on the 7Seg-DB. If you could try this on socket "A", it will help eliminate coprocessor "B" as the culprit. Then you'll know what to tell Parallax when you contact them for a replacement.

    -Phil
  • wvp32wvp32 Posts: 22
    edited 2009-03-28 16:23
    If so, when I do that the 7Seg is blank. Also the Vdd select jumper is set to 5V.
  • wvp32wvp32 Posts: 22
    edited 2009-03-28 16:25
    When I did this I had 7seg.hex in both A and B.
Sign In or Register to comment.