Ultrasonic Sensor and 7-seg display
wvp32
Posts: 22
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
' 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
Comments
-Phil
Owio CON 6
PAUSE 10 'Wait for AVR to come out of reset.
OWOUT Owio, 0, [noparse][[/noparse]DEC3 cmDistance] 'Display cm value.
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.
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.
·································································································································· PULSIN 15, 1, time
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
Also you said A2 is connected to pin 11. So If I load coprocessor A with GPIO3.hex do I say PULSOUT 11, 5
Yes. 'Same for PULSIN.
-Phil
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.·
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
-Phil
' 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
-Phil
' 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
Try running this program, and tell me what you see on the 7Seg display:
-Phil
Post Edited (Phil Pilgrim (PhiPi)) : 3/28/2009 5:07:29 AM GMT
' {$STAMP BS2pe}
' {$PBASIC 2.5}
Owio CON 6
PAUSE 10
OWOUT Owio, 0, [noparse][[/noparse]"AB.CD"]
Also, what is Vdd set to on your Mobo? 5V or 3.3V? If it's 3.3V, retry the above on 5V.
-Phil
' {$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.
' {$STAMP BS2pe}
' {$PBASIC 2.5}
Owio CON 6
PAUSE 10
DO
· OWOUT Owio, 0, [noparse][[/noparse]HOME, "09"]
LOOP
-Phil