PDA

View Full Version : Help with morse code program



A.C. fishing
08-04-2006, 12:39 AM
I have two pushbuttons that I have made sure are working right in other programs. What is wrong with my morsecode program below? Possible it takes too much time to load, and it requires a lot of power. My homework's little green LED goes off when i hit the button that translates the letter attached to IN1. It does require a lot of memory... Is there a way to be a little more creative with the programming so it doesn't include all those ELSEIF statements? I'm also going to try a new 9-volt for it.
thanks,
AC


' {$STAMP BS2}
' {$PBASIC 2.5}
counter VAR Word
morse VAR Word
a VAR Word 'a=amount
dashdecoder VAR Word
dotdecoder VAR Word
top:
DO
IF IN0 = 0 THEN
FOR counter = 1 TO 1
a = a + 1
IF a > 120 THEN
morse = 2
ELSE '2- dash 1- dot
morse = 1
ENDIF
PAUSE 10
NEXT
ENDIF
IF morse = 1 THEN
dotdecoder = dotdecoder + 1
ELSEIF morse = 2 THEN
dashdecoder = dashdecoder + 1
ENDIF
LOOP UNTIL IN1 <> 0
IF dotdecoder = 1 AND dashdecoder = 1 THEN
DEBUG "a", CR
GOSUB top
ELSEIF dotdecoder = 1 THEN
DEBUG "b"
GOSUB top
ELSEIF dashdecoder = 1 THEN
DEBUG "c"
GOSUB top
ELSEIF dotdecoder = 2 AND dashdecoder = 2 THEN
DEBUG "d"
GOSUB top
ELSEIF dotdecoder = 2 AND dashdecoder = 1 THEN
DEBUG "e"
GOSUB top
ELSEIF dotdecoder =1 AND dashdecoder = 2 THEN
DEBUG "f"
GOSUB top
ELSEIF dashdecoder = 3 AND dotdecoder = 1 THEN
DEBUG "g"
GOSUB top
ELSEIF dashdecoder = 3 AND dotdecoder = 2 THEN
DEBUG "h"
GOSUB top
ELSEIF dashdecoder = 3 AND dotdecoder = 3 THEN
DEBUG "i"
GOSUB top
ELSEIF dotdecoder = 3 AND dashdecoder = 1 THEN
DEBUG "j"
GOSUB top
ELSEIF dotdecoder = 3 AND dashdecoder = 2 THEN
DEBUG "l"
GOSUB top
ELSEIF dashdecoder = 4 AND dotdecoder =1 THEN
DEBUG "m"
GOSUB top
ELSEIF dashdecoder = 4 AND dotdecoder = 2 THEN
DEBUG "n"
GOSUB top
ELSEIF dashdecoder = 4 AND dotdecoder = 4 THEN
DEBUG "o"
GOSUB top
ELSEIF dotdecoder = 4 AND dashdecoder = 1 THEN
DEBUG "p"
GOSUB top
ELSEIF dotdecoder = 4 AND dashdecoder = 2 THEN
DEBUG "q"
GOSUB top
ELSEIF dotdecoder = 4 AND dashdecoder = 3 THEN
DEBUG "r"
GOSUB top
IF dotdecoder = 4 AND dashdecoder = 4 THEN
DEBUG "s"
GOSUB top
ELSEIF dotdecoder = 5 AND dashdecoder = 1 THEN
DEBUG "t"
GOSUB top
ELSEIF dotdecoder = 5 AND dashdecoder = 2 THEN
DEBUG "u"
GOSUB top
ELSEIF dotdecoder = 5 AND dashdecoder = 3 THEN
DEBUG "v"
GOSUB top
ELSEIF dotdecoder = 5 AND dashdecoder = 4 THEN
DEBUG "w"
GOSUB top
ELSEIF dotdecoder = 5 AND dashdecoder = 5 THEN
DEBUG "x"
GOSUB top
ELSEIF dashdecoder = 5 AND dotdecoder = 1 THEN
DEBUG "y"
GOSUB top
ELSEIF dashdecoder = 5 AND dotdecoder = 2 THEN
DEBUG "z"
GOSUB top
ELSEIF dotdecoder = 3 AND dashdecoder = 5 THEN
DEBUG " "
GOSUB top
ELSE
DEBUG "Not in database!"
GOSUB top
ENDIF
ENDIF

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔



Somebody said...
-Never Underestimate the power of human stupidity.



Charlie Johnson
08-04-2006, 01:28 AM
Do not have time to run your code, but I would suggest you change all the GOSUBs to GOTOs

metron9
08-04-2006, 02:49 AM
Yes gosubs need returns or you crash program.

How about a lookup table?

your data goes from 1 to 5 on the dashdecoder and 1 to 5 on the dot decoder
so
Multiply dotdecoder by 5 and add dashdecoder

dotdecoder=5 * 5 = 25 + dotdecoder =1 table entry for array data 26 = 'z"

so

lookup=(dotdecoder*5) + dashdecoder

character=arraydata(lookup)

debug character

and get rid of all the if's and elseifs

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think outside the BOX!