Spin Help Needed
NWCCTV
Posts: 3,629
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
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.
Also you might want to try this object for servo control - http://obex.parallax.com/objects/445/
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.
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?
Actually you might want to put a small wait after initializing the servo object
The error probably means the correct object isn't specified but I'm not 100% on that.
Also this:
Should be:
oops!