PDA

View Full Version : Problem using Else



tazntex
- 24th July 2008, 12:41
I am using the latest version of Picbasic Pro with Micro Studio Plus, if I write something like:

IF Portb.0=0 THEN Portb.1=1
ELSE
Portb.1=0
ENDIF

Or
IF i=%00000001 THEN HIGH 1
ELSE
LOW 1
ENDIF

ERROR Line 49: ELSE without a matching IF..THEN. (TEST(mod1).pbp)

Just about anything I try using else doesn't work, Any suggestions? Thanks again

sayzer
- 24th July 2008, 13:00
IF i=%00000001 THEN HIGH 1
ELSE
LOW 1
ENDIF


should be like

IF i=%00000001 THEN
HIGH 1
ELSE
LOW 1
ENDIF

tazntex
- 24th July 2008, 13:22
IT WORKS!!!!!!!!!!!!!

Thank you very much

tazntex
- 24th July 2008, 13:54
can I put a pause in for debounce?
IF i=%00000001 THEN
HIGH 1
pause 50
ELSE
LOW 1
ENDIF

tenaja
- 24th July 2008, 15:03
IF i=%00000001 THEN HIGH 1
ELSE
LOW 1
ENDIF


should be like

IF i=%00000001 THEN
HIGH 1
ELSE
LOW 1
ENDIF

If you want to fit the THEN on one line, then the ELSE must be there too. To fit multiple commands on your line, you need a colon ":" between them...

