November 23, 2024, 11:02:45 PM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Detecting Direction

Started by bsnut, July 23, 2012, 11:09:49 PM

Previous topic - Next topic

bsnut

July 23, 2012, 11:09:49 PM Last Edit: July 24, 2012, 12:55:00 AM by bsnut
I have been trying, but been having problems trying too get this code too work below. The goal is detect direction using two of TTL inputs and then count up or count down based on the how the inputs are triggered.

pub main | t ,nconstates

  setup_io                                               '' setup HC-8+ IO pins

  term.start(RX1, TX1, %0000, 115_200)                   '' start terminal

'  rs485.start(RX2, TX2, %0000, 115_200)
'  cognew(rtc_timer,@timestack)                           '' start scanning cog
 
  pause(1)                                               '' let all cogs start
 
  term.tx(CLS)
  term.str(string("EFX-TEK HC-8+ BSM Nachod Signals Controller", CR, CR))

  repeat

    waitcnt(t += constant(100 * MS_001))  ' run loop every 100ms
   
   
    if ttl_bit(0) == 1 'and southcount < 1  '%10 Counting in the northbound / tested
      outb[0] := 1            
      repeat while outb[0] == 1
        if ttl_bit(1) == 1                 
          high(OUT3)
          flashsignal(OUT0)
          northcount := northcount + 1
          outb[0] := 0
         

    if ttl_bit(1) and northcount >= 1   'Counting out of northbound / doesn't work
      outb[1] := 1
      repeat while outb[1] == 1
        if ttl_bit(0) == 1
          northcount := northcount - 1
          outb[1] := 0
         


pub flashsignal (pin)

  high(pin)
  pause(750)
  low(pin)
  pause(750)
  high(pin)         


The question...Is there a better way of coding this? The final goal is too do it with four TTL inputs after I get the two to work.
William Stefan
The Basic Stamp Nut

JonnyMac

July 24, 2012, 08:26:24 AM #1 Last Edit: July 24, 2012, 08:32:59 AM by JonnyMac
The waitcnt instruction is really powerful, but has to be used with caution. In your case when you press a button you jump out to flashsignal() which overruns the waitcnt timer; this will cause a delay of almost a minute waiting for the timer to wrap back around to the desired target. In this case, where super precise timing is not required, pause() is better used.

Give this some thought:

  repeat
    pause(100)                                                  ' scan delay
    scan := ttl_inputs(true) & %11                              ' scan inputs
   
    if (scan == %10)                                            ' increment?
      flashsignal(OUT1)                                         ' indicate button
      northcount := ++northcount <# MAX_COUNT                   ' increment and limit

    elseif (scan == %01)
      flashsignal(OUT0)
      northcount := --northcount #> 0



In the event you need to use waitcnt in the future, here's how you would deal with possible overruns. Note that when you call a method that could cause an overrun you need to re-sync the timer.

  t := cnt                                                      ' sync timer
  repeat
    waitcnt(t += constant(100 * MS_001))                        ' scan delay
    scan := ttl_inputs(true) & %11                              ' scan inputs
   
    if (scan == %10)                                            ' increment?
      flashsignal(OUT1)                                         ' indicate button
      t := cnt                                                  ' re-sync timer
      northcount := ++northcount <# MAX_COUNT                   ' increment and limit

    elseif (scan == %01)
      flashsignal(OUT0)
      t := cnt
      northcount := --northcount #> 0
Jon McPhalen
EFX-TEK Hollywood Office

bsnut

Thanks Jon for showing me this, since I got caught in waitcnt instruction trap agian. I should've tried the

pause(100)


Also, thanks for showing me another way of reading the TTL inputs.
William Stefan
The Basic Stamp Nut