Shop OBEX P1 Docs P2 Docs Learn Events
COGS??? — Parallax Forums

COGS???

Travis M. BestTravis M. Best Posts: 9
edited 2009-12-15 16:47 in Propeller 1
I am trying to figure out how to split my program in two parts and be able to transfer my A/D Data between the two maybe by using a cog???


CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000

OBJ
ADC : "CD_LTC1298"
PWM : "PWM_32_v2"

PUB MAIN | Temp,Temp1,Temp2,smpcnt,dimled,briled,midled,offled,offdim,dim,dimdled,leda,ledb,ledc,ledd,lede,ledf,ledg,ledh,ledchange,dimdim

adc.start(0)
PWM.Start
dim := 0


repeat
repeat until smpcnt > 59
Temp1 := adc.GetADC(0)
Temp2 := Temp2 + Temp1
smpcnt := smpcnt + 1


Temp := Temp2 / 60


offled := 0
offdim := 1
dimled := 30
midled := 40
briled := 100
smpcnt := 0
temp1 := 0
temp2 := 0



If Temp =< 81
If ledh < 100

ledchange := 100
dimdim := 30

ledh := 0
ledb := 0
ledc := 0
ledd := 0
lede := 0
ledf := 0
ledg := 0

PWM.Duty(4,briled,5000)
PWM.Duty(7,offled,5000)
PWM.Duty(9,dimled,5000)
PWM.Duty(13,dimled,5000)
PWM.Duty(14,dimled,5000)
PWM.Duty(22,dimled,5000)
PWM.Duty(23,offdim,5000)
PWM.Duty(25,offdim,5000)
PWM.Duty(26,offdim,5000)

ledh :=ledh + 1


If Temp => 82
If Temp =< 107
If leda < 100

ledchange := 100
dimdim := 30

ledb := 0
ledc := 0
ledd := 0
lede := 0
ledf := 0
ledg := 0
ledh := 0

PWM.Duty(4,offled,5000)
PWM.Duty(7,briled,5000)
PWM.Duty(9,briled,5000)
PWM.Duty(13,dimled,5000)
PWM.Duty(14,dimled,5000)
PWM.Duty(22,dimled,5000)
PWM.Duty(23,dimled,5000)
PWM.Duty(25,dimled,5000)
PWM.Duty(26,dimled,5000)

leda :=leda + 1

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2009-12-15 03:38
    By splitting your program "into two parts" do you mean you want to create a Main program and then a method that is called up by the Main program?
    Do you want to pass variable values from, say, the Main to the called up method?
    I think one option is that you can use global variables to pass values around.
    What exactly are you trying to do?

    Also, indenting is essential to your code. When you post code, use the little code box button under the smilies, one at the start to open the code and one at the end to close the code.

    So your code will look like this
         with the indentations preserved.
    
    



    Otherwise it's hard for people to understand what your code is really doing.
  • Travis M. BestTravis M. Best Posts: 9
    edited 2009-12-15 03:44
    What I am looking to do is constantly read the A/D and while it is sampling compare values with the IF statements to determine what leds to light up. Right now it reads the A/D then compares the samples. I would like it to read the A/D and compare samples at the same time
  • ElectricAyeElectricAye Posts: 4,561
    edited 2009-12-15 03:49
    I hope one of the bigger brains on the forum will correct me if I'm wrong, but I think you could achieve that by having one cog (perhaps the Main) continuously running your IF comparisons while another cog runs a method that includes your A/D operations and also inserts its results into global variables. Your Main would then be looking at the continuously updated global variables and make the desired decisions.

    Also, you could simply have a Main program call up 2 separate methods. One method continuously loops to read the A/D and inserts results into global variables. The second method does nothing but read the global variables and run IF statements and blink lights in a continuous loop.


    You would always have some kind of time lag to consider, of course, so you'd have to evaluate what sort of timing your system requires.

    hope that helps,
    Mark

    Post Edited (ElectricAye) : 12/15/2009 3:55:39 AM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-12-15 06:07
    ElectricAye is right. Use one COG for measurement and do the if-loop in your main - or maybe in another SPIN COG.

    Depends on how many COGs you can efford and how responsive it needs to be.

    I don't know your ADC object, but the start function makes me believe that it's already running in a COG. It just spends the time waiting for a 'measure now' command (adc.GetADC()) . Anyway, I'd have a look into this driver and change that in the way that it loops endlessly, doing the AD conversion and store the result in a variable for which you pass it's address in the start function.

    In your main program you will always find the latest measurement in this variable.

    Another way would be to put the if decision-making into the ADC COG as well. Because the value can only change from one measurement to the next and the ADC COG knows when it makes sense to check.

    It all depends on the goals you have for this program and what the objects you use can do. How many measurements can be done per second? How many do you need? At what rate do you need to change the LED output? Do you need the ADC value for something else? (Logging for example - or calculations). It only makes sense to split up a program in several sub-tasks, when one COG can't do the job fast enough.
  • kwinnkwinn Posts: 8,697
    edited 2009-12-15 16:47
    You could also have more than one cog doing the IF statements if you have the cogs available. The cog reading the ADC does a read whenever a flag is clear, stores the value in hub ram, and sets the flag . Each "IF" cog reads the ADC value and the cog that finds the value in range clears the flag.
Sign In or Register to comment.