Shop OBEX P1 Docs P2 Docs Learn Events
Spin Help Needed — Parallax Forums

Spin Help Needed

NWCCTVNWCCTV Posts: 3,629
edited 2013-04-23 20:03 in Propeller 1
I will start by saying I am pretty much a newbie when it comes to Spin. I am getting confused because I am use to coding in VB, VC, VC++ and PBasic. I am really getting confused as to how Spin works. I have entered the Micro Medic contest and I do not really have a whole lot of time to teach myself Spin but I am going to try. What I need help with is the attached Demo code for the Pressure Sensor that comes in the Micro Medic kit. This item is a perfect match for a portion of my project but I just need to modify the code. What I need is when pressure is >= xxx then start the Servo on Pin 16. The Servo I am using is a Parallax continuous rotaion servo. The Servo will continue to turn until pressure is no longer >= xxx. Any help is appreciated.
{{
Pressure Sensor and BarGraph DEMO.spin
Program Date: 1/4/2013
Version:      1.0
Description:
This program reads voltage values from the Propeller BOE's ADC and prints the raw value to
the Parallax Serial Terminal program running on your PC.  The program also uses a cog to
show a linear representation of the pressure sensed on a 10-segment LED bar graph.
 
                           Propeller BOE Wiring Diagram

=== Pressure Sensor Pin Connections ===   
                                                   
                         +5v                       
                                                  
                          │                        
          ┌─────────────┐ │                        
          │             │ │                        
          │   MPX5010   │ │                         
          │             │ │                         
          └─┬─┬─┬─┬─┬─┬─┘ │                         
      Pin 1 │ │ │ x x x   │                        
    AD0 ───┘ │ └─────────┘                        
                                                  
             GND
 
=== LED Bar Graph Pin Connections ===
                                     
     P0 ──────┐                 
     P1 ──────┫                 
     P2 ──────┫                 
     P3 ──────┫                 
     P4 ──────┫                 
     P5 ──────┫                 
     P6 ──────┫                 
     P7 ──────┫                             
     P8 ──────┫                                                                    
     P9 ──────┫                 
          220Ω     │                 
                   │                 
                     GND             
 
}}
CON
  'let the compiler know which crystal and PLL settings to use
  _xinfreq = 5_000_000
  _clkmode = xtal1 + pll16x
  INPUT         = false         'bit pattern is all 0s
  OUTPUT        = true          'bit pattern is all 1s
  PC_BAUD       = 115_200       'PC serial terminal's baud rate
  
  ADC_CH0       = 1 << 0
  ADC_CH1       = 1 << 1
  ADC_CH2       = 1 << 2
  ADC_CH3       = 1 << 3
  ADC_ALL       = ADC_CH0 + ADC_CH1 + ADC_CH2 + ADC_CH3
  'LED Bar graph pin definitions
  BAR_GRAPH_0   = 0
  BAR_GRAPH_1   = 1
  BAR_GRAPH_2   = 2
  BAR_GRAPH_3   = 3
  BAR_GRAPH_4   = 4
  BAR_GRAPH_5   = 5
  BAR_GRAPH_6   = 6
  BAR_GRAPH_7   = 7
  BAR_GRAPH_8   = 8
  BAR_GRAPH_9   = 9
OBJ
  adc   :       "PropBOE ADC"                           'Requires 1 cog
  pc    :       "Parallax Serial Terminal"              'Requires 1 cog
VAR
  'Globally accessible variables, shared by all cogs
  long pressure
  long cogStack[20]
PUB Go | rawReading
  'Start other objects
  pc.Start(PC_BAUD)
  adc.Start(ADC_CH0)
  'Launch additional cogs
  cognew(RunBarGraph, @cogStack)
  repeat
    rawReading := adc.In(0)                             'Get the reading from channel 0 on the ADC.
    pressure := rawReading / 25                         'Scale the raw reading to be displayed on the LED bar graph.
                                                        'Note that this scaled reading does not correspond with a particular
                                                        'unit of pressure such as mmH2O, mmHg, kPa, or PSI.  Check the sensor's
                                                        'datasheet (MPX5010DP) for the mV/mmH2O conversion value if you want an
                                                        'absolute reading in a particular unit of pressure.
                                                        '
                                                        'The global variable "pressure" is shared between this cog and the cog
                                                        'that is controlling the LED bar graph.
    '===== Print to the PC serial terminal =====
    pc.Home
    pc.Str(string("=== Pressure Sensor Test ==="))
    pc.NewLine
    pc.NewLine
    pc.Str(string("Raw ADC Value: "))
    pc.dec(rawReading)
    pc.Chars(" ", 5)
    pc.NewLine
    waitcnt(cnt + clkfreq/20)                           'wait for ~1/20th seconds so as not to flood the computer's serial port with data.
 
