Shop OBEX P1 Docs P2 Docs Learn Events
~~~~~~~code help~~~~~~~ — Parallax Forums

~~~~~~~code help~~~~~~~

SpragmaticSpragmatic Posts: 59
edited 2010-03-29 07:48 in General Discussion
· Please see code attached-· A few questions I have.

#1--· I'm wondering if I can stop the timing sequence after PButton2 is pressed by pressing PButton2 again anytime during the sequence(during RCTIME)?· Hope that made sense?· I need to be able to stop the program during the pause for safety reasons if possible.· (This is·the closing of a panel.)···

#2--· At this point I need to press PButton1 to go fully open again while making PButton2·non-functional until completely open again.· (This only allowing the panel to open again·no matter·if you try and·keep pressing PButton2 to close.)·

#3--· Then able to close and back to program start.

· Not quite sure how to make this happen.· Thanks!!·

······························ Greg



Post Edited (Spragmatic) : 3/22/2010 3:07:26 PM GMT

Comments

  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-22 15:14
    Is this possible with DO...LOOP or IF...THEN...ELSE...ENDIF? Do I need to use an Interrupt because I'm breaking the timing sequence of RCTIME? I've tried all types to my knowledge and I can't seem to get it to work. Thanks!!

    Greg
  • ZootZoot Posts: 2,227
    edited 2010-03-22 21:33
    Yes, use an interrupt. Here is *pseudo* code. The important think is that really, all the ISR is doing is keeping time, both for delays in user interface work (buttons) and counting RCtime decays. I used 10us, but resolution is totally up to you. I'm not showing variable declarations or anything.

    INTERRUPT 100_000 ' 10us "ticks"
    
    
    Interrupt_Handler:
        
    Update_RC_Time_But_Only_If_Flag_Set:
    
       IF doRCflag = 1 THEN
          IF rcPin = 1 THEN ' it did not fall to 0 yet (or rise to 1 depending on your circuit)
             INC rcTimer ' best as Word to get a decent amount of possible time
          ENDIF
       ELSE
          INC rcTimer ' to track charge time
       ENDIF
    
    Update_User_Delay_Time_If_Flag_Set:
    
      IF doUserDelay = 1 THEN
         INC userTimer ' best as Word to get a decent amount of possible time
      ENDIF
    
       RETURNINT
    
    
    Main_Program:
    ' the important think here is that the main loop keeps whipping through everything all the time
    ' and sets flags, pin states, clears counters, as needed...
    
    Track_RCtime_Decay:
    
      IF doRCflag = 1 THEN ' are you in the middle of measurement?
         IF rcPin = 0 THEN ' pin finally fell, check and save the reading...
            doRCflag = 0
            rcValue = rcTimer
            HIGH rcPin  ' OUTPUT and high
            rcTimer = 0
         ENDIF
      ELSE  ' otherwise wait 2ms charge time (or whatever you want)
         IF rcTimer > 200 THEN '  2ms/10us
            INPUT rcPin  ' set to measure
            rcTimer = 0 ' clear the count
            doRCflag = 1 ' set the flag
         ENDIF
      ENDIF
    
      ' so at this point, you always have the most recent current reading from the RCTIME circuit in rcValue
    
    Track_User_Button_Stuff:
       IF button0pin = 0 THEN ' active low
          doUserDelay = 1
          IF userTimer > 50_000 THEN ' half second of "holding" button
             ' do something
          ELSEIF userTimer > 5000 THEN ' 50ms hold (so everything is debounced)
            ' do something else
           ENDIF
       ELSE ' released state, clear timer
          userTimer = 0 
          doUserDelay = 0
       ENDIF
    
    
    Do_Something:
      IF rcValue > 10_000 THEN
          ledPin0 = 1
      ELSE
          ledPn1 = 0
      ENDIF
    
    Etc:
    
    Done:
    
       GOTO Main
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 3/22/2010 10:52:47 PM GMT
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-22 22:22
    Thanks Zoot! It's still looks like Greek to me though. I'm having a hard time grasping how this actually works. I haven't worked with Interrupts yet. I guess it's time to learn. smile.gif

    Greg
  • ZootZoot Posts: 2,227
    edited 2010-03-22 22:49
    The code in the interrupt runs every 10 microseconds "in the background". Think of it as a separate program that runs at very precise intervals, over and over. All it does, really, is count "up".

    The "main" program occasionally reads this count to see how much time has passed, and to reset the counter to zero. The rest is keeping track of what's going on, and making some decisions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-23 14:45
    I tried to integrate this Interrupt with my program and I can't seem to get it to work. Please look at code and inspect. I must be missing something. PButton2 does not respond. I'm just trying to get PButton2 to turn on Led2 for now to see how this works. Thanks!!

    Greg
  • ZootZoot Posts: 2,227
    edited 2010-03-23 17:14
    I wouldn't use this part until you have everything else working:

    IF rcValue > 10_000 THEN
    HIGH Led2
    ELSE
    LOW Led2
    ENDIF


    I don't what or how your RC circuit is setup, so besides waiting for debounce on Pb2, the above would also require that the RC time is above 100_000 micro seconds (10ms).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-23 20:50
    From my initial code at top of forum I am using Pot1 to time for between 1-5sec. (Using 10k pot with .01uf cap) Pot2 (Using 10k pot with .01uf cap) times from 1-24sec. On the board I built I have set Pot2 to be maxed at 24sec because I need that time for the panel closing. This is where I get lost as far as the timing needed for the Interrupt. I just need to be able to stop RCTIME anywhere in that 24sec with the press of PButton2. This in turn would force PButton2 to be non functional. Pbutton1 would then have to be pressed for the panel to open again. Once all the way open(the actuator on the panel has a limit switch to stop it at full travel) (so let's say after 20sec) you then could press PButton2 to close again. Hope this made sense? Thanks!! Greg

    I've tried moving things around with your Interrupt code from above and still can't get PButton2 to function. I've ran program in debug and I get confused on what's going on. If I leave it in debug for to long all LED's come on and all relays energize and program locks up? Not sure what is happening there?


    I'm willing at this point to pay someone for their time to correct this problem.· freaked.gif
  • ZootZoot Posts: 2,227
    edited 2010-03-24 15:04
    If you want times that long, you will need to use a much coarser resolution for the interrupt, or get into 32 bit counters (two words). At 10us, a max counter value of $FFFF would only be .655 seconds -- pretty long for an RC circuit but not long enough for your purposes. You may want to try dropping the whole thing with a divider to slow down the RC counter, e.g.

    INC rcDivider
    IF rcDivider >= 100 THEN
    rcDivider = 0
    INC rcCounter
    ENDIF

    That would effecticly scale the count up by 100, so you'd have a max time count of around 65.5 seconds.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-24 17:35
    Thanks Zoot!! I will work with this.
  • SpragmaticSpragmatic Posts: 59
    edited 2010-03-28 22:47
    Just to let everyone know. I FINALLY figured it out! smile.gif I did it without having to use an Interrupt. Just need to finish my button debouncing and I'm good. Thanks!!

    Greg
  • Shawn LoweShawn Lowe Posts: 635
    edited 2010-03-29 07:48
    Can you post your code? I'm curious what your solution was!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    When all else fails.....procrastinate!
Sign In or Register to comment.