Propeller and IR object detection in Spin
Martin_H
Posts: 4,051
I'm using a Propeller Protoboard and want to try IR object detection. So I wired up two IR LED's with 1k ohm resistors to pins 3 and 4 and a 38 kHz IR detector on pin 2. I verified that the IR detector works and could decode signals from my TV remote. I also verified that if I bring pins 3 and 4 high I can see the IR LED's turn on in my camera.
So now to write some spin code. I noticed that the IR detect code on learn.parallax.com uses the PropBoe's onboard DAC, so I cant use that. So I grabbed the BS2_Functions and ported the PBasic code from "Robotics with Boe Bot" to Spin. But it doesn't work.
I heard the high pulse tone and see the output written to the terminal window, but it is always 5 which means the IR detector isn't receiving any IR no matter how I position my hand. I added additional logging to verify that the frequency select code works, but the duty cycle of the IR LED is too fast for me to see it in the camera. So I'm not sure what's wrong. I'm wondering if there's some limit on the frequencies the BS2_Functions can generate and if I'm outside that range.
Here's the code in case someone can spot something obvious.
So now to write some spin code. I noticed that the IR detect code on learn.parallax.com uses the PropBoe's onboard DAC, so I cant use that. So I grabbed the BS2_Functions and ported the PBasic code from "Robotics with Boe Bot" to Spin. But it doesn't work.
I heard the high pulse tone and see the output written to the terminal window, but it is always 5 which means the IR detector isn't receiving any IR no matter how I position my hand. I added additional logging to verify that the frequency select code works, but the duty cycle of the IR LED is too fast for me to see it in the camera. So I'm not sure what's wrong. I'm wondering if there's some limit on the frequencies the BS2_Functions can generate and if I'm outside that range.
Here's the code in case someone can spot something obvious.
{{ Robotics with the Boe-Bot - DisplayBothDistances.bs2 Test IR detector distance responses of both IR LED/detector pairs to frequency sweep. }} CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 SPEAKER = 15 IR_LEFT = 4 IR_RIGHT = 3 SENSOR = 2 OBJ BS2 : "BS2_Functions" ' Create BS2 Object VAR long frequencies[5] byte irDetectLeft byte irDetectRight byte distanceLeft byte distanceRight PUB Initialize {{ -----[ Initialization ]----------------------------------------------------- }} BS2.start (31,30) ' Initialize BS2 Object timing, Rx and Tx pins for DEBUG BS2.FREQOUT(SPEAKER, 1000, 2000) ' Battery Tester BS2.Debug_CLS BS2.Debug_Str(string("IR OBJECT ZONE",13)) BS2.Debug_Str(string("Left Right",13)) BS2.Debug_Str(string("----- -----",13)) frequencies[0] := 37500 frequencies[1] := 38250 frequencies[2] := 39500 frequencies[3] := 40500 frequencies[4] := 41500 Main Pub Main {{ Main loop }} repeat Get_Distances Display_Distances Pri Get_Distances | freqSelect, irFrequency {{ Use an IR sweep to estimate distances. }} distanceLeft := 0 distanceRight := 0 repeat freqSelect from 0 TO 4 irFrequency := frequencies[freqSelect] BS2.FREQOUT(IR_LEFT, 1, irFrequency) irDetectLeft := BS2.IN(SENSOR) distanceLeft := distanceLeft + irDetectLeft BS2.FREQOUT(IR_RIGHT, 1, irFrequency) irDetectRight := BS2.IN(SENSOR) distanceRight := distanceRight + irDetectRight BS2.PAUSE(50) Pri Display_Distances {{ }} BS2.Debug_CRSRXY(2,3) BS2.Debug_DEC(distanceLeft) BS2.Debug_CRSRXY(9,3) BS2.Debug_DEC(distanceRight)
Comments
Here's the trick: the output pin controls the CATHODE side of the circuit. You can then gate the IR LED (without touching the counter) by writing a "1" (modulation off) or "0" to the pin.
I know the BS2 functions are not optimal, but I use them when porting working code from the basic stamp. Usually they make it quick to get something working and then I fine tune by moving to a more native programming model.
There is really nothing wrong with using the BasicStamp code in the Propeller to get your hardware setup confirmed and working before you switch to something else.
In fact, it is a smart thing to do if you are learning a new language. Going back and forth with wondering if the hardware is wrong or the code is wrong is a huge waste of time.
The Basic Stamp code is there for a reason -- to help users migrate over from the BasicStamp. After all, why use a $50 BasicStamp if you can get a Propeller in some setups for less. And of course, you eventually can do more, and learn more.
IR remote control is a huge favorite of mine.
Thanks a bunch.
I don't want anyone to think I have anything against the BASIC Stamp; my company (EFX-TEK) uses the BS1 and the BS2 (in our Prop-1 and Prop-2 controllers). But for every design I've done with the Propeller I've coded it in Spin and Assembly; even though I once had a business card when I worked for Parallax that labelled me "King of BASIC Stamps." Don't handcuff yourself; do it right the first time because we rarely do it over.