PDA

View Full Version : How Is This Possible????



Navic
01-26-2010, 12:51 AM
I have a BS2px hooked up to 3 sets of IR TX/RX's that work just fine. I'm transmitting data via a serial connection to another device, and after that data is sent the BS2px waits for a received byte. Everything seems to work just fine until two of my four conditionals repeat instantly every time for no apparent reason. Here is my code:




IRL VAR Byte
IRR VAR Byte
IRF VAR Byte
ard VAR Byte

DO
GOSUB IR_Left
GOSUB IR_Right
GOSUB IR_Front
IF IRL = 0 THEN
DEBUG "Left", CR
SEROUT 0, 396, ["2"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Left", CR
ELSEIF IRR = 0 THEN
DEBUG "Right", CR
SEROUT 0, 396, ["3"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Right", CR
ELSEIF IRF = 0 THEN
DEBUG "Front", CR
SEROUT 0, 396, ["4"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Front", CR
ELSEIF (IRL = 0) AND (IRR = 0) THEN
DEBUG "Both", CR
SEROUT 0, 396, ["1"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Both", CR
ENDIF
IRF = 1
IRL = 1
IRR = 1
LOOP
END


IR_Left:
FREQOUT 7,6,6385
IRL = IN6
RETURN


IR_Right:
FREQOUT 5,6,6385
IRR = IN4
RETURN


IR_Front:
FREQOUT 15,6,6385
IRF = IN14
RETURN





Here's what I get in my debug window when this is running and the 'front' condition is met:

Front
Front
Response from Front

This happens with the 'both' condition also:

Both
Both
Response from Both

But when 'left' or 'right' conditions are met, everything works as I envisioned:

Left
Response from Left

Right
Response from Right

I've tried reordering the IF statements, setting IRR, IRL, IRF to 1 before looping (as you can see), tried adding timeout to the SERIN to jump to a label that pauses for 5 seconds, pausing randomly during the code, changing the GOSUB orders, I just can't see why 'front' and 'both' repeat readings! Every time it should wait for a response, 'left' and 'right' work fine, what is going on here??

Thanks for your help, I'm not a master programmer but I think my code is correct.... this is driving me crazy!!

Spiral_72
01-26-2010, 01:47 AM
Hello.

For what it's worth: See if this works. It's your code written in a different way. I've never been a big fan of multiple else conditions. It seems they have never worked properly for me. Also, I can't understand why it ever run "Both" in the first place!? It should have read your Left condition and dropped out of the IF-ELSE block. Wierd..... anyways, that's why I put the "Both" test first.


IRL VAR Byte
IRR VAR Byte
IRF VAR Byte
ard VAR Byte

main:
GOSUB IR_Left
GOSUB IR_Right
GOSUB IR_Front

IF (IRL = 0) AND (IRR = 0) THEN
DEBUG "Both", CR
SEROUT 0, 396, ["1"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Both", CR
goto main

IF IRL = 0 THEN
DEBUG "Left", CR
SEROUT 0, 396, ["2"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Left", CR
goto main

IF IRR = 0 THEN
DEBUG "Right", CR
SEROUT 0, 396, ["3"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Right", CR
goto main

IF IRF = 0 THEN
DEBUG "Front", CR
SEROUT 0, 396, ["4"]
PAUSE 50
SERIN 1, 396, [WAIT("@"), ard]
DEBUG "Response from Front", CR
goto main

IRF = 1
IRL = 1
IRR = 1
goto main
END


IR_Left:
FREQOUT 7,6,6385
IRL = IN6
RETURN


IR_Right:
FREQOUT 5,6,6385
IRR = IN4
RETURN


IR_Front:
FREQOUT 15,6,6385
IRF = IN14
RETURN

Navic
01-26-2010, 02:27 AM
Although this looks like my first version, I did paste it in (added the ENDIFs) and gave it a shot - same thing. I originally did use an IF...ENDIF for each condition and changed to the ELSEIFs later. I was hoping it would work due to the manual looping with GOTO rather than the DO...LOOP but unfortunately it does still repeat 'front' and 'both'

Navic
01-26-2010, 02:42 AM
Added a PAUSE 1000 to the beginning of main, before the GOSUB readings and something interesting happens: The left and right work fine but the both and front do not show the debug message reporting the receipt of the SERIN command as well as operating only once instead of twice!! The 1 second pause is completely inefficient for my project so that must go, but seeing the strangeness in slowing down the operation stumps me even more!

stamptrol
01-26-2010, 04:13 AM
Haven't had a chance to go thru the logic step by step, but I'd get rid of the PAUSE 50 after the SEROUT.

SERIN takes some execution time to set up, so the extra time may be causing you to miss the WAIT character within a particular loop.

Cheers,

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk

http://www.siskconsult.com
·

Navic
01-26-2010, 05:18 AM
Same results.

allanlane5
01-26-2010, 07:42 AM
1. What is sending data to pin 1? If that pin is "open", it will recieve 'noise', which might give it a false 'hit'.

2. Have you connected a ground to the device sending to pin 1? If not, that signal may always look like 'noise'.

Navic
01-26-2010, 07:58 AM
The pin is connected to an Arduino, all grounds are connected. It's just the 'both' and 'front' conditions that are repeating, the other two 'left' and 'right' work perfectly all the time.

Franklin
01-26-2010, 11:40 AM
Try running it without the serout and serin code.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

allanlane5
01-26-2010, 10:04 PM
Does the Ardwino 'drive' the signal all the time? Or does it let it 'float' at times?

Oh, and in your original code, 'Both' would never be reached, but you already knew that.

Navic
01-27-2010, 05:03 AM
Franklin - I commented out the serout and serin, front and both are still generated twice in a row while left and right work fine. allanlane - I don't know what driving and floating are, but it sounds like that has something to do with it because the Arduino takes longer to respond from the front and both data than the left and right... Could you please explain driving and floating? Thanks guys!!!

Franklin
01-27-2010, 05:58 AM
Could you use the POST REPLY button and the attachment manager to attach the actual program you are loading into the stamp? That way we could run it and see what we get. Leave the serin/serout commands commented out. Thanks.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

Navic
01-27-2010, 06:07 AM
Here's the attached program.

Post Edited (Navic) : 1/26/2010 11:07:06 PM GMT

Franklin
01-27-2010, 11:37 AM
·This is what I get if I remove the serial commands. Don't see repeats

Both
Serial going Out for Both
Response from Both
Front
Serial going Out for Front
Response from Front
Left
Serial going Out for Left
Response from Left
Right
Serial going Out for Right
Response from Right
Both
Serial going Out for Both
Response from Both
Front
Serial going Out for Front
Response from Front
Left
Serial going Out for Left
Response from Left

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen