Shop OBEX P1 Docs P2 Docs Learn Events
SX/B Elementry coding — Parallax Forums

SX/B Elementry coding

RsadeikaRsadeika Posts: 3,837
edited 2005-02-18 21:49 in General Discussion
The following is a modified freqout.sxb program.
When I run this program with the sx key, the LED turns on for two secs and then turns off. What is supposed to happen is when it detects an object, then the LED is supposed to turn on.

Now, when I remove the sx key, and run the program, it works the way it is supposed to. Is their some major concept that I am missing here?

'
' Device Settings
'
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ··········· 50_000_000

'
' IO Pins
'
IrLED··VAR·RB.6···' output to IR LED
Detect··VAR·RB.7···' input from IR detector
LED1··VAR·RC.7···· 'LED (green)

'
' Constants
'
IrMod··CON·38000···' modulation freq = 38 kHz
Yes··CON·0·' for active-low output; Yes, detected
No··CON·1·' No, not detected

' =========================================================================
· PROGRAM Start
' =========================================================================

'
' Program Code
'
Start:
· freqout IrLED, IrMod, 1
· if Detect = No then Start
· low rc.7·'If yes, then turn on the LED
· pause 2000
· high rc.7·'Turn off the LED
end

·
«1

Comments

  • BeanBean Posts: 8,129
    edited 2005-02-11 19:09
    You need to put a "goto Start" before the "end".

    Also realize that when you are checking "Detect" the freqout has stopped, so unless the detector stays on for a little while after the IR stops you may not detect anything. (But I think it will stay on long enough.)

    I would also put a "INPUT Detect" to be safe.

    You should also change the osc setting to OSCHS2 for 50MHz.

    '
    ' Device Settings
    '
    DEVICE········· SX28, OSCHS2, TURBO, STACKX, OPTIONX
    FREQ··········· 50_000_000

    '
    ' IO Pins
    '
    IrLED··VAR·RB.6···' output to IR LED
    Detect··VAR·RB.7···' input from IR detector
    LED1··VAR·RC.7···· 'LED (green)

    '
    ' Constants
    '
    IrMod··CON·38000···' modulation freq = 38 kHz
    Yes··CON·0·' for active-low output; Yes, detected
    No··CON·1·' No, not detected

    ' =========================================================================
    · PROGRAM Start
    ' =========================================================================

    '
    ' Program Code
    '
    Start:
    · input Detect

    Main:
    · freqout IrLED, IrMod, 1
    · if Detect = No then·Main
    · low rc.7·'If yes, then turn on the LED
    · pause 2000
    · high rc.7·'Turn off the LED
    · goto Main
    end


    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.TerryHittConsulting.com


    Post Edited (Bean) : 2/11/2005 7:14:46 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-02-11 20:25
    Yes, it is a key concept of the way the BS2 (and others) use the IR-Detector as a collision detector that it DOES stay on a little while after the 'ping' signal has gone away. This does work, and quite well, too.

    One of these days, I'm going to measure how long it does stay on.
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-11 20:48
    Bean, I tried your suggestion, but the same problem occurs. Works fine in stand alone, does not work when the sx key is installed. I guess I could work around the problem by not running the program with the sx key installed.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-02-11 21:00
    It is conceivable the SK-Key is introducing some delay into the processing, such that the reading of the Pin value happens too late after the end of the FREQOUT.

    I would suggest the following:

    FREQOUT IRLed, IRMod, 1
    DetectVal = Detect
    IF DetectVal = 1 THEN GOTO Start
    ...
    This would 'unwrap' the reading of the pin from the 'IF' statement.
    I'm not sure this would solve your problem -- you still need to read the IR-Detector pin within a very short time (200 uSec?) after the pulse ends -- and the SX-Key environment may or may not support that.

    You might also want to put a 'PAUSE 1' before the FREQOUT.· I'd put this in to allow the IR-Detector to 'settle' a little.· The SX runs really fast -- I don't know what the 'ping' rate is of the tight loop you've built, but it sounds like under the SX-Key the pin is always low, ie the Detector is always active.· That can't be right.


    Post Edited (allanlane5) : 2/11/2005 9:05:37 PM GMT
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-11 21:36
    allanlane5, I guess you answered my question. Certain programs react differently when run with the sx key or run in a stand-alone.
    I appreciate your suggestion, but the project I am working on will be run in stand-alone config. So, I do not want to add code as a work around to enjoy the convience of a run with the sx key installed. Eventually I expect the code to grow, so I want to keep the code as tight as possible while it is still in the early stages.
  • BeanBean Posts: 8,129
    edited 2005-02-11 21:59
    Looking at your posted program, I can't see any reason why it wouldn't run with the sx-key ?
    How exactly are you trying to run it with the sx-key ?
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.TerryHittConsulting.com
    ·
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-11 22:17
    Bean, I hit the RUN-RUN command, it does the programming thing, and then the LED immediately goes on. What I am expecting is, that the LED does not go on until there is an object in front of the detector. Like I said earlier, the thing works as expected when the sx key is not installed.

    While I am on this subject, would you have any coding suggestions as to how to make the program work using an interrupt concept. The example in the SX/B examples dir leaves a lot to be desired.
  • Jim McCorisonJim McCorison Posts: 359
    edited 2005-02-11 22:30
    Caution: Thoughts from an SX newbie ahead...

    Timing loops in SX/B are frequency dependent. Rsadeika, you have a seperate thread going on about this. Are you sure that you've got the various run frequencies straightened out between with the Key and without?

    Jim
  • James NewtonJames Newton Posts: 329
    edited 2005-02-11 22:49
    Rsadeika said...
    Bean, I hit the RUN-RUN command, it does the programming thing, and then the LED immediately goes on. What I am expecting is, that the LED does not go on until there is an object in front of the detector. Like I said earlier, the thing works as expected when the sx key is not installed.
    I'll bet $10 that the IR detector is seeing a reflected signal FROM the SXKey when it is installed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-12 13:39
    I have the IRLED and the detctor facing away from the components on the tech board. As to the timing issue, the only time my LED (signal) should go on is when there is an object that has been placed in front of the detector. So, I am thinking, how is the 'timing and freq' get involved in this issue. The program content does change between the time I run the tech board with sx key and without sx key.

    As to the reflected signal from the sx key; does any body know if an sx key has the capability to send an IR signal.

    In my project I will be using the sx tech board exclusively, and the same sx28 chip will remain in place. Since I will be doing some reprogramming, it would be nice to be able to run the program while the sx key is installed, and still get the same results after the sx key is removed.
  • BeanBean Posts: 8,129
    edited 2005-02-12 16:18
    The SX-key is not generating IR, it may be putting out 38KHz that the IR detector is picking up.

    If that is not the problem, then there is no reason the program shouldn't run properly from the sx-key.

    Try the program with the freqout commented out. You may want to put a pause in it's place. See how that runs with and without the sx-key.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.TerryHittConsulting.com
    ·
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-12 18:26
    Bean, I just tried your suggestions. First I took out the freqout and ran the program, the LED did not turn on. When I placed an object in front of the detector, nothing. I also replaced the freqout with a pause 1000, again nothing, no LED. So, I guess their is something that is occuring within the sx key when the freqout command is used that affects either the IRLED, the detector, or both. I also tried putting back the freqout, and in front of that I put a pause 500. Now the LED flashes according to the pause commands. Also when I placed an object in front of the detector, it seems like the detector is not functioning at all, I was expecting a quick flashing LED.

    I tried replicating the program using an interrupt scheme, and the same problem occurs. And of course their are new problems that have crept in.
  • BeanBean Posts: 8,129
    edited 2005-02-12 23:04
    So basicly if you use the SX-Key, then the detector is always active (sees IR light) ?
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.TerryHittConsulting.com
    ·
  • James NewtonJames Newton Posts: 329
    edited 2005-02-13 02:20
    I was trying to suggest that the SXKey could be reflecting the IR beam from the LED or some other source onto the detector causeing it to trigger as soon as the program was running.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-13 13:58
    A summary so far, for my simple IrLED-Detector experiment.

    1. With sx key installed, my signal LED goes on imediately, and placing objects in front of the detector has no affect what so ever.

    2. Without the sx key installed (stand-alone), my signal LED works as expected. Placing and removing of objects, the signal LED goes on and off accordingly.

    3. With the sx key installed (stand-alone), but no comm cord attached, nothing works. No immediate turn on of signal LED, and placing objects in front of the detector has no affect.
    In this set-up I was expecting some sort of activity. I tried other simple programs, and in a stand-alone condition the experiments worked with or without sx key installed, in all variations.

    Now I am thinking, is their a subtle code variant, in the freqout command, at the assembly level, when, in combination with the sx key installed, odd things start to occur. When I did a debug, the code listing is to complicated for me to make any sense of it.

    So far I think that I have ruled out any kind of IR frquency reflection, I have double checked all of my wiring on the breadboard, and I believe that all my componets are working correctly.

    Again I plan to use the SX Tech board exclusively, so I want to make sure that every thing responds in a predictable manner.
  • SteveWSteveW Posts: 246
    edited 2005-02-13 14:37
    Some suggestions, point by point.
    1) IR remote receivers are notoriously susceptible to RFI (interference). How about removing it from the system, and tying your SX pin to either ground, or power, through a switch. This should give you a hint whether it's a hardware or a software problem.

    2) Super!

    3) With an SX Key, but no serial cable, you'll get no clock (how would it know what speed to clock at, without access to the PC?).
    When you say you have the key unplugged, what are you doing for a clock? (Is there any way you can get access to an oscilloscope? This would be trivial to solve if you did (probably)).

    Steve
  • BeanBean Posts: 8,129
    edited 2005-02-13 15:12
    I pretty sure you WILL get a clock from the SX-Key without the serial cable attached.
    If I remember correctly I have done this to go back and forth between the SX-Key and the BS2.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.TerryHittConsulting.com
    ·
  • SteveWSteveW Posts: 246
    edited 2005-02-13 15:26
    Hmm - mine certainly doesn't.
    there's something really rough, 37 MHz or so, unstable, and 100mV tall, but nothing like the proper clock signal.
    (The SX-Key will continue to generate a clock once it's been set up by the software, but from a cold power-on, mine definitely comes up silent). I don't believe that it's broken. (After all, the Key can't possibly know what frequency to run at until told. I favour silence, over just picking a frequency that will cause confusion)

    Steve
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-13 15:40
    The clock question. I have a 50MHz resonator plugged in, which stays in all the time.

    The other suggestion, I do not have the electronic background to attempt something like that. Besides, I think that the Parallax tech people should be investagating that, and saying whether a problem like that would be remotely possible, and if it is possible, then a tech note would be in order (maybe their is a tech note in existence, I have not read all of the documentation yet). But, that is just my opinion.

    I have written some code using my IrLED-Detector example where I placed the freqout in an interrupt scenario. The program works, but the timing is off for the pause statements. When I code 'pause 1000', in real time the pause is about four seconds. Any ideas as to what to look for; in the documentation it talks about interrupts, and added cycle time. I thought the beauty of SX/B is that it does a lot of the balancing act for you.
  • SteveWSteveW Posts: 246
    edited 2005-02-13 15:50
    Err, you've got the resonator in all the time? Even when the key's plugged in? Isn't that a terribly bad idea? (and explicitly banned in the documentation...)

    Steve
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-13 16:41
    Steve, my defense, if that was hazardous, Parallax would have warnings on the board or the first page of documentation would be stating that fact, in bold print no less.

    Before I start working on trying to make an IR remote control work with my experiment, I will post the components that I am using , and some approximate values for the IrLED-Detctor.

    The components are standard Parallax issue:

    350-00017 - Shrink wrapped IR LED
    350-00014 - 40 kHz IR Detector
    150-02210 - 220 resistor

    The values:
    kHz distance
    37500 - 5 inches
    38000 - 6 inches
    38500 - 6 1/4 inches
    39000 - 6 1/2 inches
    40000 - 7 inches
    41000 - 7 1/4 inches
    42000 - 7 1/2 inches
    43000 - 7 inches
    44000 - 5 1/2 inches
    45000 - 5 inches

    The method:
    I used the 'old science' method, a piece of paper as the object, and a tape measure with my eyeballs to determine the distances.

    I hope this saves some time for somebody. Again, these are approximations, plus/minus a quater of an inch.
  • SteveWSteveW Posts: 246
    edited 2005-02-13 16:54
    Well, it's not going to cause hardware damage, loss of life, or any long term damage at all, just confusion. There are an infinite number of ways to make things not work - this is just one of them.

    Does this mean that your project now works properly with the Key, or are we still hunting problems?

    (Also, note that IR detection like this really isn't a very reliable way of measuring distance, since it depends much more on the reflectance (and orientation) of teh object than it does on the distance. If you're trying to do things liek folowing walls at a set distance, you might be disappointed).
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-13 18:08
    Steve, no it does not work correctly with the sx key inserted. Since it works well enough without the sx key, I will just hve to suffer through the extra step. Maybe with the next release of SX/B the problem will dissapear.

    The only reason I posted the chart is for some neophytes like myself who ask the question, what kind of distance are we talking about. Is it 6 yards, 6 feet, 6 inches, or 6 mm. I am also aware of the reflection qualities of the object. Maybe I should use a black piece of cloth as the object and see what the differences are. Or maybe some experts out there can write some tight code that determines the reflective quality of the object and adjusts accordingly.
  • SteveWSteveW Posts: 246
    edited 2005-02-13 18:58
    I doubt it'll change with future versions of SX/B. In 'Run' mode, the Key just generates a clock - it's doing nothing at all cunning when your code's running at full speed. Therefore, I suspect that either, for some reason, you're not getting the clock speed you think (testable easily enough by writing a LED flasher program and seeing if it runs the same speed with the key and the resonator. Component cost - 5 cents or so for a LED, and 10 minutes of your time).
    If it's not a clock speed issue, then it's likely to be some subtle effect that will bite you when it's least convenient, and you might as well hunt it down now. Replacing your IR receiver with a bit of wire is hardly a huge investment in time or effort, and should let you see if it's a hardware or software problem. If you can't be bothered to chase it, fair enough.)

    As for the distance sensing, determining distance by the amount of reflected light is a doomed enterprise unless you can control the target. Measuring distance by, err, measuring distance is a much better method. Sharp's GP2D12 (and other similar) sensors are a far better bet - they work by triangulating the angle of a spot of light, rather than a slightly related second (or worse) order effect). Sure, the reflectance approach is temptingly cheap, but I've never seen it work usefully.

    Steve
  • James NewtonJames Newton Posts: 329
    edited 2005-02-13 19:56
    Rsadeika said...
    The clock question. I have a 50MHz resonator plugged in, which stays in all the time.
    BINGO!
    You can't do that! It will cause all sorts of "interesting" effects including causing the chip to run at speeds that could cause the exact problem you are seeing.
    The SXKey is a clock source. So is the resonator. Running the chip with both a resonator AND the SXKey installed is like trying giving the chip two bosses and watching it have a nervous breakdown.
    I don't know where parallax documented it or how bold they made it, but I KNOW you can't do that and expect correct operation.
    Disconnect the resonator, connect the SXKey and then try again. Better yet, have you ever been able to step through the program with the SXKey? E.g. Debug mode rather than just Run mode? This should be a pleasent supprize for you if you haven't seen it before.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-13 21:02
    As to your first point I already tested that aspect. In fact I used one of the examples out of the book that has the LED flash, in assembly and in SX/B code.

    As for your single wire suggestion it reminds me of the previous experiment.
    I took the code out of the Daubach book that pertains to the IrLED-Detector for his SX tech bot, midified the code a little bit so it would use just one IrLED-Detector combination, and ran the code. I was thrilled that when it was running, that when I placed my hand in front of the detector, my test LED would go on, and when I removed my hand, the LED would go off. Then I decided to start debugging, I put my finger over the IrLED, and moved my hand in, the test LED turned on. I blocked off both the IrLED and the detector, hand in LED on. At this point I started to remove all the pieces from the breadboard one at a time, HAND in LED on. I finally ended up with my test LED and resistor which was hooked to rc.7 and a wire stuck in rc.6, when I moved my hand toward the test LED the LED would turn on, and when I moved my hand out, the LED turned off. At this point I was thinking about getting a video input card hooked up to a TV set, and the tech board, to see if I could dial something in. Now, I am not implying that their is anything wrong with the Daubach code, the complexity is why I switched over to SX/B, after discovering the freqout command.

    If you looked at my code example you can see that it is very straight forward, with the minimum amount of code lines to do the job. So, I am getting all kinds of different outcomes, some of which defy logic while some of which, confirm equipment stability.

    I am planning to use SX/B for the quick gratification aspect, then on a parallel write code in bit-banger. I prefer bit banging, but their is a learning curve to overcome.
  • RsadeikaRsadeika Posts: 3,837
    edited 2005-02-13 21:10
    James, I tried that, removing the resonator, after the suggestion was made.
  • James NewtonJames Newton Posts: 329
    edited 2005-02-13 21:15
    And are you able to debug the program from the SXKey? E.g. Use Run - debug rather than Run - run and then press Step and watch the values on the port pins change?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • SteveWSteveW Posts: 246
    edited 2005-02-13 21:20
    >As to your first point I already tested that aspect. In fact I used one of the examples out of the book that has the LED flash, in assembly and in SX/B code.

    Does it flash at the same speed with the SX-key and the resonator?

    You seem to be trying to solve a lot of problems at the same time, and I'm not sure how much they're related.
    Since your program relies on accurate frequency control, I'd keep trying to get the SX-key version to work, since that'll get you the useful debug features. Also - have you considered sticking a LED and resistor on the output of the IR receiver, so you can actually see what's going on. At the moment, you don't seem to know whether you've got a design, implementation, software or hardware problem, and you can't trust your tools. It's time to stop, and remove as many variables as possible. (Or you kood keep on changing stuff, and hoping. It's your project, and your time [noparse]:)[/noparse]

    Steve
  • SteveWSteveW Posts: 246
    edited 2005-02-13 21:22
    Good grief, sorry about 'kood'. That goes well beyond a typo - no idea what I was thinking of...
Sign In or Register to comment.