PDA

View Full Version : Ping sensor and LEDs



Miles. k
11-01-2009, 03:45 AM
i recently bought a ping senor and used this test code from the ping's product page:

' {$STAMP BS2}
' {$PBASIC 2.5}

time VAR Word

DO

PULSOUT 15, 5
PULSIN 15, 1, time
DEBUG HOME, "time = ", DEC5 time
time = time ** 2251
DEBUG CR, "Distance = ", DEC4 time, " cm"
PAUSE 100

LOOP

and this worked very good but i was wondering how to make a "IF, THEN" statement so it could light up more LEDs as a object gets farther away, and light up less leds as a object gets closer.
any help would be greatly appreciated.

SRLM
11-01-2009, 04:11 AM
Take a look at What's a Microcontroller to learn more about PBASIC. Give it a shot, then post the code that you have.

Miles. k
11-01-2009, 04:55 AM
i have the book and this is what i tried, and it sort of works but when i put a object closer to it the LEDs that got turned on wont go off.

' {$STAMP BS2}
' {$PBASIC 2.5}

time VAR Word

DO

PULSOUT 15, 5
PULSIN 15, 1, time
DEBUG HOME, "time =", DEC5 time
time = time ** 2251
DEBUG CR, "distance =", DEC4 time, "cm"
PAUSE 100
IF (time = 5) THEN
HIGH 0
ENDIF
IF (time = 10) THEN
HIGH 0
HIGH 1
ENDIF
IF (time = 15) THEN
HIGH 0
HIGH 1
HIGH 2
ENDIF

LOOP

Mike Green
11-01-2009, 05:01 AM
1) You don't need the parentheses around the condition in the IF / THEN statement

2) How about using ">"? Under what condition do you want an LED to be on and when do you want it off?
It's very important to define what you want your program to do and under what circumstances. If you do
a good job of that, the program almost writes itself. It's also more likely to work the way you want.
Successful experienced programmers still do this, particularly for logically complex parts of a program.

3) Remember that, once you turn an LED on by doing a HIGH, it will stay on until you turn it off (LOW).

Miles. k
11-01-2009, 06:41 AM
thanks for the info mike, i was a little confused at first but i found the symbols ">" and "<" on page 232 in my "basic stamp syntax and reference manual" this is the code i used:

' {$STAMP BS2}
' {$PBASIC 2.5}

time VAR Word

DO

PULSOUT 15, 5
PULSIN 15, 1, time
DEBUG HOME, "time =", DEC5 time
time = time ** 2251
DEBUG CR, "distance =", DEC4 time, "cm"
PAUSE 100
IF time > 5 THEN HIGH 0
IF time < 5 THEN LOW 0
IF time > 10 THEN HIGH 0: HIGH 1
IF time < 10 THEN LOW 1
IF time > 15 THEN HIGH 0: HIGH 1: HIGH 2
IF time < 15 THEN LOW 2

LOOP

Mike Green
11-01-2009, 06:53 AM
One more thing to think about ...
If time > 10, then it's surely > 5, so you don't need the HIGH 0 in that IF statement.

Also, what if time = 5 or time = 10 or time = 15? What happens then?

Would it not be better to use IF / THEN / ELSE?