Menu

problem with condition test

Help
2008-08-12
2013-05-30
  • Nobody/Anonymous

    I am new to GCbasic and I am trying to convert a Mbasic progrqam. It comples ok but does not run. Where have I gone wrong? Each if statement is supposed to test the condition of two switchs and go to the right subroutine. I am not getting any output as far as I can tell.

    #chip 16f628, 20

    cmcon = 7

    #define Start porta.0
    #define Up porta.1
    #define Down porta.2
    #define Direction portb.0
    #define motor portb.1
    #define bulb portb.2

    dir a0 in
    dir a1 in
    dir a2 in
    dir b0 out
    dir b1 out
    dir b2 out

    Main:
    if start = 1 and Down = 1 then
    gosub tiltup
    end if

    if start = 1 and Up = 1 then
    gosub tiltdn
    end if
    goto main
    end

    tiltup:
    do while up = 0
    set bulb on
    set motor on
    loop
    set motor off
    return

    Tiltdn:
    do while down = 0
    set direction on
    set motor on
    loop
    set motor off
    set direction off
    set bulb off
    return

     
    • kent_twt4

      kent_twt4 - 2008-08-13

      The Comcon = 7 is set by GCBasic in the Initsys sub, so you can skip that.

      May not be your problem, but debouncing the switches wouldn't hurt by using a delay like:

      if start = 1 and Down = 1 then
      wait 20 ms
      if start = 1 and Down = 1 then got tiltup ;test condition again
      end if

      Also having a dc motor 'on' for some significant time would be desirable like:

      tiltup:
      set bulb on
      set motor on
      do while up = 0
      wait 1 ms ;some significant delay
      loop
      set motor off
      return

      Seems like this should work, lots of things could be happening on the hardware side tho.  Maybe try attaching V+ to motor, and switch the low side with the mofet/relay? thru GCBasic just to verify the hardware is O.K.

       
    • Nobody/Anonymous

      I have tried removing one of the conditions from the if statements. "if Start = 1 then" and the outputs work.
      If I can't use "and" in an if statement how do I test for two conditions to be true at the same time?

       
    • kent_twt4

      kent_twt4 - 2008-08-13

      Should still work, have you got say a 10k pulldown or pullup resistor for the idle state of each switch?  Also a current limiting 1k series resistor for the inputs would be a good idea.  Having a floating input may cause irregular results.  A similar test as shown below works as you would expect. Without the pulldown/pullup resistor, the button read seemed to return the 'previous' result.

      If Button1 = 0 AND Button2 = 1 Then
      Set Led On
      Do While Button1 = 0 AND Button2 = 1
      wait 1 ms
      Loop
      Else
      Set Led Off
      End if

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.