Menu

Converting some Arduino code to GCB

Help
viscomjim
2016-07-11
2016-07-12
  • viscomjim

    viscomjim - 2016-07-11

    I am trying to convert some Arduino code to GCB as follows...

    if (MaxLevel >= 5)
      {
        Divisor=Divisor+1;
        ChangeTimer=0;
      }
      else
        if(MaxLevel < 4)
        {
          if(Divisor > 65)
            if(ChangeTimer++ > 20)
            {
              Divisor--;
              ChangeTimer=0;
            }
        }
        else
        {
          ChangeTimer=0; 
        }
      }
    

    Here is what I am guessing at...

      if MaxLevel >= 5 then
          Divisor = Divisor + 1
          ChangeTimer = 0
          end if
    
      else
    
          if MaxLevel < 4 then
              if Divisor > 65 then
                  ChangeTimer = ChangeTimer + 1
                   if ChangeTimer > 20 then
                       Divisor = Devisor -1
                       ChangeTimer = 0
                   end if.....
    

    The rest of it I can't figure out as there are multiple ifs and else if which I don't think GCB has. Can anyone with a bit of Arduino background give me a hand? I am at a loss... Thanks for any and all help!

     
  • Anobium

    Anobium - 2016-07-12

    You are close. Not a constraint of Great Cow BASIC to complete.

    ~~~~~
    if MaxLevel >= 5 then
    Divisor = Divisor + 1
    ChangeTimer = 0
    end if

    else

      if MaxLevel < 4 then
          if Divisor > 65 then
              ChangeTimer = ChangeTimer + 1
               if ChangeTimer > 20 then
                   Divisor = Devisor -1
                   ChangeTimer = 0
                 else
                     ChangeTimer = 0                 
                 end if
           end if
    end if
    

    ~~~~~

    Code not tested\compiled. Buyer beware. 😃

     
  • Chris Roper

    Chris Roper - 2016-07-12

    The braces are unballanced in the origional Arduino code so it is not a functional example to start with.
    However it looks like the missing brace should be between lines 9 and 10.
    With that corrected this is how I would write it in GCBasic:

    sub Arduino
      if (MaxLevel >= 5) then
        Divisor += 1
        ChangeTimer=0
      else
        if(MaxLevel < 4) then
            if(Divisor > 65) then
                if(ChangeTimer++ > 20) then
                  Divisor -= 1
                  ChangeTimer=0
                end if
            end if
        else
          ChangeTimer=0
        end if
      End if
    end sub
    

    insufficiant info was supplied for testing but this code should work.

    Simplifying the if statement and eliminating redundancys we get:

    sub Arduino
      if (MaxLevel >= 5) then
        Divisor += 1
      else
        if((MaxLevel < 4) and (Divisor > 65)) and (ChangeTimer++ > 20) then Divisor -= 1 
      End if
      ChangeTimer = 0
    end sub
    

    Cheers
    Chris

     

    Last edit: Chris Roper 2016-07-12
  • lhatch

    lhatch - 2016-07-12

    Looks like K&R C code to me. Is this all the code? Also, the first endif needs to be removed.

    if MaxLevel >= 5 then
          Divisor = Divisor + 1
          ChangeTimer = 0
      else
          if MaxLevel < 4 then
              if Divisor > 65 then
                  ChangeTimer = ChangeTimer + 1
                   if ChangeTimer > 20 then
                       Divisor = Devisor -1
                       ChangeTimer = 0
                   end if....
    
    OR more like this.
    
    if MaxLevel >= 5 then
          Divisor = Divisor + 1
          ChangeTimer = 0
    else
        if MaxLevel < 4 then
             if Divisor > 65 then
                 ChangeTimer = ChangeTimer + 1
                 if ChangeTimer > 20 then
                      Divisor = Devisor -1
                      ChangeTimer = 0
                  else
                        ChangeTimer = 0                 
                  end if
             end if
        end if
    end if
    
     
  • viscomjim

    viscomjim - 2016-07-12

    Thanks all for the help. A couple of things...

    I didn't know you could do something like this

    ChangeTimer++ > 20

    very cool!

    Also, is there any limit, other than memory for else ifs, ie...

    if (MaxLevel >= 5) then
    Divisor += 1
    else
    if((MaxLevel < 4) and (Divisor > 65)) and (ChangeTimer++ > 20) then Divisor -= 1
    else
    if bla bla bla then
    bla bla bla
    else
    if bla bla bla bla then
    bla bla bla
    else
    if bla bla bla bla bla then
    bla bla bla
    End if

     

    Last edit: viscomjim 2016-07-12
  • Anobium

    Anobium - 2016-07-12

    No. The syntax is if - then - else - end if. Yoy cannot use if - then - else - else - else - bla bla- end if. This is a constraint of Great Cow BASIC and also see this Google search - this will highlight some of the views of using if statements.

    There are better options to resolve this Select Case - Case - End Case

     
  • viscomjim

    viscomjim - 2016-07-12

    Thanks for that info Evan. In using Select Case, can you pass variables to different subroutines something like this...

    select case testvar
    case 1
        blasub1 12, 44
    case 2
        blasub2 88
    case 3
        blasub3 44, 88
    end select
    

    Or can you call the same sub and pass different variables like this...

    select case testvar
    case 1
        blasub1 12, 44
    case 2
        blasub1 88, 72
    case 3
        blasub1 44, 88
    end select
    
     

Log in to post a comment.