PDA

View Full Version : RC time decay



electromanj
12-10-2009, 09:49 AM
Hello,

I am trying to modify the TestRcDecay.spin program.
I would like to take the result of the rc time decay and use that as a setpoint, display the setpoint on the serial terminal, and store that setpoint for future reference.
The attachment is what I have done so far.
Any advice would be very much appriciated!

StefanL38
12-10-2009, 02:07 PM
Hello electromanj,

CONGRATS !!
this is the BEST way to get help. Making a first try and then attaching your code


You are using the variable time as a local variable




PUB Main | time




local variables are only accessible in the method where they are defined.
In this case the variable belongs to the method main and can only be accessed INSIDE the method main

You have to define the variable in the VAR-section




VAR
long time




you can't use a method for storing a value. A method can just GIVE BACK a value.
therefore you can use the reserved word "result".




pub setpoint

if time => 3000
'setpoint :=2
result :=2
elseif time =<2999
'setpoint :=1
result :=1
waitcnt (clkfreq / 2 + cnt)

debug.str(string(13,"setpoint= "))
debug.dec(result)
'debug.dec(setpoint)
'the calling a method in itself means you call it recursive
' every time setpoint gets called it calls itself again and again and again and again ....
'until ALL memory is used and your program CRASHES




storing as long as the program is running or should it be stored and restored even if the propeller gets resetted or power disconnected ?

best regards

Stefan

Post Edited (StefanL38) : 12/10/2009 6:12:13 AM GMT

electromanj
12-11-2009, 05:33 AM
Hello,

Thanks for the help. Everything is working now regarding seting the setpoint with the rc circuit, and displaying the result on the terminal. The next method I am trying is to turn on p8 if the setpoint was 1, and turn on p9 if the setpoint was 2. It seems to be running right through the first IF. Like it doesn't know what the result from the previous method was. No matter what the setpoint is it always turns on the led on P8. Any suggestions?

Thanks.

P.S. I only want to store the result of the rc circuit while the program is running. It should reset when the prop is reset.

StefanL38
12-11-2009, 07:48 AM
OK I understand,

the reserved word "result" is a special kind of a local variable.
regardless of the name of the method the "variable" result contains the value the method GIVES back

example




VAR
long MyGlobalVar_SetVal


Pub MyMethod
MyGlobalVar_SetVal := setpoint




the method setpoint GIVES BACK the value of the internal and local "variable" result

another hint:

":=" does assign a value

if you want to do a REAL comparing you have to code "==" a double-equal




if result == 1
dira[8]:=1
outa[8]:=1
waitcnt (clkfreq/2+cnt)
debug.str(string(13,"p8=high"))
'repeat 'this repeat makes the program stay forever in this empty repeat-loop
elseif result :=2
dira[9]:=1
outa[9]:=1
waitcnt (clkfreq/2+cnt)
debug.str(string(13,"p9=high"))
'repeat 'this repeat makes the program stay forever in this empty repeat-loop




the if statement does compile with the assigment operator
as this is a very short way to assign and compare at the same time
but you get a different result as with just a compare with the "=="-operator.
As you seem to be a beginner I highly recommend to use SIMPLE commands


If you want your program to re-read the pot again and again you have to code a repeat loop
all around the measuring and the code below but NO repeat inside of your method OutEqualSet

Now I want to make another suggestion: It is really good that you code on your own.
In general THAT'S the way to go. But I think you will climb up the learning curve faster
if you read and test the code-samples from the PE-kit labs

best regards

Stefan

electromanj
12-11-2009, 08:49 AM
Stefan, Thank You very much for your help. I finally feel like I'm on my way.

I am confused about variables. I am used to jellyjar VAR WORD. Now there is local var's and global var's. I am not sure how do you write to a global variable from within a method and access it from another method. I am spending alot of time reading the PE kit labs and I now have a printed version of the Propeller Manual. With the bs2 you delare a variable one time at the beginning of a program and it is accessible throughout every subroutine in the program. From what I understand so far every PUB is a subroutine. I do not understand how to make the varibles that are declared in the "var section" readable from any pub/method.
Thanks for everybody's help!
P.S I hesitated to get involved with the propeller for so long because I wasn't sure if I could "get it". Now that I have started with it I am glad I did. I have the confidence of the cool projects that I have done with the BS series, AND the confidence that I will recieve the same kind of support from the Prop Crowd. Thanks.

localroger
12-11-2009, 09:40 AM
Hi electromanj, welcome to Propland.

If you're only used to an environment with global vars (most really small things) locals can take a bit of time to wrap your head around. The answer to your question, how do you get to one pub's local var from another is ... you can't. At all. In fact, local vars don't even exist at all when the routine they belong to isn't running. That's another thing you need to keep in mind; a propeller local var isn't "static." If you call a routine with a local var, do some stuff, then call that routine again, the local var probably won't have the same value. In fact they aren't even guaranteed to be initialized to zero like Spin global vars are.

Local vars are for things like that loop counter everyone calls i so all the different i's don't interfere with one another. They also don't permanently take memory so they reduce Hub RAM requirements. For things you need to see from more than one routine, you must put them in a VAR or DAT block -- the distinction is a bit advanced, use VAR if you don't understand it -- and then the variable is visible to everybody, anywhere, at any time. It's also guaranteed to be set to zero when your program starts.

And fortunately, in Spin, those are pretty much the only two kinds of variables you ever see. The zoo of "scoping" in more "advanced" languages is enough to give you a "migraine."

electromanj
12-11-2009, 09:52 AM
OK so you declare var
word
jellyjar
colored by a pink surrounding. How do you write to that var from a pub, and how do you read from that var from another pub? Or am I completely lost?

electromanj
12-11-2009, 09:56 AM
My indentations didn't come out right in the previous post. I meant to have the word and jellyjar indented past the var.

StefanL38
12-11-2009, 02:44 PM
simply as shown in the code-snippet above

you can read a global var EVERY-where in the same file in ANY subroutine

to write to a global var you just mention its name - just the same way as you did it in PBasic




MyVar := whatever you like as long as it is an 32bit INTEGER-value (or a 16bit-word or a 8bit-byte)




This means whenever you want to access a variable in more than one sub-routine declare it as a global variable in the VAR-section

best regards

Stefan