IF i=%00000001 THEN HIGH 1 : ELSE : LOW 1
(And I don't remember if the Endif is required in this situation or not.)

skimask
- 24th July 2008, 16:55
If you want to fit the THEN on one line, then the ELSE must be there too. To fit multiple commands on your line, you need a colon ":" between them...

IF i=%00000001 THEN HIGH 1 : ELSE : LOW 1
(And I don't remember if the Endif is required in this situation or not.)

I'm not sure that statement is 100% valid, and I'm all about using colons! :D I'll have to try it out later today.
Also, GOSUBs don't work well after a single line IF/THEN, and neither do FOR/NEXT loops.

tazntex
- 24th July 2008, 19:18
The manual says:

W2 = W0
W0 = W1
W1 = W2
is the same as:
W2 = W0 : W0 = W1 : W1 = W2

So why wont this compile:
IF mydata=%01010110 THEN: HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
ERROR Line 48: Bad expression. (Test(test mod2).pbp)

I've even tried it as this :
IF mydata=%01010110 THEN HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop

Still no go
I just started to try to use multi-line statements as suggested, sure would look better

skimask
- 24th July 2008, 20:30
The manual says:
W2 = W0
W0 = W1
W1 = W2
is the same as:
W2 = W0 : W0 = W1 : W1 = W2

Yes, that is completely valid..



So why wont this compile:
IF mydata=%01010110 THEN: HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
ERROR Line 48: Bad expression. (Test(test mod2).pbp)

I've even tried it as this :
IF mydata=%01010110 THEN HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop

Just like I said in my last post, I'm not 100% sure that an else in the IF/THEN is valid, and apparently it's not. So you still have to split up the If/Then/Else/Endif statements.

This is valid:


If mydata = x then
high 7
else
low 7
endif
pause 50:goto loop


You're examples above are not valid.
These don't work either:


IF mydata = x then pause 50 : GOSUB something

If mydata = x then for x = 1 to 10 : pause 1 : next x

These work:


if mydata = x then
pause 50 : gosub something
endif

if mydata = x then
for x = 1 to 10 : pause 1 : next x
endif

Just a few examples of what works and what doesn't when using colons...and I know colons!

tazntex
- 24th July 2008, 20:34
Well that makes me feel better, I always try to learn something each day. Thanks again for the explanation.

sayzer
- 25th July 2008, 08:06
can I put a pause in for debounce?
IF i=%00000001 THEN
HIGH 1
pause 50
ELSE
LOW 1
ENDIF

debounce?



IF i=%00000001 THEN

WHILE i=%00000001
PAUSE 50
WEND

HIGH 1

ELSE

LOW 1

ENDIF

tazntex
- 6th August 2008, 18:03
I have tried the:
IF i=%00000001 THEN

WHILE i=%00000001
PAUSE 50
WEND

HIGH 1

ELSE

LOW 1

ENDIF

using this on four outputs my program does nothing with these four. If I take out the while statement and use the :
If i=%00000001 THEN
HIGH 1
ELSE
LOW 1
ENDIF
then my outputs for these four comes on and stays on, forever.
I been experimenting with SELECT CASE and what I am trying to accomplish is if I am pressing the button on the transmitter, the output selected comes on and stays on until I release the button instead of cycling on/off. So here is my latest rx version which does nothing, what am I doing wrong?

INCLUDE "bs2defs.bas"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
@ DEVICE pic16F628a, PWRT_ON
@ DEVICE pic16F628a, MCLR_ON
@ DEVICE pic16F628a, BOD_ON
@ DEVICE pic16F628a, LVP_OFF
@ DEVICE pic16F628a, CPD_OFF
@ DEVICE pic16F628a, PROTECT_OFF
DEFINE OSC 4
mydata1 VAR byte
mydata2 var byte
address1 var byte
address2 var byte
checksum var byte
chk_sum var byte
serpin VAR porta.1
PORTA = 0
PORTB = 0
trisa = %00000010
trisb = %10000000
CMCON=%00000111
PAUSE 50

loop:
gosub loop1
CheckSum = (address1 + address2)
CheckSum = CheckSum + (mydata1 + mydata2)
IF checksum != chk_sum THEN loop
IF (mydata1) != (mydata2) THEN loop
IF (address1) != (address2) THEN loop

select case mydata1
case 0
mydata1=%01010110
toggle 0
pause 50

case 1
mydata1=%01011001
high 1
pause 50
case else
low 1

case 2
mydata1=%01011010
high 2
pause 50
case else
low 2

case 3
mydata1=%01100101
high 3
pause 50
case else
low 3

case 4
mydata1=%01100110
high 4
pause 50
case else
low 4

case 5
mydata1=%01101001
toggle 5
pause 50

case 6
mydata1=%01101010
toggle 6
pause 50
end select
goto loop

loop1:
SERIN serpin,N9600,[254],address1,address2,mydata1,mydata2,chk_sum
Return

One more thing when I copy and paste my code to this forum everything becomes lower case. I have been reminded as a courtesy of DEFINE OSC 4 just wanted to let ya'll know
why everything appears in the lower case. Many Thanks

Normnet
- 6th August 2008, 18:43
FineLine Viewer was created to sort out the if - endif statements by drawing brackets between the if and endif's.

See IDE FineLine Viewer (http://www.picbasic.org/forum/showthread.php?t=8215).
Scroll down to and download v1.3

Be sure to have "Hide extensions for known file types"
unchecked in Any folder>Tools>Folder options>View to see
the ".TXT" to remove.


Norm

skimask
- 6th August 2008, 19:04
For one thing, you've got more than one CASE ELSE inside the SELECT CASE block.
Check your manual...

Archangel
- 6th August 2008, 19:17
Order of operations, I dunno maybe the compiler sorts this out, but . . . logic dictates, cmcon goes before anything portA, then tris registers dictate input or output, then port dictates hi or low status. Unless you are writing this for a basic stamp, why INCLUDE "bs2defs.bas" I would and always do use INCLUDE "MODEDEFS.BAS", as I haven't the foggiest as to what makes a stamp tick. Now having said this , someone will explain to me why I am all wrong :)

tazntex
- 6th August 2008, 20:25
Ok, I am seeing some light, hopefully it isn't attached to a train. Thanks for pointing out the CASE ELSE, never thought about that. I started with sample code from RENTRON's site and that why I had INCLUDE "bs2defs.bas" up there, and the CMCON=%00000111 never saw exactly where that should be placed in the manual. One thing is for certain I have learned a lot from this forum. And thanks for the FINE LINE link.

I still have a problem with:
'This will do nothing

IF mydata1=%01011001 THEN
WHILE mydata1=%01011001
pause 50 ' pause for debounce
WEND
HIGH 1
ELSE
LOW !
ENDIF

or
'This works, to turn on but will not turn off

IF mydata1=%01011001 THEN
HIGH 1
ELSE
LOW !
ENDIF

Thanks again

Archangel
- 6th August 2008, 20:37
Is there another "half" to this code, that is sending data? I ask because I want to try it out and am too lazy to write code to test it ;)

tazntex
- 6th August 2008, 20:48
Here is the entire code with the WHILE WEND example I was trying to use for a debounce.

Please ignore what should be upper case because when I copy and paste it changes back to lower case.

INCLUDE "MODEDEFS.BAS"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
@ DEVICE pic16F628a, PWRT_ON
@ DEVICE pic16F628a, MCLR_ON
@ DEVICE pic16F628a, BOD_ON
@ DEVICE pic16F628a, LVP_OFF
@ DEVICE pic16F628a, CPD_OFF
@ DEVICE pic16F628a, PROTECT_OFF
DEFINE OSC 4
CMCON=%00000111
PORTA = 0
PORTB = 0
trisa = %00000010
trisb = %10000000
mydata1 VAR byte
mydata2 var byte
address1 var byte
address2 var byte
checksum var byte
chk_sum var byte
serpin VAR porta.1
PAUSE 50

loop:
gosub loop1
CheckSum = (address1 + address2)
CheckSum = CheckSum + (mydata1 + mydata2)
IF checksum != chk_sum THEN loop
IF (mydata1) != (mydata2) THEN loop
IF (address1) != (address2) THEN loop

if mydata1=%01010110 then toggle 0
pause 50

if mydata1=%01011001 then
while mydata1=%01011001 ' trying this example for debounce
pause 50
wend
high 1
else
low 1
endif

if mydata1=%01011010 then
while mydata1=%01011010
pause 50
wend
high 2
else
low 2
endif

if mydata1=%01100101 then
while mydata1=%01100101
pause 50
wend
high 3
else
low 3
endif

if mydata1=%01100101 then
while mydata1=%01100101
pause 50
wend
high 4
else
low 4
endif

if mydata1=%01101001 then toggle 5

if mydata1=%01101010 then toggle 6

goto loop

loop1:
SERIN serpin,N9600,[254],address1,address2,mydata1,mydata2,chk_sum
Return

skimask
- 6th August 2008, 21:57
I think you're confused on the correct method of debouncing.

The object is to check for a switch hit, wait for the bouncing to stop, then check again and see if the same switch is still hit.
The way you are doing it, you'll catch the bouncing and kick out of your check before you get a chance to check it after the bouncing is over...

Try this:



INCLUDE "MODEDEFS.BAS"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
@ DEVICE pic16F628a, PWRT_ON
@ DEVICE pic16F628a, MCLR_ON
@ DEVICE pic16F628a, BOD_ON
@ DEVICE pic16F628a, LVP_OFF
@ DEVICE pic16F628a, CPD_OFF
@ DEVICE pic16F628a, PROTECT_OFF
DEFINE OSC 4
CMCON=%00000111 : PORTA = 0 : PORTB = 0 : trisa = %00000010
trisb = %10000000 : mydata1 VAR byte : mydata2 var byte : address1 var byte
address2 var byte : checksum var byte : chk_sum var byte : serpin VAR porta.1
PAUSE 50
loop: gosub loop1 : CheckSum = (address1 + address2) + (mydata1 + mydata2)
IF checksum != chk_sum THEN loop
IF (mydata1) != (mydata2) THEN loop
IF (address1) != (address2) THEN loop
if mydata1=%01010110 then toggle 0
pause 50
lp1:
if mydata1=%01011001 then
pause 50
if mydata1=%01011001
high 1
else
low 1
endif
if mydata1=%01011010 then
pause 50
if mydata1=%01011010
high 2
else
low 2
endif
if mydata1=%01100101 then
pause 50
if mydata1=%01100101
high 3
else
low 3
endif
if mydata1=%01100101 then
pause 50
if mydata1=%01100101
high 4
else
low 4
endif
if mydata1=%01101001 then toggle 5
if mydata1=%01101010 then toggle 6
goto loop
loop1:
SERIN serpin,N9600,[254],address1,address2,mydata1,mydata2,chk_sum
Return

Archangel
- 6th August 2008, 23:46
Here is the entire code with the WHILE WEND example I was trying to use for a debounce.

Please ignore what should be upper case because when I copy and paste it changes back to lower case.


That is because you typed it in lower case and MCS changes it for it's use, when you copy it to the clipboard it reveals the truth.

Archangel
- 7th August 2008, 00:38
Please let us see the code you are sending to this program. Are yoy sending from a PC or a PIC?

Melanie
- 7th August 2008, 01:31
Also, GOSUBs don't work well after a single line IF/THEN, and neither do FOR/NEXT loops.

I missed this when it was first posted (on the basis I don't read everything)... this is news to me... do you have examples?

I've written some pretty complex programs and haven't come across this problem - unless of course you're dropping COLONs all over the place and your GOSUB follows on... not something I've ever done (on one line).

Example

If A=0 then Goto Start else A=A+1:Gosub DoThis

This is equivalent to...

If A=0 then
Goto Start
else
A=A+1
Gosub DoThis
Endif

And NOT as may be expected...

If A=0 then
Goto Start
else
A=A+1
endif
Gosub DoThis

There is a distinct difference (and some tricky workings) if you bung all your code on one line separated with colons. It's not really the way it should be written. There are rules for IF/THEN/ELSE, WHILE/WEND, FOR/NEXT etc.

tazntex
- 7th August 2008, 04:33
Joe, here is the code I am sending from pic to pic:

INCLUDE "MODEDEFS.BAS"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
' Watchdog Timer
@ DEVICE pic16F628a, PWRT_ON
' Power-On Timer
@ DEVICE pic16F628a, MCLR_ON
' Master Clear Options (Internal)
@ DEVICE pic16F628a, BOD_ON
' Brown-Out Detect
@ DEVICE pic16F628a, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F628a, CPD_OFF
' Data Memory Code Protect
' Set to CPD_OFF for Development Copy
' Set to CPD_ON for Release Copy
@ DEVICE pic16F628a, PROTECT_OFF
define osc 4
CMCON=%00000111
trisb = %11111111
trisa = %00010111
chk_sum var byte
dgood var porta.2
serpin var porta.3
nokey con %11111111
synch con 254
address con %00000001
keyin var portb
keydata var byte
E VAR PORTB.0
D VAR PORTB.1
G VAR PORTB.2
A VAR PORTB.3
B VAR PORTB.4
C VAR PORTB.5
F VAR PORTB.6
na VAR PORTB.7
PreAmble CON $A5 ' 10100101
findkey:
if A = 0 then sw0
if B = 0 then sw1
if C = 0 then sw2
if D = 0 then sw3
if E = 0 then sw4
if F = 0 then sw5
if G = 0 then sw6

goto findkey
sw0:
keydata = %01010110
goto dout
sw1:
keydata = %01011001
goto dout
sw2:
keydata = %01011010
goto dout
sw3:
keydata = %01100101
goto dout
sw4:
keydata = %01100110
goto dout
sw5:
keydata = %01101001
goto dout
sw6:
keydata = %01101010
goto dout

dout:
low dgood
pause 100
chk_sum = (address + address)
chk_sum = chk_sum + (keydata + keydata)
goto transmit

transmit:

serout serpin,N9600[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
if keyin = nokey then x2
goto findkey

x2:
high dgood
keydata = %00000000
serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
goto findkey
end

Anyhow, it works, tx side seems to be fine, my big problem is on the rx side each output that is a if/then statement will come on but when I release the button it stays on. The only way to make it turn off is to press another momentary button. The toggle function works fine, although if I hold onto the button to long it will toggle on/off. if/then example I have tried after Skimask post:

if mydata1=%01011001 then pause 50
if mydata1=%01011001 then
high 1
else
low 1
endif

I am using a direct cable link, and will try again later with the Linx modules.

"logic dictates, cmcon goes before anything portA, then tris registers dictate input or output, then port dictates hi or low status."
And I have modified the suggestion to this tx program yet, but I will later this evening.

Many thanks for the help.

skimask
- 7th August 2008, 05:13
I missed this when it was first posted (on the basis I don't read everything)... this is news to me... do you have examples?
I've written some pretty complex programs and haven't come across this problem - unless of course you're dropping COLONs all over the place and your GOSUB follows on... not something I've ever done (on one line).

I don't have any specific examples off the top of my head, although I've never actually used an If/Then/Else on one continuous line. Thanks for the idea :D
And yes, you are right about the colons. I try to stuff as much into a single line as I can, just so I don't have to scroll up and down so much. Therefore I tend to run into various issues with the aforementioned statements. I just know what works, and when something doesn't, I have a rough idea of where to look :) My current 'project' is about 4000 lines of my 'colonized' code. I split it up one day and came up with something around 19,000 lines. Sure, it's not easy to read, but I wrote it, I know what's where, and for the most part, others don't have to read it (and probably couldn't anyways :) )
And you're right, it's probably not so much the way that code should be written, but there's nothing that says it CAN'T be written in such a fashion...Unless of course it doesn't work right.

skimask
- 7th August 2008, 05:17
Anyhow, it works, tx side seems to be fine, my big problem is on the rx side each output that is a if/then statement will come on but when I release the button it stays on. The only way to make it turn off is to press another momentary button. The toggle function works fine, although if I hold onto the button to long it will toggle on/off. if/then example I have tried after Skimask post:
Debouncing isn't going to work very well without a 'switch' to actually debounce. If you're trying to debounce a received signal, I don't think that it's the right way to go about it.
This shouldn't be all that hard...but if you continue having problems, you might want to thinking about sending MAKE and BREAK codes with the TX similar to a PC keyboard.
I.E. code1 = button 1 pressed
code2 = button 1 released
code3 = button 2 pressed....
and so on and so on...

Archangel
- 7th August 2008, 05:56
is the space in the word "address" a cut paste error or a typo in the transmit code?


transmit:

serout serpin,N9600[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,<font color=red>addr ess</font color>,address,keydata,keydata,chk_sum]
if keyin = nokey then x2
goto findkey

x2:
high dgood
keydata = %00000000
serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,<font color=red>addr ess</font color>,address,keydata,keydata,chk_sum]
goto findkey
end

Archangel
- 7th August 2008, 06:42
I can turn on any of the ports using realterm and a max232 using the code below.


INCLUDE "MODEDEFS.BAS"
;@ DEVICE PIC16F628a,XT_OSC
;@ DEVICE pic16F628a, WDT_OFF
;@ DEVICE pic16F628a, PWRT_ON
;@ DEVICE pic16F628a, MCLR_ON
;@ DEVICE pic16F628a, BOD_ON
;@ DEVICE pic16F628a, LVP_OFF
;@ DEVICE pic16F628a, CPD_OFF
;@ DEVICE pic16F628a, PROTECT_OFF
@MyConfig = _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF
@MyConfig = MyConfig & _BODEN_OFF
@ __config MyConfig
DEFINE OSC 20
CMCON = 7

trisa = %00000010
trisb = %00000000

PORTA = 0
PORTB = 0

MYDATA1 VAR byte
MYDATA2 var byte
address1 var byte
address2 var byte
checksum var byte
chk_sum var byte
;serpin VAR PortA.1
PAUSE 50

loop:
high portA.2
pause 500
low portA.2
gosub loop1
serout portA.0,T9600,[254,2,mydata1," ",mydata2," "]
serout portA.0,T9600,[$FE,$C0,address1," ",address2," " ]
'CheckSum = (address1 + address2)
'CheckSum = CheckSum + (mydata1 + mydata2)
'IF checksum != chk_sum THEN loop
'IF (mydata1) != (mydata2) THEN loop
'IF (address1) != (address2) THEN loop

if mydata1= 86 then toggle portB.0


if mydata1= 89 then
'while mydata1 = 89 ' trying this example for debounce
'pause 50
'wend
high PortB.1

else
low PortB.1
endif

if mydata1 = 90 then
'while mydata1 = 90
'pause 50
'wend
high PortB.2

else
low PortB.2
endif

if mydata1= 101 then
'while mydata1 = 101
'pause 50
'wend
high PortB.3

else
low PortB.3
endif

if mydata1 = 102 then
'while mydata1 = 102
'pause 50
'wend
high PortB.4

else
low PortB.4
endif

if mydata1=105 then toggle PortB.5
if mydata1=106 then toggle PortB.6

goto loop

loop1:
SERIN2 PortA.1,84,[DEC address1,DEC ADDRESS2,DEC MYDATA1,DEC MYDATA2]
Return
end

Much stuff just commented out, serouts added so as to see variable contents, a blinky added for life signs, easy to undo. Thanks for sharing your problem, I got to learn too.

sayzer
- 7th August 2008, 08:36
Tazntex,

You are forgetting something I think.


When you press a button, your IF statement works only WHILE you are pressing the button.

When you do NOT press a button, your ELSE statement works continuously.

This is why "you think" that you see no change; because it happens so fast when you hit the button down and release it.

I think you should set an IF statement with the first button press, and keep it that way until you press it second time. Like a flip flop.

tazntex
- 7th August 2008, 12:54
Yes Joe that is a typo in the copy and paste. When I read all the replies with example code how are ya'll inserting the code block whereas I am just copying and pasting my code to this reply. I am assuming, which I don't like to do, that under additional options I could attach my questionable code.
What does the DEC mean and do? In the manual I understand it to be Decimal as in MYDATA would be recieved as a decimal. After reading many post on Manchester, I would think I would only be using Binary, but it would seem in the end Binary, Decimal and Hex would all be the same.

loop1:
SERIN2 PortA.1,84,[DEC address1,DEC ADDRESS2,DEC MYDATA1,DEC MYDATA2]
Return
end

Sayzer, in the flip/flop method you are speaking of, I can see how to turn an output on/off with the toggle command, but I do not understand how I could press the button on the tx and the output would come on the receiver but would have to release the button and press again to make it go off. As I have it using the toggle command as long as I am pressing the button on the tx the rx output just cycles on and off. Well I will keep reading the PBP manual maybe thats in there and I have missed it. By the way I am using the PBP v2.50 with the update installed.

Many, many thanks to all for your help and patience.

skimask
- 7th August 2008, 15:38
Yes Joe that is a typo in the copy and paste. When I read all the replies with example code how are ya'll inserting the code block whereas I am just copying and pasting my code to this reply. I am assuming, which I don't like to do, that under additional options I could attach my questionable code.
Check out this page for all the neat stuff here on the forums:
http://www.picbasic.co.uk/forum/misc.php?do=bbcode#imgcode

(take out the extra spaces here)

[ code ]This will go in a code block[ / code ]


This will go in a code block
And there's a bunch more formatting type stuff listed on that page linked above.



What does the DEC mean and do? In the manual I understand it to be Decimal as in MYDATA would be recieved as a decimal.
It actually means that SERIN (or whatever) will wait until it receives x characters to be interpreted as a decimal.
For instance:
SERIN xxx,xxx,xxx, [ DEC1 x ]
Waits for a single character between "0" and "9", preceeded by a character that's not between "0" and "9" and followed by another character that's not between "0" and "9".
Another for instance:
SERIN xxx,xxx,xxx, [ DEC5 x ]
Waits for 5 consecutive characters that are each between "0" and "9", preceeded by any character that's not between "0" and "9" and followed by another character that's not between "0" and "9".
So, the DEC (or HEX or BIN) modifier actually does 'parsing' of an input stream for you.
HEX would wait for ASCII characters between "0" and "9" in addition to "a" (or "A") through "f" (or "F").
BIN would only wait for "0" and "1".
For reference..."0" = ASCII code 48 ($30), "9" = ASCII code 57 ($39), and so on. Just so we're on the same page here.

Archangel
- 7th August 2008, 17:43
Mostly it means I have an easier time dealing with decimal numbers, than binary, as Jeremy pointed out you can use hex, binary, or decimal, it's all good.


As I have it using the toggle command as long as I am pressing the button on the tx the rx output just cycles on and off.
Your receiver doesn't just stop because you are still holding the button, and besides your transmitter is sending a signal, not some d/c value, additionally when you go the R/F route, this is where switch debouncing comes in and should be on the transmitter I think. I think the transmitter program should send a packet of data, size determined by you, for each valid button press and not just keep sending continuously, otherwise scratchy switches will corrupt your data.

tazntex
- 8th August 2008, 00:21
ok, how come if i use this on the receiver:

if mydata1=%01100101 then pause 50
if mydata1=%01100101 then
high 4
else
low 4
endif

And with this clip on the transmitter:

transmit:

serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
if keyin = nokey then x2
goto findkey

x2:
high dgood
keydata = %00000000
serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
goto findkey

Why is it that by transmiting to the receiver =%01100101 by pressing the button then once I release the button the transmitter sends keydata = %00000000 which I would think the ELSE would then turn PORTB.4 LOW, but it doesn't. Only when I press another button which activates the other three outputs which are using IF/THEN, ELSE. Then and only then will PORTB.4 go LOW, but the next output comes on and stay on like PORTB.4. Now the outputs I am toggling work just fine. Hopefully I am making some sense, and the folks with the white coats won't come get me:)
Thanks again, Ya'll have been a great help