PUB RunBarGraph | i
  dira[BAR_GRAPH_9..BAR_GRAPH_0] := OUTPUT              'set range of pins to output
                                                        '(this works in this case because the pins are consecutive)
  
  repeat
    outa[BAR_GRAPH_9..BAR_GRAPH_0] := 1<<pressure - 1   'Continually set the value of the scaled pressure to the LED bar graph pins.
                                                        'Do a little bitwise manipulation to make the LEDs look nice.

DAT
{{

Comments

  • garyggaryg Posts: 420
    edited 2013-03-30 19:31
    I'm attempting to learn this very thing also.
    Do you have an active circuit that you are attempting to use the pressure controller with?
    It would appear to me, that a some point while running the objects called
    adc : "PropBOE ADC" 'Requires 1 cog
    pc : "Parallax Serial Terminal" 'Requires 1 cog
    That a variable called

    long pressure

    will be available.

    This is all still very confusing to me, but Look for something called long pressure and try using that in your main object.

    Sorry if I caused you more confusion,
    I'm truly hoping that someone else chimes in here.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-03-30 19:48
    Yes, I am using the Demo as posted in the code. I have the code for the Servo inserted in it also. It's a simple servo.Set(14, 200) line.When I run the program I can get it to work but I can not get it to work with an "IF" statement. That is how far I have gotten since I posted this. I am going to keep plugging away at it until I get it resolved or someone else does chime in. I will try your suggestion to see what happens. Thus far I have tried "If Pressure > 1 servo.Set(14, 200) but that does not work. Getting back to it now.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-03-30 20:42
    Getting closer. It has to do with the "rawReading function. Now I just need to figure out where to place it. I have LED on Pin 12 for testing purposes. When I place "IF rawReading >200 dira [12] outa [12] " the LED does not light up until after I blow on my tube connected to the pressure sensor and the output goes above 200.
  • lardomlardom Posts: 1,659
    edited 2013-03-30 21:38
    dira[BAR_GRAPH_9..BAR_GRAPH_0] := OUTPUT
    
    The way you've written it may or may not work but I've never seen it written that way.
    dira[BAR_GRAPH_9..BAR_GRAPH_0] := 1
    
    This will definitely set the pins to output. I like the double tilde "~~" which is the same as ":= 1"
  • kuronekokuroneko Posts: 3,623
    edited 2013-03-30 21:44
    lardom wrote: »
    I like the double tilde "~~" which is the same as ":= 1"
    Not quite. It's only the same when you affect a single pin, for a pin group you want -1 instead (IOW TRUE).
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-03-30 22:11
    The way you've written it may or may not work but I've never seen it written that way.
    No, I'm not modifying that code at all. I am just adding my own to get an led running. Then I will work on getting the Servo going. I currently have it so if the pressure is above 200 the LED lights up and when it goes below 200 it goes off. Now I just need to make that work for the Servo. I had tried to create a new cog for this but it was not working at all. I just added my code under the last waitcnt line. I just can't believe how touchy Spin is to indentation.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-03-30 22:42
    At this point I just need to know how to get the Servo to go with the IF statement. I have the LED working but trying to figure the Servo out is a little more difficult. I am stuck here:
    IF rawReading >= 200 servo.Set(14, 200)
    
    It makes one quick movement and then the program stops responding.
  • xanaduxanadu Posts: 3,347
    edited 2013-03-30 22:47
    Can you post your code with the simple servo lines you added?

    Also you might want to try this object for servo control - http://obex.parallax.com/objects/445/
  • kuronekokuroneko Posts: 3,623
    edited 2013-03-30 22:53
    NWCCTV wrote: »
    IF rawReading >= 200
       servo.Set(14, 200)
    
    It makes one quick movement and then the program stops responding.
    In SPIN you should use => if you mean greater than or equal.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-03-30 23:05
    if rawReading => 150
    servo.Set(14, -200)
            'time.Pause(5000)   
            dira[11] := 1 
            outa[11] := 1
    elseif rawReading << 149
            dira[11] := 0
            outa[11] := 0
    
    In SPIN you should use => if you mean greater than or equal
    I just posted it backwards.
    Also you might want to try this object for servo control - http://obex.parallax.com/objects/445/
    I already have a Servo Object that is used in the 32 Servo configuration. I have been commenting out the LED function for Pin 11. When I comment out the servo.Set command the LED lights when the pressure goes above 150 and turns off below 149 and it works as it is suppose to. (Also, I know, I have no comments for what everything does. I usually go and do that after I get the code to work.)
  • xanaduxanadu Posts: 3,347
    edited 2013-03-30 23:12
    garyg wrote: »
    Do you have an active circuit that you are attempting to use the pressure controller with?

    The Propeller BOE has a built in ADC. If you don't have a BOE you'll need to add an ADC. That's the MPX5010 in the notes section of the code.
  • xanaduxanadu Posts: 3,347
    edited 2013-03-30 23:17
    NWCCTV wrote: »
    if rawReading => 150
    servo.Set(14, -200)
            'time.Pause(5000)   
            dira[11] := 1 
            outa[11] := 1
    elseif rawReading << 149
            dira[11] := 0
            outa[11] := 0
    
    I just posted it backwards. I already have a Servo Object that is used in the 32 Servo configuration. I have been commenting out the LED function for Pin 11. When I comment out the servo.Set command the LED lights when the pressure goes above 150 and turns off below 149 and it works as it is suppose to. (Also, I know, I have no comments for what everything does. I usually go and do that after I get the code to work.)

    For the LED "dira[11] := 1" should be in your init section toward the top, then toggle it with "outa[11] : = 1" and "outa[11] : = 0" (just some housekeeping)

    I haven't used the servo object you're using, but have you gotten it to work all by itself, minus the demo code for the pressure sensor?
  • xanaduxanadu Posts: 3,347
    edited 2013-03-30 23:31
    This is what it would look like if you used the servo object I referenced -
    {{
    Pressure Sensor and BarGraph DEMO.spin
    Program Date: 1/4/2013
    Version:      1.0
    Description:
    This program reads voltage values from the Propeller BOE's ADC and prints the raw value to
    the Parallax Serial Terminal program running on your PC.  The program also uses a cog to
    show a linear representation of the pressure sensed on a 10-segment LED bar graph.
     
                               Propeller BOE Wiring Diagram
    
    === Pressure Sensor Pin Connections ===   
                                                       
                             +5v                       
                              &#61463;                        
                              &#9474;                        
              &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488; &#9474;                        
              &#9474;             &#9474; &#9474;                        
              &#9474;   MPX5010   &#9474; &#9474;                         
              &#9474;             &#9474; &#9474;                         
              &#9492;&#9472;&#9516;&#9472;&#9516;&#9472;&#9516;&#9472;&#9516;&#9472;&#9516;&#9472;&#9516;&#9472;&#9496; &#9474;                         
          Pin 1 &#9474; &#9474; &#9474; x x x   &#9474;                        
        AD0 &#61615;&#9472;&#9472;&#9472;&#9496; &#9474; &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;                        
                  &#61464;                                    
                 GND
     
    === LED Bar Graph Pin Connections ===
                                         
         P0 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9488;                 
         P1 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                 
         P2 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                 
         P3 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                 
         P4 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                 
         P5 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                 
         P6 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                 
         P7 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                             
         P8 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                                                                    
         P9 &#61615;&#9472;&#9472;&#61629;&#61630;&#9472;&#9472;&#61606;&#61608;&#9472;&#9472;&#9515;                 
              220&#937;     &#9474;                 
                       &#9474;                 
                       &#61464;  GND             
     
    }}
    CON
      'let the compiler know which crystal and PLL settings to use
      _xinfreq = 5_000_000
      _clkmode = xtal1 + pll16x
      INPUT         = false         'bit pattern is all 0s
      OUTPUT        = true          'bit pattern is all 1s
      PC_BAUD       = 115_200       'PC serial terminal's baud rate
      
      ADC_CH0       = 1 << 0
      ADC_CH1       = 1 << 1
      ADC_CH2       = 1 << 2
      ADC_CH3       = 1 << 3
      ADC_ALL       = ADC_CH0 + ADC_CH1 + ADC_CH2 + ADC_CH3
      'LED Bar graph pin definitions
      BAR_GRAPH_0   = 0
      BAR_GRAPH_1   = 1
      BAR_GRAPH_2   = 2
      BAR_GRAPH_3   = 3
      BAR_GRAPH_4   = 4
      BAR_GRAPH_5   = 5
      BAR_GRAPH_6   = 6
      BAR_GRAPH_7   = 7
      BAR_GRAPH_8   = 8
      BAR_GRAPH_9   = 9
    OBJ
      adc   :       "PropBOE ADC"                           'Requires 1 cog
      pc    :       "Parallax Serial Terminal"              'Requires 1 cog
      servo :       "jm_servo_8x"                          
    VAR
      'Globally accessible variables, shared by all cogs
      long pressure
      long cogStack[20]
    PUB Go | rawReading
      'Start other objects
      servo.start(1, 14)                                    
      pc.Start(PC_BAUD)
      adc.Start(ADC_CH0)
      'Launch additional cogs
      cognew(RunBarGraph, @cogStack)
      repeat
        rawReading := adc.In(0)                             'Get the reading from channel 0 on the ADC.
        pressure := rawReading / 25                         'Scale the raw reading to be displayed on the LED bar graph.
                                                            'Note that this scaled reading does not correspond with a particular
                                                            'unit of pressure such as mmH2O, mmHg, kPa, or PSI.  Check the sensor's
                                                            'datasheet (MPX5010DP) for the mV/mmH2O conversion value if you want an
                                                            'absolute reading in a particular unit of pressure.
                                                            '
                                                            'The global variable "pressure" is shared between this cog and the cog
                                                            'that is controlling the LED bar graph.
    
       if rawReading => 150
         servo.set(14, 200, 2) 
           
         elseif rawReading < 149
           servo.halt(14)
    
                                                            
        '===== Print to the PC serial terminal =====
        pc.Home
        pc.Str(string("=== Pressure Sensor Test ==="))
        pc.NewLine
        pc.NewLine
        pc.Str(string("Raw ADC Value: "))
        pc.dec(rawReading)
        pc.Chars(" ", 5)
        pc.NewLine
        waitcnt(cnt + clkfreq/20)                           'wait for ~1/20th seconds so as not to flood the computer's serial port with data.
     
    PUB RunBarGraph | i
      dira[BAR_GRAPH_9..BAR_GRAPH_0] := OUTPUT              'set range of pins to output
                                                            '(this works in this case because the pins are consecutive)
      
      repeat
        outa[BAR_GRAPH_9..BAR_GRAPH_0] := 1<<pressure - 1   'Continually set the value of the scaled pressure to the LED bar graph pins.
                                                            'Do a little bitwise manipulation to make the LEDs look nice.
    
    DAT
    {{
    &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
    &#9474;                                                   TERMS OF USE: MIT License                                                  &#9474;
    &#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
    &#9474;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    &#9474;
    &#9474;files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    &#9474;
    &#9474;modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software&#9474;
    &#9474;is furnished to do so, subject to the following conditions:                                                                   &#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.&#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          &#9474;
    &#9474;WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         &#9474;
    &#9474;COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   &#9474;
    &#9474;ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         &#9474;
    &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
    }}
    



    Actually you might want to put a small wait after initializing the servo object
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-04-01 19:56
    @xanadu, thank you for this. I will give it a try in the next day or two when my normal job calms down.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-04-01 20:21
    servo.start(1, 14)
    
    @xanadu, I am getting an "expected a subroutine name" error message on this line of code.
  • xanaduxanadu Posts: 3,347
    edited 2013-04-23 19:50
    Did you get it working?

    The error probably means the correct object isn't specified but I'm not 100% on that.
    OBJ   adc   :       "jm_servo_8x"     
    



    Also this:
    if rawReading => 150      
      servo.set(14, 200, 2)               
    elseif rawReading < 149        
      servo.halt(14)
    

    Should be:
    if rawReading => 150      
     servo.set(14, 200, 2)
    elseif rawReading < 150
     servo.halt(14) 
    

    oops!
  • NWCCTVNWCCTV Posts: 3,629
    edited 2013-04-23 20:03
    Actually I gave up and went on to other issues. I still can not get any modified wav files to play but I think that is because of the conversion software.
Sign In or Register to comment.