Shop OBEX P1 Docs P2 Docs Learn Events
Strings, Integers, and Decimals - What am I missing? — Parallax Forums

Strings, Integers, and Decimals - What am I missing?

sethen320sethen320 Posts: 7
edited 2012-05-15 22:11 in Propeller 1
I have an application (Hummingbird_Ctrlr.spin) which is supposed to pulse 4 pins (which will drive pumps) based on characteristics received via RS-232. The characteristics are "off time" and "on time". Both are in milliseconds. Using the attached code I can send the command to change the speed to the propeller and it is able to echo back what I sent with no problems, but it does not have an effect on the output. It seems as if it's not reading the number correctly. If anyone has any clues as to what I may be doing wrong I would really appreciate it. I believe that somewhere I am not handling the conversion from string to integer correctly. I have read the documentation, but I can't find my problem.

ASCII0_STREngine_1.spinExtended_FDSerial.spinFullDuplexSerial.spinHummingbird_Ctrlr_0.spin

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-15 09:08
    I usually add more debug statements to a program that isn't behaving the way I expect it to.

    I'd suggest you add statements to display the interger values after the converstions.

    For example:
          'convert command string received to integer
          intCmd:=Str.decimalToInteger(@RecCmd)
         [B]FDX.str(string(13, "intCmd = "))
          FDX.dec(intCmd)
    [/B]     
          if intCmd==100
            pEnable:=0  'pumps off
            FDX.str(string(13, "pumps off"))
          next 
          
          if intCmd==101
            pEnable:=1  'pumps on
            FDX.str(string(13, "pumps on"))
          next
     
          if intCmd==142 'detected pump setting command; more parameters needed
            intPar1:=Str.decimalToInteger(@RecPar1)'loaded pump number
            intPar2:=Str.decimalToInteger(@RecPar2)'loaded off time
            intPar3:=Str.decimalToInteger(@RecPar3)'loaded on time
            [B]FDX.str(string(13, "pump parameters = "))
            FDX.dec(intPar1)
            FDX.str(string(", "))
            FDX.dec(intPar2)
            FDX.str(string(", "))
            FDX.dec(intPar3)
    [/B]       
            if intPar1==1                                   'setting up pump 1
              P1_Off:=intPar2
              P1_On:=intPar3
             
    
    

    I had to look up the next statement. I haven't ever used it in one of my Spin programs.

    I would have used elseif for each condition check (of the same variable) after the first if statemtment.

    Using next at the end of a loop isn't necessary.
  • sethen320sethen320 Posts: 7
    edited 2012-05-15 10:28
    Thanks, I'll give it a shot later.


    Duane Degn wrote: »
    I usually add more debug statements to a program that isn't behaving the way I expect it to.

    I'd suggest you add statements to display the interger values after the converstions.

    For example:
          'convert command string received to integer
          intCmd:=Str.decimalToInteger(@RecCmd)
         [B]FDX.str(string(13, "intCmd = "))
          FDX.dec(intCmd)
    [/B]    
          if intCmd==100
            pEnable:=0  'pumps off
            FDX.str(string(13, "pumps off"))
          next 
          
          if intCmd==101
            pEnable:=1  'pumps on
            FDX.str(string(13, "pumps on"))
          next
     
          if intCmd==142 'detected pump setting command; more parameters needed
            intPar1:=Str.decimalToInteger(@RecPar1)'loaded pump number
            intPar2:=Str.decimalToInteger(@RecPar2)'loaded off time
            intPar3:=Str.decimalToInteger(@RecPar3)'loaded on time
            [B]FDX.str(string(13, "pump parameters = "))
            FDX.dec(intPar1)
            FDX.str(string(", "))
            FDX.dec(intPar2)
            FDX.str(string(", "))
            FDX.dec(intPar3)
    [/B]      
            if intPar1==1                                   'setting up pump 1
              P1_Off:=intPar2
              P1_On:=intPar3
             
    
    

    I had to look up the next statement. I haven't ever used it in one of my Spin programs.

    I would have used elseif for each condition check (of the same variable) after the first if statemtment.

    Using next at the end of a loop isn't necessary.
  • sethen320sethen320 Posts: 7
    edited 2012-05-15 21:11
    Thanks for the help. I have only been at this for about 2 weeks and I am on a very short development cycle. I picked the propeller because I thought it could handle the job perfectly but I've never worked with it before. Your troubleshooting code helped me out a lot and I think I have it figured out now. Part of it was user error. I had originally written the program and was having random comm errors (probably due to the fact that I had no idea what I was doing). Last night I decided to re-write the code from the ground up using everything I had learned and nothing would communicate. This prompted me to put out this post as I was ready to tear my hair out.

    I plugged in your debugging statements and guess what I realized...I was using the old protocol I had written before, not the new one I put in when I re-wrote it. I was issuing the wrong commands...DUH! I feel like an idiot now. I will be putting in some error handlers.

    Thank you again.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-15 21:16
    No problem.

    Bugs are usually easy to fix once the problem is found.

    I sometimes have code that seems to be 90% debug statements. If I add enough, I usually find the problem (and then slap my forehead since it should have been obvious in the first place).
  • AribaAriba Posts: 2,690
    edited 2012-05-15 21:20
    Your NEXT commands make no sense in the control loop. The first NEXT will jump over all the further code in the loop. So only the intCmd==100 part can really work, the rest is not even checked.
    You could indent the NEXTs inside the IF command, but better delete them all, they are not necessary, the loop works without them (the only effect is that it speeds up the loop a bit, but speed does anyway not matter here).

    Andy
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-15 21:26
    Ariba wrote: »
    Your NEXT commands make no sense in the control loop. The first NEXT will jump over all the further code in the loop. So only the intCmd==100 part can really work, the rest is not even checked.
    You could indent the NEXTs inside the IF command, but better delete them all, they are not necessary, the loop works without them (the only effect is that it speeds up the loop a bit, but speed does anyway not matter here).

    Andy

    I didn't catch the indentation of the next statements. Your right. I just assumed they were indented past the previous "if" statement. The "next" statemets looked like they belong were they are because of our experience with C (and I think some forms of BASIC).

    If "elseif" statements were used, then subsequent "if" elvaluations wouldn't need to be made also saving time. I assume an unevaluted "elseif" is faster than evaluating an 'if" statement.
  • AribaAriba Posts: 2,690
    edited 2012-05-15 21:50
    Yes ELSEIFs work fine here, but if I have to check the same variable against different values, I like most the CASE construct:
    case intCmd
          100: pEnable:=0  'pumps off
               FDX.str(string(13, "pumps off"))
          
          101: pEnable:=1  'pumps on
               FDX.str(string(13, "pumps on"))
     
          142: 'detected pump setting command; more parameters needed
               intPar1:=Str.decimalToInteger(@RecPar1)'loaded pump number
               ...
               if intPar1==1
                  ...
    

    Andy
  • sethen320sethen320 Posts: 7
    edited 2012-05-15 22:07
    I like that idea. I'm going to implement it and see if it improves my performance (it will certainly simplify my code). I'll let you know what happens. Thanks!
    Ariba wrote: »
    Yes ELSEIFs work fine here, but if I have to check the same variable against different values, I like most the CASE construct:
    case intCmd
          100: pEnable:=0  'pumps off
               FDX.str(string(13, "pumps off"))
          
          101: pEnable:=1  'pumps on
               FDX.str(string(13, "pumps on"))
     
          142: 'detected pump setting command; more parameters needed
               intPar1:=Str.decimalToInteger(@RecPar1)'loaded pump number
               ...
               if intPar1==1
                  ...
    

    Andy
  • sethen320sethen320 Posts: 7
    edited 2012-05-15 22:11
    Did I mention that I'm new to this? :)

    I really appreciate all of the suggestions. This is better learning than the tutorials in the help file.
    Duane Degn wrote: »
    I didn't catch the indentation of the next statements. Your right. I just assumed they were indented past the previous "if" statement. The "next" statemets looked like they belong were they are because of our experience with C (and I think some forms of BASIC).

    If "elseif" statements were used, then subsequent "if" elvaluations wouldn't need to be made also saving time. I assume an unevaluted "elseif" is faster than evaluating an 'if" statement.
Sign In or Register to comment.