mackrackit
- 8th August 2008, 01:58
On the receiver end change to SERIN2.

Have it wait for the "synch" and use the time out modifier. The time out modifier will take place of the "else".

So the reciever is "waiting" for the synch, if it does not get in in the specified time, the program will jump to where ever.

You do not need to send keydata = %00000000 . Once the receiver does not see the synch it will time out and go on.

I agree with Joe, debounce on the receiver end. Here is a simple but not so efficient way one of my kids debounced with a radio using SERIN.

The code waits for the number 9 and writes the next bit to a variable. 9 is the synch in this case. Then if the variable = 3 enough times then another piece of code executes. You will see the the next piece is called "send". He was doing a relay project. If the 9 was not seen the program went back to looping, if the 9 and 3 was seen the program sends something out to the next receiver.

He debounced the heck out of it:) I was told there is to much noise in the shop.



LOOP:
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D1
GOTO LOOP

D1:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D2
GOTO LOOP

D2:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D3
GOTO LOOP

D3:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D4
GOTO LOOP

D4:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D5
GOTO LOOP

D5:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN SEND
GOTO LOOP

D6:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D7
GOTO LOOP

D7:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D8
GOTO LOOP

D8:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D9
GOTO LOOP

D9:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D10
GOTO LOOP

D10:
PAUSE 50
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN SEND
GOTO LOOP

