Ping sensor and LEDs
Miles. k
Posts: 34
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.
' {$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.
Comments
' {$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
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).
' {$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
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?