diff options
| author | Marti Bolivar <mbolivar@mit.edu> | 2010-10-20 06:46:52 -0400 | 
|---|---|---|
| committer | Marti Bolivar <mbolivar@mit.edu> | 2010-10-20 06:46:52 -0400 | 
| commit | 85c1c72db022bba891868afd3375e39dbe245701 (patch) | |
| tree | 9d86a3db825667362a8c89a98a205586015aec94 /source/arduino/static.rst | |
| parent | abcfcc62cc62dfc088d30d5a6b6c36d6c89f7b07 (diff) | |
| download | librambutan-85c1c72db022bba891868afd3375e39dbe245701.tar.gz librambutan-85c1c72db022bba891868afd3375e39dbe245701.zip  | |
initial check-in of arduino docs in RST format (converted using wget+pandoc)
Diffstat (limited to 'source/arduino/static.rst')
| -rw-r--r-- | source/arduino/static.rst | 71 | 
1 files changed, 71 insertions, 0 deletions
diff --git a/source/arduino/static.rst b/source/arduino/static.rst new file mode 100644 index 0000000..1c0340e --- /dev/null +++ b/source/arduino/static.rst @@ -0,0 +1,71 @@ +.. _arduino-static: + +Static +====== + +The static keyword is used to create variables that are visible to +only one function. However unlike local variables that get created +and destroyed every time a function is called, static variables +persist beyond the function call, preserving their data between +function calls. + + + +Variables declared as static will only be created and initialized +the first time a function is called. + + + +Example +------- + +:: + +     +     +    /* RandomWalk +    * Paul Badger 2007 +    * RandomWalk wanders up and down randomly between two +    * endpoints. The maximum move in one loop is governed by +    * the parameter "stepsize". +    * A static variable is moved up and down a random amount. +    * This technique is also known as "pink noise" and "drunken walk". +    */ +     +    #define randomWalkLowRange -20 +    #define randomWalkHighRange 20 +    int stepsize; +     +    int thisTime; +    int total; +     +    void setup() +    { +      Serial.begin(9600); +    } +     +    void loop() +    {        //  tetst randomWalk function +      stepsize = 5; +      thisTime = randomWalk(stepsize); +      Serial.println(thisTime); +       delay(10); +    } +     +    int randomWalk(int moveSize){ +      static int  place;     // variable to store value in random walk - declared static so that it stores +                             // values in between function calls, but no other functions can change its value +     +      place = place + (random(-moveSize, moveSize + 1)); +     +      if (place < randomWalkLowRange){                    // check lower and upper limits +        place = place + (randomWalkLowRange - place);     // reflect number back in positive direction +      } +      else if(place > randomWalkHighRange){ +        place = place - (place - randomWalkHighRange);     // reflect number back in negative direction +      } +     +      return place; +    } +     +  | 
