RC time decay
electromanj
Posts: 270
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!
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!
Comments
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
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
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".
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
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.
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
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
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
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.
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."
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?
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
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