D11:
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D12
GOTO LOOP

D12:
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN D13
GOTO LOOP

D13:
SERIN PORTC.4,T2400,[9],net
IF net = 3 THEN SEND
GOTO LOOP

tazntex
- 8th August 2008, 02:32
That is interesting, thanks so much.

tazntex
- 14th August 2008, 03:47
After many tries I am about to take a hammer to my processor and just put it out of its misery. I have tried several different versions of this an they always do the same thing. I press a button on the transmitter data link works great on the the receiver the bits that I am toggling works good, but no matter what I try the outputs on portb 1-4 just come on but they won't go off. I've added a few lines to make sure the receiver knows keydata=%00000000 when no key is pressed, even add a little delay to hear it on my service monitor.
transmit:

serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
if keyin = nokey then pause 10
if keyin = nokey then x2
goto findkey

x2: 'I have taken this out as suggested but then what tells the Rx mydata1,mydata2 to equal %00000000 other than CLEAR or mydata1=%00000000?
high dgood
keydata = %00000000
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
goto findkey

On the receiver side:
loop1:
SERIN2 serpin,16780,[wait(254),address1,address2,mydata1,mydata2,chk_su m]
Return
when I am sending data everything checks out fine using the checksum then continues on, but for whatever reason even though when I release the key on the transmitter, and it sends keydata=%00000000 , I would think mydata1,mydata2 being (%00000000 + %00000000) would match and continue on so the ELSE command would return the outputs back to their low state but it doesn't.

