Shop OBEX P1 Docs P2 Docs Learn Events
Help writing code(Spin) — Parallax Forums

Help writing code(Spin)

Hello all,
How do one write code for a variable say "Pressure" value of which changes continuously fluctuating between 5 and-15.
If the fluctuations remains between remains between 3 and -5 for over 5 seconds, then I would like to some thing, if not do nothing.

Thanks for your help

Siri

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-09-29 03:27
    This should get you started:
    PUB start | timer, interval, maxval, minval, pressure
    
      interval := clkfreq * 5       '5 seconds
      timer := cnt                  'Initialize time.
      repeat
        timer += interval           'Add interval to get next time.
        maxval := $8000_0000        'Smallest number.
        minval := $7fff_ffff        'Biggest number.
        repeat until (cnt - timer => 0) 'Repeat until five seconds are up.
          pressure := read_pressure 'Get the pressure.
          maxval #>= pressure       'Maxval is the maximum pressure read.
          minval <#= pressure       'Minval is the minimum pressure read.
        if (minval => -5 and maxval <= 3) 'do something if max and min are in range.
          do_something
    
    PUB read_pressure
    
    PUB do_something 
    

    Untested, so use at your own risk.

    -Phil
  • I think you meant "if (minval => -5 and maxval =< 3)" for the second to last line of PUB start.
  • I think you meant "if (minval => -5 and maxval =< 3)" for the second to last line of PUB start.
    Right. Thanks for catching that! Fixed.

    -Phil
  • Thanks Phil
    That is very elegant fix,I will use it my machine.
    I gave it shot(amateur programming) please can you see whether it works if not correct me.

    I have attached the code.

    Thank you.
    Siri
  • Phil - I do not think I made the issue very clear - The machine I am making has a air chamber and the pressure in the chamber is monitored about every 1/100 sec.
    When the machine is working properly the pressure readings are 6 to -8
    when it malfunctions the readings are 1 to -2.

    I need to fix this malfunction in about 5 sec. if the malfunction remains > 5 sec.
    So if the pressure readings self corrects within 5sec - no fix is needed and the fix is necessary only if the
    malfunction continue for greater than 5 sec.

    Is the code snippet you suggested will take care of this issue.Will this code work if the malfunction self corrects?

    Thanks for the help.

    Siri
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-09-29 21:29
    Structurally, you might be looking for something like this:
    pub main | check, t
    
      check := 0
    
      t := cnt
      repeat
        waitcnt(t += (100 * MS_001))
        pressure := read_pressue
        if ((pressure => -5) and (pressure =< 3))
          if (++check => 5000/100)
            do_something
        else
          check := 0
    

    If your pressure falls within the range you want to monitor the value in check is incremented; when this value is equal to five seconds of the issue, then do_something is called. If the pressure is not in that range, check is cleared.
  • Thanks Jon & Phil for the help,The issue was solved with your help.
    I also learned something new.

    Thanks again for helping the amateurs.

    Siri
  • lardomlardom Posts: 1,659
    edited 2015-09-30 23:23
    Unless I'm missing something, your code looks like you need to start with the "Propeller Education Kit"
    PUB MalfunctionFix  | x                 ' Fix start set to 10 secs
        
        
     Repeat while Pressure1 == 0  or Pressure1 == -1
        
       Repeat 9                    ' 10 sec  count down
         x := 0
         delay(1)
         x++
         if x => 9
         Repeat 3                     
            outa[21]~~                 'Malfuncfion correction stepps
            outa[22]~
            outa[23]~               
            outa[14]~
            delay(1)
            x := 0
    

    From the very first line your code will stop executing if Pressure1 is not equal to 0 or -1.
    "delay(1)" is not defined anywhere.
    "X" never gets beyond 1 because the first instruction in that repeat loop clears X back to zero.
    "if X => 9" Is a conditional statement which should be followed by an indented line that will only be executed if that condition is true. (You have already said "Repeat 9".)
    There are syntax errors as well as logical errors.
Sign In or Register to comment.