Shop OBEX P1 Docs P2 Docs Learn Events
if statment question. please let this be a simple fix. — Parallax Forums

if statment question. please let this be a simple fix.

Rob311Rob311 Posts: 83
edited 2007-03-07 03:38 in BASIC Stamp
I'm inputing some numbers as a string to my bs2 (via bluetooth).
when i use an if statement, it will only spit out the 1st number. ie 300 is seen as 3.

also, why cant my if handle more then 1 digit? i know its b.c its a string, but can i use then?

here is a portion of the code

Recive:
'==BT recive==
SEROUT lcdpin, baud19200, [noparse][[/noparse]12] 'clear
SEROUT lcdpin, baud19200, [noparse][[/noparse]"Bluetooth Control"]

'serin pin,baud,[noparse][[/noparse]STR var\how many]
SERIN 0,84,[noparse][[/noparse]STR szData\3] <==== inputs 3 characters as a string

'send confirmation to program
SEROUT 1,84,[noparse][[/noparse]CR,"You sent ",STR szdata, CR] <== spits it back out as the full 3 digit number via bluetooth

'==Display Input==
SEROUT lcdpin, baud19200, [noparse][[/noparse]12] 'clear
SEROUT lcdpin, baud19200, [noparse][[/noparse]STR szData\3] <=== spits it out as full 3 digit number to my lcd screen
DEBUG szdata,CR <=== here it spits out only the first digit (3 if i type in 342)
PAUSE readtime

'==Switch Case==


IF (szdata = "1") THEN <===wont let me use more then 1 digit. why?
SEROUT lcdpin, baud19200, [noparse][[/noparse]12] 'clear
SEROUT lcdpin, baud19200, [noparse][[/noparse]"Case 1"]
PAUSE readtime
GOSUB fowardbump

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-05 02:20
    Other than the special cases of strings in input/output statements, there really are no strings in PBasic, only arrays of characters. When you refer to the name of an array as a variable, you get the first element. If you want to look at the numeric value of a string of digits, you can use one of the formatter prefixes like DEC. Your input statement would look like "SERIN 0,84,[noparse][[/noparse]DEC szData]". This would convert a sequence of ASCII digits into a number and store its value in szData. Similarly, you could echo the value back with "SEROUT lcdpin,baud19200,[noparse][[/noparse]DEC szData]". Now you can say "IF szData = 300" (if you declared szData as a word). Read the sections in the PBasic manual on SERIN/SEROUT. They describe the input and output editing possible with the many formatters.
  • Rob311Rob311 Posts: 83
    edited 2007-03-05 03:00
    I had done that (DEC in) previously, and it worked. the only problem was that i needed to send an alpha after my string of numbers to signal i was done with the transmission, is there another way around this? with the STG i can use the [noparse][[/noparse]STG sdData\3] and take in 3 digits
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-05 03:24
    If you have 3 digits, you can use DEC3 to accept a fixed length (of 3) sequence of digits. Again, look in the PBasic manual for details.

    You could also convert the 3 characters to a number like:
    value = 0
    for i = 0 to 2
    value = value * 10 + sdData(i) - "0"
    next i
    
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-03-05 03:54
    Hi Rob, if its going to be a lot of hassle to change your format you can do this with your string, for the first problem in the original post change this line

    DEBUG szdata,CR <=== here it spits out only the first digit (3 if i type in 342)

    TO

    DEBUG STR szdata,CR

    next convert the string to a number place in a word variable szFinal

    szFinal=(szData(0)-48)*100
    szData(1)=(szData(1)-48)*10
    szFinal=szFinal+szData(1)+szData(2)
    IF szFinal = 1 THEN

    Jeff T.

    EDIT I messed up the string to number conversion......should be correct now

    Post Edited (Unsoundcode) : 3/5/2007 7:01:22 AM GMT
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-03-05 14:10
    Rob -

    Unless I missed it somewhere, you've never shown how the variable "szData" is defined. If it's defined as:

    szData VAR BYTE

    that's the problem.

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 3/5/2007 2:22:07 PM GMT
  • Rob311Rob311 Posts: 83
    edited 2007-03-07 03:01
    Bruce, if that is the problem, why? and how do i fix it? just define it as something bigger? word maybe?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-07 03:38
    Yes, you have to define it as a word. A byte can only hold a value from 0 to 255. A word can hold a value from 0 to 65535 (or -32768 to 32767). All arithmetic on the Stamp is done with words (even if the result is stored back into a byte).
Sign In or Register to comment.