loop:
gosub loop1 ' check serial
CheckSum = (address1 + address2)
CheckSum = CheckSum + (mydata1 + mydata2)
IF checksum != chk_sum THEN loop
IF (mydata1) != (mydata2) THEN loop
IF (address1) != (address2) THEN loop



Anyone know what I am doing wrong?

skimask
- 14th August 2008, 04:34
Read thru your code very very carefully again! And again....and again....and again....
Use a piece of paper or something to keep track of what your program is doing if you have to.
Your transmit program is doing EXACTLY what you are writing it to do, like I was trying to explain in the other thread.
YOU aren't doing anything wrong, your program isn't doing anything wrong. Again, it's doing exactly what you programmed it to do.
What you have to do is program it to do what you WANT it to do...
The problem is in your program, your logic flow. Read it...You'll get it...

Darrel Taylor
- 14th August 2008, 05:15
You know, the idea of this forum is to provide asnwers for peoples questions.

If you see something helpfull that you can provide, by all means, speak up and point it out.

Telling someone to re-read their own program over and over, as if (ha ha, I know what's wrong and you don't).

Does not help. http://www.picbasic.co.uk/forum/images/icons/icon4.gif
<br>

skimask
- 14th August 2008, 12:47
I would, and I will, eventually, maybe. The O/P is seeming to have difficulty in realizing that PICs do only what they are programmed to do...nothing more, nothing less, and just because a programmer wants the PIC to do something, doesn't mean it's going to do it, at least not without proper program and logic flow and hardware to back it up. Feeding, fishing, teaching, learning, all that stuff... He'll get it eventually. He's already figured out a few things for himself. It's only a matter of time before a light bulb goes on...

