Shop OBEX P1 Docs P2 Docs Learn Events
could use some help with buttons, cycles and starting/stopping — Parallax Forums

could use some help with buttons, cycles and starting/stopping

LBwaterCoLBwaterCo Posts: 6
edited 2012-06-18 15:58 in BASIC Stamp
Hello,

I've been a lurker here for a week or so now, good info here.
I recently got assigned a project at work and the boss handed me a BS2.
I haven't had much experience with the Basic Stamp , but I think I got the hardware part figured out, and some coding (for testing my buttons)


So,
what I am trying to do is set up a timer / cycle changer for our water tanks.
I have been reading and I think I may need a clock chip to get my timings correct, but for now I suppose pauses would work.

I have been checking the forums and manuals but without much success.
Seems I start adding code and things just stop or I find another command that may do something my convoluted coding was trying to accomplish.
I am not a programmer, I'm a PC tech. Gimme a screwdriver and I can turn it like no other. :)

What I would like the program to do is, (start/stop/select cycles and ring a bell when finished)
I have 2 buttons and the parallax LCD to work with.

From startup:
press start button (IN0) run cycle 1 (runs a timer) or select a cycle with IN1(advance +1)
when timer is complete It will hit a relay for a bell (for now I'm using the LED from the kit on IN15)
to shut off the LED press the start/stop button (IN0)
then to advance the cycle +1 press (IN1) (there will be 7 cycles)
start current cycle use Start/stop button (IN0)
and repeat for all 7 cycles

Not sure if this outline of operations would be the way to go, so any suggestions would be appreciated.
I will continue to read the manuals and hopefully find something.

Here is what I have for my buttons

' {$STAMP BS2}
' {$PBASIC 2.5}

PRESS VAR Bit
STATE VAR Bit
SEROUT 3, 84, [12]
PAUSE 500
SEROUT 3, 84, 18, [ "Regen Timer" , CR, "SELECT A CYCLE" ]

DO
IF IN0 > 0 THEN STATE = 1 ELSE STATE = 0
IF STATE = 1 THEN HIGH 15
IF STATE = 0 THEN LOW 15
IF IN1 > 0 THEN PRESS = 1 ELSE PRESS = 0
IF PRESS = 1 THEN HIGH 14
IF PRESS = 0 THEN LOW 14
LOOP


Thank you for any help.

Comments

  • ercoerco Posts: 20,257
    edited 2012-05-30 10:36
    Got relays already? Order one of these relay boards first, then we can talk about your code: http://www.ebay.com/itm/160809461854
  • LBwaterCoLBwaterCo Posts: 6
    edited 2012-05-30 10:40
    erco wrote: »
    Got relays already? Order one of these relay boards first, then we can talk about your code: http://www.ebay.com/itm/160809461854

    I do have one relay already NTE R22-1D16-5/6 to control a bell we have.
    That board does look nice but the 4 channels may not be needed.

    Thanks for the fast reply.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-30 12:08
    Part of your problem is that switches are not ideal ... their contacts bounce for a while after being "closed" and the Stamp is fast enough to follow the bounces. There's a variety of ways to handle this, but the easiest with the Stamps is to use the built-in BUTTON statement. Here's an example with a Start/Stop button and a Go To Next Phase button. Each button requires a byte work area:
    workOnOff   var   byte
    workPhase   var   byte
    onOff   var   bit
    oldPhase   var   nibble
    newPhase   var   nibble
    
    mainLoop:
         button 0, 1, 255, 0, workOnOff, 0, offState
         if onOff = 0 then gosub turningOn   ' called when button first pushed
         onOff = 1
         goto checkPhase
    offState:
         if onOff = 1 then gosub turningOff   ' called when button first released
         onOff = 0
    checkPhase:
         button 1, 1, 255, 0, workPhase, 0, updatePhase
         if oldPhase = newPhase then
              newPhase = (newPhase + 1) // 8   ' phase goes from 0 to 1 to ... to 7 to 0
              gosub changedPhase   ' called when phase changes due to button push
         endif
         goto mainLoop
    updatePhase:
         oldPhase = newPhase   ' button is released, get ready for next push
         goto mainLoop
    
  • LBwaterCoLBwaterCo Posts: 6
    edited 2012-05-30 14:06
    Thank you Mike,
    I wasn't sure about the button command, I was having troubles earlier with the 'bounces'. Thought I had it taken care of, but Button should work better.
    Now I'm trying to wrap my head around your coding. I try to load it, but it says it is missing some labels
    turningOn
    turningOff
    changedPhase

    I tried to make a new subroutine for the missing labels witout success. They end up stopping the program or "overloading"? the BS2 (on board LED goes out)

    Hate to have you guys do my work for me, I'm just stuck on this.

    Thanks again for looking.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-30 14:21
    You've got to learn the basics of programming. The "What's a Microcontroller?" tutorial is a good introduction to PBasic programming. The "Basic Stamp Syntax and Reference Manual" discusses each PBasic statement and gives examples of its use.

    The labels are flagged as undefined because they are. You have to supply subroutines with those labels and, like all subroutines, they have to return to the point of call using a RETURN statement. turningON gets called when the on/off button is first pushed. turningOff gets called when the on/off button is first released. changedPhase gets called each time the other button is pushed and the newPhase variable will be incremented each time with a roll-over after 7. The first time changedPhase is called, newPhase will be 1. The second time, it will be 2. The 7th time, it will be 7. The 8th time, it will be 0 then it will repeat the cycle. I'm assuming that zero is a "do nothing" phase and 1-7 are active phases that do something.
  • LBwaterCoLBwaterCo Posts: 6
    edited 2012-05-30 15:34
    Thanks Mike,
    I was hoping to have all my ducks in a row when asking about this. I've seen your previous posts ;)

    Anyways, I made some progress with the sample you gave me. I added the missing subroutines and was able to get the majority of the project working.
    There are a couple problems, but with some fiddling ( and some help from our guys here) , I should be able to figure it out.
    One thing I think I need more help on are adding the delays or pauses during the 7 cycles. What I'm trying to do is after I select a cycle with Button 1 "newPhase" I would like it to link to a timer after I click Button0.
    So basically the 7 cycles would all have differently timed pauses, all started and stopped from Button 0.
    What I am not finding is a way to select these variables, (need to study more) and a way to display them on a LCD

    Hope Im not being too much of a pain. I promise I will read more 'Basic Stamp Syntax and Reference Manual' after work. haha

    Thank You for all your help
  • Mike GreenMike Green Posts: 23,101
    edited 2012-05-30 17:55
    With these variable length pauses and the need to keep interacting with the pushbuttons even during the various phases, you are going to need to add a routine in your main loop that handles the "states" that occur after each of the pauses. You'd have a routine that counts down a time counter maybe once every 100ms. When the timer expires, the code goes to a subroutine based on newPhase, onOff, and another variable that keeps track of which time delay is in effect. The subroutine can do something to the outside world, can set a new time delay, or move on to the next phase. The delay routine would come after mainLoop: and might look like:
    if delay > 0 then
       pause 100
       delay = delay - 1
       if delay = 0 then gosub delayOver
    endif
    
    The delayOver routine would consist of a series of if statements, one for each combination of phase and timeSlot like this:
    if newPhase = 1 and timeSlot = 0 then
       delay = 1000 ' wait for 100 seconds before doing 2nd action
       timeSlot = timeSlot + 1 ' initialized originally to zero and zeroed when newPhase changed
       ' do something with the outside world
    elseif newPhase = 1 and timeSlot = 1 then
       ' this picks up after 100 seconds
       ' maybe there's no further delays in phase 1
       timeSlot = 0
       newPhase = newPhase + 1   ' automatically go on to phase 2
    elseif
       ' ...
    endif
    return   ' back to main loop
    
    This is what is known as a "state machine". Look up the description in the Wikipedia. You may need some changes to the other routines as well.
  • LBwaterCoLBwaterCo Posts: 6
    edited 2012-06-04 08:03
    Thanks for the reply,

    I was distracted by my PC duties last week and didn't have much time to work on this.

    I did some studying over the weekend and hopefully I can piece this together.
  • LBwaterCoLBwaterCo Posts: 6
    edited 2012-06-18 15:58
    well, the unit is now in use. Seems to be doing ok.

    it has 2 buttons, a warning bell, 8 timed cycles and a nice LCD readout.

    timing is a little off, maybe 4 seconds slow for 10 minutes , but that's acceptable since there isn't a timing chip.

    Not sure if you want a copy of the program to look at or whatever (let me know)

    But thanks again for the help (and reading suggestions)
Sign In or Register to comment.