Ping - Close a Circuit When Ping is Too Close
The following program has been written that allows me to push a button to set a distance using Ping, then an LED turns on if Ping is closer than the set distance. This is one of my breadboards and is related to my previous post "Ping - How Can I Measure Water Depth" from a few days ago.
This program has two issues I would like some help on:
1) The set point is not remembered when the battery is removed. Is there a way to remember the set point after the battery power is removed and then start back up where it left off when the battery power is applied again? If I need to leave the 9VDC battery pluged in continuously, how long will it last? What if the program only runs runs but only runs the Ping for about 3 minutes three times a week?
2) Would like to replace the LED with a technique that will close a 12 VDC circuit with a max of 1 Amp DC going through it. Can I close a circuit like that with the Home Work Board or do I need a relay. If I need a relay, could someone spec a relay that would do the job?
I hope to capture a signal from a fish finder from either the transducer or from one of its display outputs which would replace the Ping in this breadboard for a water friendly solution.
Thanks in advance for the helpful ideas everyone!
'Smart Sensors and Applications - PingLitesLEDIfPingDistLessThanPushButtonSetPoint.bs2
'Press push button to set a distance then turn on a light if the distance to ping is less than set point.
' {$STAMP BS2}
' {$PBASIC 2.5}
'Conversion constants for rooom temperature measurements:
CmConstant CON 2260
InConstant CON 890 'Conversion constant changes sound in air to inches to target in one direction
inSet VAR Word ' Define Word variables
inDistance VAR Word
time VAR Word
Set VAR Word
uS VAR Word
DO
IF (IN7 = 1) THEN ' if pushbutton is pushed high (1) Then
PULSOUT 4, 5 ' w/ Ping Signal Pin hooked to P4, triger Ping (5 us is typical Ping trigger pulse)
PULSIN 4, 1, Set ' Ping Signal Pin hooked to P4, 1 us Echo hold off time, store result in VAR "Set"
ELSE
' if pushbutton is not pushed it is low (0) Then
PULSOUT 4, 5 ' w/ Ping Signal Pin hooked to P4, triger Ping (5 us is typical Ping trigger pulse)
PULSIN 4, 1, time ' Ping Signal Pin hooked to P4, 1 us Echo hold off time, store result in VAR "time"
inSet = inConstant ** Set ' Set time is converted to inches set and put into VAR "inSet"
inDistance = inConstant ** time ' Time is converted to inches set and put into VAR "inDistance"
uS = time /2 ' Time cut in half to just show distance to target (not out and back) & stored in VAR "uS"
DEBUG HOME, DEC3 inSet, " in Set" 'Display results in debug screen
DEBUG CR, DEC3 inDistance, " in"
DEBUG CR,CR, CR, DEC3 uS, " uS"
IF inSet > inDistance THEN ' Determine if current distance is less than set distance
HIGH 13 ' if current distance is less than set distance then turn LED on
ELSE
LOW 13 ' Otherwise LED is off
ENDIF
PAUSE 100
ENDIF
LOOP
This program has two issues I would like some help on:
1) The set point is not remembered when the battery is removed. Is there a way to remember the set point after the battery power is removed and then start back up where it left off when the battery power is applied again? If I need to leave the 9VDC battery pluged in continuously, how long will it last? What if the program only runs runs but only runs the Ping for about 3 minutes three times a week?
2) Would like to replace the LED with a technique that will close a 12 VDC circuit with a max of 1 Amp DC going through it. Can I close a circuit like that with the Home Work Board or do I need a relay. If I need a relay, could someone spec a relay that would do the job?
I hope to capture a signal from a fish finder from either the transducer or from one of its display outputs which would replace the Ping in this breadboard for a water friendly solution.
Thanks in advance for the helpful ideas everyone!
'Smart Sensors and Applications - PingLitesLEDIfPingDistLessThanPushButtonSetPoint.bs2
'Press push button to set a distance then turn on a light if the distance to ping is less than set point.
' {$STAMP BS2}
' {$PBASIC 2.5}
'Conversion constants for rooom temperature measurements:
CmConstant CON 2260
InConstant CON 890 'Conversion constant changes sound in air to inches to target in one direction
inSet VAR Word ' Define Word variables
inDistance VAR Word
time VAR Word
Set VAR Word
uS VAR Word
DO
IF (IN7 = 1) THEN ' if pushbutton is pushed high (1) Then
PULSOUT 4, 5 ' w/ Ping Signal Pin hooked to P4, triger Ping (5 us is typical Ping trigger pulse)
PULSIN 4, 1, Set ' Ping Signal Pin hooked to P4, 1 us Echo hold off time, store result in VAR "Set"
ELSE
' if pushbutton is not pushed it is low (0) Then
PULSOUT 4, 5 ' w/ Ping Signal Pin hooked to P4, triger Ping (5 us is typical Ping trigger pulse)
PULSIN 4, 1, time ' Ping Signal Pin hooked to P4, 1 us Echo hold off time, store result in VAR "time"
inSet = inConstant ** Set ' Set time is converted to inches set and put into VAR "inSet"
inDistance = inConstant ** time ' Time is converted to inches set and put into VAR "inDistance"
uS = time /2 ' Time cut in half to just show distance to target (not out and back) & stored in VAR "uS"
DEBUG HOME, DEC3 inSet, " in Set" 'Display results in debug screen
DEBUG CR, DEC3 inDistance, " in"
DEBUG CR,CR, CR, DEC3 uS, " uS"
IF inSet > inDistance THEN ' Determine if current distance is less than set distance
HIGH 13 ' if current distance is less than set distance then turn LED on
ELSE
LOW 13 ' Otherwise LED is off
ENDIF
PAUSE 100
ENDIF
LOOP
Comments
2) You will need some kind of driver transistor to drive a relay that switches the 12V circuit. Look at Nuts and Volts Column #6 for an explanation and sample circuits. Click on the Resources tab on the main Parallax webpage and you'll see a button for the Nuts and Volts Columns index.
Another piece of this puzzle is that I hacked a Key FOB that runs a motor. I want to use the output of this project to turn power on to the Key FOB with what ever replaces the LED in this write up. The Key FOB uses a small 12 VDC battery (size A27) about 3/4" long and about 1/4" in diameter and draws about 6.5 to 7.0 mA (measured with my hand held multimeter) - I think I said 1 amp before. Do I even need a relay to do this job or will the 2N3904 transitor supplied with my Board of Education be all I need for the job of turning power on to the Key FOB?
Since this initial post I have looked at:
http://forums.parallax.com/showthread.php?129898-Controlling-Relay&highlight=relay This provides good ohms law discussion on sizing components around a relay.
http://forums.parallax.com/showthread.php?127328-High-Power-Relays&highlight=relay Would I need a high powered 2A relay like this post suggests?
http://forums.parallax.com/showthread.php?125626-Help-wiring-relay&highlight=relay This has great explainations through out this the thread on how to wire a relay, the specs for a relay, resistor values of 330 ohms and almost any kind of diode.
Since initiating this post, I have wired a transistor inbetween P13 and the LED in the program above by using the explaination on page 264(?) of "What's a Microcontroller?" thinking that the emiter from the transistor going into the LED would be one step closer to getting the relay to work if I bought the correct relay for the job (the LED lit up as expected). I hope you will say I can just get by using only a transistor.
One other question: Why do I need to see what is stored in the EEPROM before I store it? If it is the same value, it is still the same value, right?
Thanks for all of your help!
2) EEPROMs have a limit on the number of times they can be written. It's big (100,000 or more for each location), but reachable. It's better to avoid writing if the value is already there.
I opened the FOB at the negative battery end.
One end of the cut is called the negative pole of the battery
The other end of the cut is called the Battery Negative Cut Wire
Transistor connections:
Collector CONNECTS to the Battery negative cut wire
Emitter CONNECTS to Vss ground of the Homework Board and then CONNECTS with a jump from VSS to the Negative pole of the battery
Base CONNECTS to P13 of the Homework Board which is the I/O pin in this case.
I understand the transistor basically as a switch in this example that is closed when the transistor base goes high and then current flows through the transistor from the collector to the emitter. If that is correct, why can't it go directly from the emitter to the Negative pole of the battery without stopping at the Homework Board ground Vss?