tazntex
- 14th August 2008, 13:00
Ya'll are great, I will go back through this, print it and try to figure it out. Will be back after awhile and let ya'll know what I have or haven't found.



Thanks again

tazntex
- 14th August 2008, 15:03
INCLUDE "bs2defs.bas"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
' Watchdog Timer
@ DEVICE pic16F628a, PWRT_ON
' Power-On Timer
@ DEVICE pic16F628a, MCLR_ON
' Master Clear Options (Internal)
@ DEVICE pic16F628a, BOD_ON
' Brown-Out Detect
@ DEVICE pic16F628a, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F628a, CPD_OFF
' Data Memory Code Protect
' Set to CPD_OFF for Development Copy
' Set to CPD_ON for Release Copy
@ DEVICE pic16F628a, PROTECT_OFF
DEFINE OSC 4
mydata1 VAR byte
mydata2 var byte
address1 var byte
address2 var byte
checksum var byte
chk_sum var byte
serpin VAR porta.1 'serial input pin
PORTA = 0
PORTB = 0
trisa = %00000010
trisb = %10000000
CMCON=%00000111
PAUSE 50 ' settle time

loop:
clear 'Do I really need to do this? I tried to make sure everything is back to 0
gosub loop1 'After button is released on tx mydata1=%00000000
CheckSum = (address1 + address2) 'address sent %00000001 added together in this case this = 10
CheckSum = CheckSum + (mydata1 + mydata2) '10 + (%00000000 +%00000000)= 10
IF checksum != chk_sum THEN loop 'checksum = chk_sum because of above
IF (mydata1) != (mydata2) THEN loop '(mydata1) = (mydata2)
IF (address1) != (address2) THEN loop '(address1) != (address2)
'Since everything should be a match We move on
if mydata1=%01010110 then rly1 'this works so I will go to the next one

