Shop OBEX P1 Docs P2 Docs Learn Events
please help with Speed Detector - Page 2 — Parallax Forums

please help with Speed Detector

2

Comments

  • djh82ukdjh82uk Posts: 193
    edited 2007-06-27 13:56
    Ok, have it working on 4 sensors with 4 cogs, but needed an extra cog to handle the tv method (aswell as the object).

    It is not done well but works, now I just need to try codemonkeys way to make it bi-directional, will post up the code later so you can see where I can improve.

    DJH
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2007-06-28 14:38
    I am glad to hear you got it working DJH82UK. In a short amount of time you have come a long ways in understanding the Prop. Lets see some code when you get a moment.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter
    tdswieter.com
    One little spark is all it takes for an idea to explode
  • djh82ukdjh82uk Posts: 193
    edited 2007-06-28 15:18
    Well, it's very very messy, not efficient whatsoever (look at the var block), and ignore any comments for the most part.

    I have learnt a lot on this project and still have a way to go, my next effort is to make it bi-directional, then make the code use more efficient, then add "max" speed which is stored in the free space of the eeprom, then finally see what recources are left to make the display a bit sexier, maybe use the graphics object. What I should probably do next tho is sort out the sensors, i am still using your example to simulate them, I want to use photo emitters and receivers, but I am yet to figure out how to wire them, do I need pull-ups? Pull-downs?.

    If anyone has any comments on getting rid of some of those Var's or to make it bi-directional I would very much appreciate it.

    Thanks for all your help so far.

    heres the code:
    Somebody said...

    CON

    _clkmode = xtal1 + pll16x 'Using the Parallax Demo Board Rev C
    _xinfreq = 5_000_000 'The crystal is 5MHz. and the Propeller will run at 80MHz.

    'Pin Definitions, change the pin numbers to match the desired output pins, on Demo Board Rev C these are LEDs
    Trigger1 = 16 'This pin is the output of Trigger 1, should attach to Sensor1
    Trigger2 = 17 'This pin is the output of Trigger 2, should attach to Sensor2

    distvar = 17004 '(0.1 * 60 *60 * 1000 / (1609 * 76)
    'sensor1 = 1
    'sensor2 = 2
    high = 1
    low = 0

    OBJ

    text : "tv_text"


    VAR

    long one_ms 'Amount of clock cycles to wait for 1ms, calculated based on clock frequency
    long cog 'Value of Cog's ID
    long stack[noparse][[/noparse]15]
    long stack1[noparse][[/noparse]50]
    long stack2[noparse][[/noparse]30]
    long stack3[noparse][[/noparse]30]
    long stack4[noparse][[/noparse]30]
    long pincheckstart
    long starttime
    long endtime
    long finaltime
    long finaltime2
    long finaltime3
    long speed100
    long whole
    long remainder
    long starttime2
    long endtime2
    long finaltime_2
    long finaltime22
    long finaltime32
    long speed1002
    long whole2
    long remainder2
    long starttime3
    long endtime3
    long finaltime_3
    long finaltime23
    long finaltime33
    long speed1003
    long whole3
    long remainder3
    long starttime4
    long endtime4
    long finaltime_4
    long finaltime24
    long finaltime34
    long speed1004
    long whole4
    long remainder4



    PUB Init

    text.start(12)
    'text.str(string(13," Train Speed Detector",13,13))
    'StartSpeedChecker(Trigger1,Trigger2)
    one_ms := clkfreq / 1_000 'Setup one millisecond based on clk frequency
    cognew(SpeedChecker(Trigger1,Trigger2), @stack)
    cognew(tv(0, 0, 1), @stack1)

    cognew(Start2(3, 5, 2), @stack2)
    cognew(Start3(6, 7, 3), @stack3)
    cognew(Start4(8, 9, 4), @stack4)
    Start1(1, 2, 1)



    pri start1 (sensor1, sensor2, sensornum)





    dira[noparse][[/noparse]sensor1] := 0
    dira[noparse][[/noparse]sensor2] := 0


    waitcnt((500 * one_ms) + cnt)
    'repeat
    'wait for the pin on sensor 1 to equal the desired state, read up on waitpeq to get it right, check examples
    waitpeq(0,|< Sensor1,0) 'Assumes Off or 0 is the state when the sensor sees the train
    StartTime := cnt 'Grab the current value of the timer
    waitpeq(0,|< Sensor2,0) 'Assumes Off or 0 is the state when the sensor sees the train
    EndTime := cnt 'Grab the current value of the timer

    'Calculate the speed and the values as needed
    FinalTime := EndTime - StartTime
    Finaltime := Finaltime -1248
    FinalTime3 := (finaltime / 800000)
    FinalTime2 := (distvar/finaltime3)
    Speed100 := finaltime2 * 10
    Whole := Speed100 / 100
    Remainder := Speed100 // 100
    'Where // means Mod, ie the remainder.

    'TV (sensor1, sensor2, sensornum)

    pri start2 (sensor1, sensor2, sensornum)





    dira[noparse][[/noparse]sensor1] := 0
    dira[noparse][[/noparse]sensor2] := 0


    waitcnt((500 * one_ms) + cnt)
    'repeat
    'wait for the pin on sensor 1 to equal the desired state, read up on waitpeq to get it right, check examples
    waitpeq(0,|< Sensor1,0) 'Assumes Off or 0 is the state when the sensor sees the train
    StartTime2 := cnt 'Grab the current value of the timer
    waitpeq(0,|< Sensor2,0) 'Assumes Off or 0 is the state when the sensor sees the train
    EndTime2 := cnt 'Grab the current value of the timer

    'Calculate the speed and the values as needed
    FinalTime_2 := EndTime2 - StartTime2
    Finaltime_2 := Finaltime_2 -1248
    FinalTime32 := (finaltime_2 / 800000)
    FinalTime22 := (distvar/finaltime32)
    Speed1002 := finaltime22 * 10
    Whole2 := Speed1002 / 100
    Remainder2 := Speed1002 // 100
    'Where // means Mod, ie the remainder.

    'TV (sensor1, sensor2, sensornum)


    pri start3 (sensor1, sensor2, sensornum)





    dira[noparse][[/noparse]sensor1] := 0
    dira[noparse][[/noparse]sensor2] := 0


    waitcnt((500 * one_ms) + cnt)
    'repeat
    'wait for the pin on sensor 1 to equal the desired state, read up on waitpeq to get it right, check examples
    waitpeq(0,|< Sensor1,0) 'Assumes Off or 0 is the state when the sensor sees the train
    StartTime3 := cnt 'Grab the current value of the timer
    waitpeq(0,|< Sensor2,0) 'Assumes Off or 0 is the state when the sensor sees the train
    EndTime3 := cnt 'Grab the current value of the timer

    'Calculate the speed and the values as needed
    FinalTime_3 := EndTime3 - StartTime3
    Finaltime_3 := Finaltime_3 -1248
    FinalTime33 := (finaltime_3 / 800000)
    FinalTime23 := (distvar/finaltime33)
    Speed1003 := finaltime23 * 10
    Whole3 := Speed1003 / 100
    Remainder3 := Speed1003 // 100
    'Where // means Mod, ie the remainder.

    'TV (sensor1, sensor2, sensornum)

    pri start4 (sensor1, sensor2, sensornum)





    dira[noparse][[/noparse]sensor1] := 0
    dira[noparse][[/noparse]sensor2] := 0


    waitcnt((500 * one_ms) + cnt)
    'repeat
    'wait for the pin on sensor 1 to equal the desired state, read up on waitpeq to get it right, check examples
    waitpeq(0,|< Sensor1,0) 'Assumes Off or 0 is the state when the sensor sees the train
    StartTime4 := cnt 'Grab the current value of the timer
    waitpeq(0,|< Sensor2,0) 'Assumes Off or 0 is the state when the sensor sees the train
    EndTime4 := cnt 'Grab the current value of the timer

    'Calculate the speed and the values as needed
    FinalTime_4 := EndTime4 - StartTime4
    Finaltime_4 := Finaltime_4 -1248
    FinalTime34 := (finaltime_4 / 800000)
    FinalTime24 := (distvar/finaltime34)
    Speed1004 := finaltime24 * 10
    Whole4 := Speed1004 / 100
    Remainder4 := Speed1004 // 100
    'Where // means Mod, ie the remainder.

    'TV (sensor1, sensor2, sensornum)


    pub TV (sensor1, sensor2, sensornum)




    repeat

    text.str(string(13," Train Speed Detector",13,13))
    'pincheckstart := Ina[noparse][[/noparse]sensor1] 'check to see if sensor has been triggered
    'waitpeq(0,|< sensor1,0)
    text.out(13)
    'text.dec(finaltime)
    text.out(13)
    'text.dec(finaltime_2)
    'text.out(" ")
    'text.dec(finaltime22)
    'text.out(" ")

    waitcnt(clkfreq / 10000 + cnt) 'wait 1M
    text.str(string("Speed Sensor 1 = "))
    text.dec(whole)
    text.out(".")
    text.dec(remainder)
    text.str(string(" MPH"))
    '

    text.str(string(13,"Speed Sensor 2 = "))
    text.dec(whole2)
    text.out(".")
    text.dec(remainder2)
    text.str(string(" MPH"))


    text.str(string(13,"Speed Sensor 3 = "))
    text.dec(whole3)
    text.out(".")
    text.dec(remainder3)
    text.str(string(" MPH"))


    text.str(string(13,"Speed Sensor 4 = "))
    text.dec(whole4)
    text.out(".")
    text.dec(remainder4)
    text.str(string(" MPH"))



    waitcnt((5000 * one_ms) + cnt)
    text.out($00)
    text.str(string(13,"calculating..."))
    waitcnt((1000 * one_ms) + cnt)
    text.out($00)



    PUB StartSpeedChecker(Trig1,Trig2)

    one_ms := clkfreq / 1_000 'Setup one millisecond based on clk frequency
    cognew(SpeedChecker(Trig1,Trig2), @stack)


    PUB SpeedChecker(trig1,trig2)

    dira[noparse][[/noparse]trig1] := 1 'Make trigger 1 an output
    dira[noparse][[/noparse]trig2] := 1

    repeat 'make the cog repeat indefinately
    outa[noparse][[/noparse]trig1] := 1 'Make sure both outputs are off, reset the outputs
    outa[noparse][[/noparse]trig2] := 1

    waitcnt((500 * one_ms) + cnt) 'wait for .5 seconds

    outa[noparse][[/noparse]trig1] := 0 'Turn on the first output

    waitcnt((1000 * one_ms) + cnt) 'Wait for the desired interval

    outa[noparse][[/noparse]trig2] := 0 'Turn on the second output

    waitcnt((500 * one_ms) + cnt) 'Wait for the specified time until the whole thing is to be repeated


  • djh82ukdjh82uk Posts: 193
    edited 2007-07-07 00:49
    Ok

    I have made some new progress, but have also found a new problem. I now have it saving the highest recorded speed to the boot eeprom, and check it against each new speed, and writes the higher value (or leaves it alone).

    This has however shown up a problem, i cannot get the sensing code to repeat, if I put a repeat above the code shown below, it simply does not get a reading, after a bit of debugging it seems that it just ignores the waitpeq and carries on as if triggering, causign the counter to not increase enough to register a speed.

    Any Ideas how I can get this code to always repeat?
    Somebody said...

    repeat
    waitpeq(0,|< Sensor1,0) 'Assumes Off or 0 is the state when the sensor sees the train
    StartTime0 := cnt 'Grab the current value of the timer
    waitpeq(0,|< Sensor2,0) 'Assumes Off or 0 is the state when the sensor sees the train
    EndTime0 := cnt 'Grab the current value of the timer

    'Calculate the speed and the values as needed
    FinalTime := EndTime0 - StartTime0
    Finaltime := Finaltime -1248
    FinalTime3 := (finaltime / 800000)
    FinalTime2 := (distvar/finaltime3)
    Speed100 := finaltime2 * 10
    Whole := Speed100 / 100
    Remainder := Speed100 // 100
    'Where // means Mod, ie the remainder.
  • codemonkeycodemonkey Posts: 38
    edited 2007-07-07 17:08
    djh82uk said...

    If anyone has any comments on getting rid of some of those Var's or to make it bi-directional I would very much appreciate it.
    Somebody said...


    pri start1 (sensor1, sensor2, sensornum)

    dira[noparse][[/noparse]sensor1] := 0
    dira[noparse][[/noparse]sensor2] := 0


    waitcnt((500 * one_ms) + cnt)
    'repeat
    'wait for the pin on sensor 1 to equal the desired state, read up on waitpeq to get it right, check examples
    waitpeq(0,|< Sensor1,0) 'Assumes Off or 0 is the state when the sensor sees the train
    StartTime := cnt 'Grab the current value of the timer
    waitpeq(0,|< Sensor2,0) 'Assumes Off or 0 is the state when the sensor sees the train
    EndTime := cnt 'Grab the current value of the timer

    'Calculate the speed and the values as needed
    FinalTime := EndTime - StartTime
    Finaltime := Finaltime -1248
    FinalTime3 := (finaltime / 800000)
    FinalTime2 := (distvar/finaltime3)
    Speed100 := finaltime2 * 10
    Whole := Speed100 / 100
    Remainder := Speed100 // 100
    'Where // means Mod, ie the remainder.

    'TV (sensor1, sensor2, sensornum)

    Maybe i can offer a bit with the var issue. Unless you need all the intermediate results (FinalTime, FinalTime2, etc) outside the routine you can just have them as local variables. You might have to increase·the stack a bit, if things start not working so well. For example:

    pri start1 (sensor1, sensor2, sensornum) | FinalTime, FinalTime2, FinalTime3
    Of course, you could gang everything together to dispense of those local variables·but that sometimes makes debugging a bit more difficult. To wit:

    FinalTime := EndTime - StartTime
    Finaltime := Finaltime -1248
    FinalTime3 := (finaltime / 800000)
    FinalTime2 := (distvar/finaltime3)
    Speed100 := finaltime2 * 10
    might become:

    Speed100 :=·(distvar / (((EndTime - StartTime) - 1248) / 800000)) * 10

    Assuming that start1, start2, start3, etc are identical, except for the particular set of sensors and "return" values, you could easily make one routine to do all of them, and pass where you want the results to be stored. The general routine could become:

    pri startX (sensor1, sensor2, sensornum, result) | FinalTime, FinalTime2, FinalTime3
    ...waitpeq code here and the answer is returned by...
    Long[noparse][[/noparse]result] := (distvar / (((EndTime - StartTime) - 1248) / 800000)) * 10
    ...saving the whole and remainder calculations for your output routines. Or if you want, you could do them here and return them instead of "result"

    called by:

    StartX(1, 2, 1, @Speed100)

    I'm assuming, of course, that all your indents are proper.
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-07 22:47
    Thanks codemonkey, will give it a go as soon as I can figure out why the waitpeq's won't repeat [noparse]:([/noparse]

    DJH
  • codemonkeycodemonkey Posts: 38
    edited 2007-07-09 00:37
    Uh, i just looked a bit more carefully. The repeat is commented out--that would certainly put somewhat of a damper on the waitpeqs repeating. Also, you probably already know to watch your beginning-of-line indent spacing.
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-09 19:03
    yeh, it is only commented out so I could test it, sorry should have said,

    everything under the repeat was included (indented 2 spaces)

    After a bit of debugging, is seems like it just does not bother waiting for an input, it just skips the waitpeq completley, If i put a delay between the two then a speed registers on the screen, in accordance to the delay.


    DJH
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-12 13:50
    is there anyway to append too numbers (variables) into one number

    for instance

    var1 = 64
    var2 = 5

    So I want Var3 to equal 645.

    Any operators that can do this?

    DJH
  • deSilvadeSilva Posts: 2,967
    edited 2007-07-12 14:28
    You have to strictly distinguish between numbers and strings - this is not TK or Perl smile.gif
    So there are two options:
    1. Covert both numbers to strings and copy one after the other with MOVEBYTES (or maybe you can even give a destination address just behind the first string when converting the second number)
    2. you can use simple math: var3 := 10 * var1 + var2

    When you are not shure of the "size" of var2 (which of course determines the factor, here assumed as "10"), run this simple loop:
    factor := 10
    REPEAT WHILE var2 > factor 
      factor *= 10 
    
    



    Typo edited..

    Post Edited (deSilva) : 7/14/2007 7:20:32 PM GMT
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-14 01:42
    well the only reason I was asking is because I just cannot get it to store a number larger than 255 in the eeprom using the basicI2C Driver Object..

    I am using the PageWrite and ReadWrite functions, as the WriteLong and WriteWord seem to mess the number up, say it tries to write 122 it actually writes 156.

    Rather confusing, make Mike or anyone else could advise?

    DJH
  • JonathanJonathan Posts: 1,023
    edited 2007-07-14 17:46
    DJH,

    I am using the BasicI2c driver to store words and longs and it works fine. I don't see any code above that uses the driver to store variables. Maybe it is just something in your code. Here is what works for me:

    i2c.initialize(eeScl)
    i2c.start(eeScl)
    i2c.writeword(eeScl,eeAdr,eeBase+Nav_delay,navfactor)

    HTH,

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-14 18:36
    yes sorry I added it after the above code was pasted

    What are those last 2 variables you are using?

    DJH
  • JonathanJonathan Posts: 1,023
    edited 2007-07-14 19:36
    eebase is a con with a value of $8000, because I am using a 512kbit eeprom. Nav_delay is the offset to the desired location above eebase. Navfactor is the name of the variable that I am storing.

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-19 15:13
    ok still struggling on my part

    the Method in the basic i2c driver for reading a long is this:

    PUB ReadLong(SCL, devSel, addrReg) : data

    I know what all the variables are within the brackets, but how do I access the data? I am presuming this is some sort of "result", so would I need something like

    readeeprom := readlong etc etc?

    How should I format it?

    The same goes for the WriteLong Method

    My problem is that I just cannot get it to store/read a number over 255

    I have tried read and write long but when I do it I get strange numbers that do not match any of the variables.

    DJH
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-19 15:24
    This does not work at all, comes back with 0

    temp := i2c.ReadLong(i2c#BootPin, i2c#EEPROM, eepromAddress)


    but perhaps it is the write long causing the problem I don't know how to tell


    if i2c.WriteLong(i2c#BootPin, i2c#EEPROM, eepromAddress, @buffer)
    abort ' an error occured during the write
    startTime := cnt ' prepare to check for a timeout
    repeat while i2c.WriteWait(i2c#BootPin, i2c#EEPROM, eepromAddress)
    if cnt - startTime > clkfreq / 10
    abort ' waited more than a 1/10 second for the write to finish

    indents are as they should be
  • JonathanJonathan Posts: 1,023
    edited 2007-07-20 00:29
    Sorry I dropped out on this thread. Here is what works for me:

    navWait := i2c.readlong(eeScl,eeAdr,eeBase+nav_delay)

    What happens if you read areas of your code? Does it match what you see in the editor? I can't see any reason why your code isn't working, are you sure you are using a valid eeprom address?

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • JonathanJonathan Posts: 1,023
    edited 2007-07-20 02:16
    Something just occured to me, is it possible that it don't like the @ modifier in the write?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-20 13:15
    Cannot see why it is not working

    What eeprom addresses would you say I should use for 2 longs?

    I have one as $7000 and one as $6000

    My code is quite small
  • JonathanJonathan Posts: 1,023
    edited 2007-07-20 18:15
    I found I had to use an address above $7FFFF. Try using $8000 as your starting address for storage. Each long is 4 bytes, so your second long should be stored at $8004.

    HTH,

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-23 13:35
    hey your right, it was the @ before the buffer in the code to write the long,

    working now [noparse]:)[/noparse]

    Thanks a lot
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-23 13:40
    Another contributing factor was that I declared the buffer2 variable as:

    Byte Buffer2[noparse][[/noparse]32]

    changed it to:

    Long Buffer2

    before it would work.

    Just got to tidy up the code and make the display look pretty now [noparse]:)[/noparse]

    Can you use TV text aswell as the graphics objects? Or how easy is it to do text with the graphics object?

    DJH
  • JonathanJonathan Posts: 1,023
    edited 2007-07-23 14:13
    DJH,

    Great! Glad it is finally working. As the the TV text and graphics, I have no idea, never used these.

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-23 14:31
    The graphics driver does have methods for adding text to a graphics screen, but it's really intended for labeling, not for a scrolled text field. The text is effectively painted on the graphics bitmap. Have a look at the comments for the "textXXX" methods in the source code. The TV_text driver uses the same bitmapped TV I/O driver as the graphics driver and it's possible to modify the TV_text driver to allow embedded graphics windows combining the two, but I'm not aware of any easy examples.
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-30 23:54
    ok, im still struggling a bit on the bi-directional part, how can I figure out which pin has been activated, and only get it to check the other pin for the 2nd trigger, for instance if pin 2 is triggered, i want it to start counting and only be checking pin 1 to be triggered and then stop counting, and vice versa

    DJH
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-31 00:19
    what format would newwatchpinstate be in?

    hex, Bin or Dec?

    I guess this is aimed at codemonkey, but help from anyone would be great

    thanks

    DJH
  • djh82ukdjh82uk Posts: 193
    edited 2007-07-31 00:28
    hmm, ok it seems to be binary, but only displays on the screen (newwatchpinstate) if I use text.bin with the number of digits set to 22

    I then get either 100000000000000000000

    or


    0010000000000000000000

    or 1010000000000000000000 if neither sensors are triggered, i have been testing it with the below code, how can i get the program to tell which pin has been activated?
    Somebody said...
    Pri start1(Sensor1, Sensor2) | WatchPin1Count, WatchPin2Count, WatchPin1Mask, WatchPin2Mask, WatchPinsMask, WatchPin1State, WatchPin2State, WatchPinsState, Button1pin, Button2pin, buttondebouncecount

    Button1pin := Sensor1
    Button2pin := Sensor2

    WatchPin1Mask := |<Button1Pin
    WatchPin2Mask := |<Button2Pin

    WatchPinsMask := WatchPin1Mask | WatchPin2Mask
    dira[noparse][[/noparse]Button1Pin]~
    dira[noparse][[/noparse]Button2Pin]~


    'dira[noparse][[/noparse]LedPin]~~

    WatchPin1Count := -1
    WatchPin2Count := -1
    'Remember what's up right now
    WatchPinsState := InA & WatchPinsMask

    Repeat


    waitpne(WatchPinsState, WatchPinsMask, 0) 'Wait any button
    NewWatchPinsState := InA & WatchPinsMask
    'text.str(string("NewWatchPinsState = "))
    'text.dec(newwatchpinsstate)
    waitcnt((5000 * one_ms) + cnt)

    WatchPinsState := NewWatchPinsState
  • djh82ukdjh82uk Posts: 193
    edited 2007-08-04 21:05
    anyone?
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-08-05 03:30
    djh,
    I think we've lost the context of your question. Try posting a question with your spin file(s) attached.

    Fred
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-05 06:30
    Fred Hawkins said...
    djh, I think we've lost the context of your question.
    That's exactly what I thought smile.gif

    @DJH: What I understand is, that you are working with two independent input streams and get confused about the states they are in. As your code consists of basic initialization only, there is no hint to anything at all....

    In general one uses a "state machine" for problems of that kind: You have four STATES:
    0: Both switches off
    1: First on, second off
    2: First off, second on
    3: both on

    The numbering in this situation reflects the pin polarity but this is just good luck that can be utilized. Generally "states" are more complex..

    When the pins change, you simply update the STATE and act accordingly:
        0   1   2   3    <- new state
     0  .   ?   ?   ?   
     1  ?   .   ?   ?  
     2  ?   ?   .   ?
     3  ?   ?   ?   .
     |
    old state
    
    



    The ? mark YOUR actions for this situation

    You can keep the STATE in a variable, or you can have different sections in your program. The latter technique is mainly used when you have JUMP or GOTO instructions, which SPN has not.


    A minor problem is that there is no WAIT instruction for an arbitrary pins change in the Propeller; thus you have to "poll" the pins using a small (!) WAITCNT inbetween - you can consider this as a kind of "low pass filter"

    EDIT: Tried to make it more explicite

    Post Edited (deSilva) : 8/5/2007 8:41:53 AM GMT
Sign In or Register to comment.