if mydata1=%01011001 then rly2 '1st case will turn portb.1 on
if mydata1=%00000000 then rly2 '2nd case appears to do nothing but as I stated above the check
'should be correct and mydata1=%00000000
if mydata1=%01011010 then rly3 'I will skip the rest of this area because it
if mydata1=%00000000 then rly3 'behaves the same as rly2 the rly6 rly7 ok.

if mydata1=%01100101 then rly4
if mydata1=%00000000 then rly4

if mydata1=%01100110 then rly5
if mydata1=%00000000 then rly5

if mydata1=%01101001 then rly6
if mydata1=%01101010 then rly7

rly1:
if mydata1=%01010110 then pause 10
if mydata1=%01010110 then toggle 0
pause 10
goto loop
rly2:
if mydata1=%01011001 then pause 10 'Down here the fun begins
if mydata1=%01011001 then high 1 '1st case will turn portb.1 on
if mydata1=%00000000 then low 1 'Since the checksum is correct why didn't I get here
'mydata1=%00000000 so this should go low but doesn't
goto loop 'here we go back to loop so I will goto loop1
rly3:
if mydata1=%01011010 then pause 10
if mydata1=%01011010 then high 2
if mydata1=%00000000 then low 2
goto loop
rly4:
if mydata1=%01100101 then pause 10
if mydata1=%01100101 then high 3
if mydata1=%00000000 then low 3
goto loop
rly5:
if mydata1=%01100101 then pause 10
if mydata1=%01100110 then high 4
if mydata1=%00000000 then low 4
goto loop
rly6:
if mydata1=%01101001 then pause 10
if mydata1=%01101001 then TOGGLE 5
pause 10
goto loop
rly7:
if mydata1=%01101010 then pause 10
if mydata1=%01101010 then TOGGLE 6
pause 10
GOTO loop


loop1:
SERIN2 serpin,16780,[wait(254),address1,address2,mydata1,mydata2,chk_su m]
Return
'loop1 receives the following
'transmit:

' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
' if keyin = nokey then pause 10
' if keyin = nokey then x2
' goto findkey

'x2:
' high dgood
' keydata = %00000000
' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
' goto findkey

'on the GOSUB loop1 after the tx button is released I should move on to x2
'x2 sends the preamble, and I am still getting my synch plus address,data an chk_sum
'so I should pass through the checksum area of my receiver code and on to the rest of
'my program. When the program arrives at "if mydata1=%00000000 then rly2 " it should
'ask "if mydata1=%00000000 then low 1" and respond and portb.0 should go low but it
'never seems to get there. I switch from the If/Then/Else to make it simpler for me
'to break it down.

'I've been over the tx side and I feel confident everything there is working correctly

' Am I heading in the right direction?

skimask
- 14th August 2008, 15:26
So between post #34 and #39, that is your entire code for both TX and RX?

And is it possible for you to connect an LCD to the RX for troubleshooting? Or at least replace those relays with LEDs.

tazntex
- 14th August 2008, 15:38
Skimask here is the entire program for both sides:

TRANSMITTER

INCLUDE "MODEDEFS.BAS"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
@ DEVICE pic16F628a, PWRT_ON
@ DEVICE pic16F628a, MCLR_ON
@ DEVICE pic16F628a, BOD_ON
@ DEVICE pic16F628a, LVP_OFF
@ DEVICE pic16F628a, CPD_OFF
@ DEVICE pic16F628a, PROTECT_OFF
define osc 4
CMCON=%00000111
trisb = %11111111
trisa = %00010111
chk_sum var byte
dgood var porta.2
serpin var porta.3
nokey con %11111111
synch con 254
address con %00000001
keyin var portb
keydata var byte
E VAR PORTB.0
D VAR PORTB.1
G VAR PORTB.2
A VAR PORTB.3
B VAR PORTB.4
C VAR PORTB.5
F VAR PORTB.6
na VAR PORTB.7
PreAmble CON $A5 ' 10100101 preamble
findkey:
if keyin != nokey then dout
goto findkey

pressed:
if A= 0 then pause 10
if A= 0 then keydata = %01010110

if B = 0 then pause 10
if B = 0 then keydata = %01011001

if C = 0 then pause 10
if C= 0 then keydata = %01011010

if D = 0 then pause 10
if D = 0 then keydata = %01100101

if E = 0 then pause 10
if E = 0 then keydata = %01100110


if F = 0 then pause 10
if F = 0 then keydata = %01101001

if G = 0 then pause 10
if G = 0 then keydata = %01101010

return
dout:
gosub pressed
low dgood
pause 100
chk_sum = (address + address)
chk_sum = chk_sum + (keydata + keydata)
goto transmit


transmit:

serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
if keyin = nokey then pause 10
if keyin = nokey then x2
goto findkey

x2:
high dgood
keydata = %00000000
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
goto findkey

end


RECEIVER

INCLUDE "MODEDEFS.BAS"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
@ DEVICE pic16F628a, PWRT_ON
@ DEVICE pic16F628a, MCLR_ON
@ DEVICE pic16F628a, BOD_ON
@ DEVICE pic16F628a, LVP_OFF
@ DEVICE pic16F628a, CPD_OFF
@ DEVICE pic16F628a, PROTECT_OFF
DEFINE OSC 4
mydata1 VAR byte
mydata2 var byte
address1 var byte
address2 var byte
checksum var byte
chk_sum var byte
serpin VAR porta.1
PORTA = 0
PORTB = 0
trisa = %00000010
trisb = %10000000
CMCON=%00000111
PAUSE 50

loop:
clear
gosub loop1
CheckSum = (address1 + address2)
CheckSum = CheckSum + (mydata1 + mydata2)
IF checksum != chk_sum THEN loop
IF (mydata1) != (mydata2) THEN loop
IF (address1) != (address2) THEN loop

if mydata1=%01010110 then rly1

if mydata1=%01011001 then rly2
if mydata1=%00000000 then rly2

if mydata1=%01011010 then rly3
if mydata1=%00000000 then rly3

if mydata1=%01100101 then rly4
if mydata1=%00000000 then rly4

if mydata1=%01100110 then rly5
if mydata1=%00000000 then rly5

if mydata1=%01101001 then rly6
if mydata1=%01101010 then rly7

rly1:
if mydata1=%01010110 then pause 10
if mydata1=%01010110 then toggle 0
pause 10
goto loop

rly2:
if mydata1=%01011001 then pause 10
if mydata1=%01011001 then high 1
if mydata1=%00000000 then low 1
goto loop

rly3:
if mydata1=%01011010 then pause 10
if mydata1=%01011010 then high 2
if mydata1=%00000000 then low 2
goto loop

rly4:
if mydata1=%01100101 then pause 10
if mydata1=%01100101 then high 3
if mydata1=%00000000 then low 3
goto loop

rly5:
if mydata1=%01100101 then pause 10
if mydata1=%01100110 then high 4
if mydata1=%00000000 then low 4
goto loop

rly6:
if mydata1=%01101001 then pause 10
if mydata1=%01101001 then TOGGLE 5
pause 10
goto loop

rly7:
if mydata1=%01101010 then pause 10
if mydata1=%01101010 then TOGGLE 6
pause 10
GOTO loop


loop1:
SERIN2 serpin,16780,[wait(254),address1,address2,mydata1,mydata2,chk_su m]
Return

No LCD, I do have leds and also monitoring the pins with scope.

thanks

skimask
- 14th August 2008, 15:43
Quite frankly...what a cluster! You really have to work on your flow. There's too many places where you expect a value to be during one if/then, and then be there again at another if/then even though you just got done changing it.

Give me a few minutes to work it out...

tazntex
- 14th August 2008, 15:47
Yes, I have a mess thanks for your help

skimask
- 14th August 2008, 15:53
Ok, for awhile, forget about your receiver...
Forget about the sync, forget about the preamble, forget about the checksum, forget about everything else. Pretend this TX is transmitting to nowhere and nowhere likes it that way...

You know how to set up your PIC, you can set the pins, etc....
You have 7 buttons, each connected to a PortB pin...
When these buttons AREN'T pressed, the pins are high, when pressed the pin is low...
Easy enough...

Write a program that does nothing but reads the state of the pins and sends that value out in a SEROUT statement of some sort.

tazntex
- 14th August 2008, 19:28
ok, on the transmitting side after stripping everything down I am sending the data for the button that is pressed and when I release each button I send the data again and it clears that particular output on the receiver. Seems to work ok. Thanks for everyones help and I read something here on the forum about closing the post when finished so,... if so how do I do that?

skimask
- 14th August 2008, 19:49
ok, on the transmitting side after stripping everything down I am sending the data for the button that is pressed and when I release each button I send the data again and it clears that particular output on the receiver. Seems to work ok. Thanks for everyones help and I read something here on the forum about closing the post when finished so,... if so how do I do that?

Well, what I'm trying to do is show you a much better way of doing things, one that's much much easier to read thru and takes up a lot less code space, by breaking it down, and building it back up again.

Closing - I wouldn't worry about it. I think I've only ever seen a handful of threads that are closed here.

tazntex
- 14th August 2008, 22:48
Thanks Skimask and everyone else who has helped me. I appreciate you trying to teach me by giving pointers, but this has been a rough one :) I am by no means a quitter, thought about it though, especially through the last couple of post. I am laughing now. Everything seems fine. I can pressed the button now portb 1-4 are momentary and stay on as long as I am pressing then go off when I release them. Every once in awhile one will stay on and I'll press the button and release it and that output will clear. Thanks again to all