Hide minor edits - Show changes to markup
place = place + (randomWalkLowRange - place); // modifica el número a una dirección poisitiva
place = place + (randomWalkLowRange - place); // modifica el número a una dirección positiva
static int place; // variable estática
// los valores se conservaron entre las siguientes llamadas a la función. Ninguna otra función puede cambiar su valor
static int place; // variable estática los valores se conservaron entre las siguientes
// llamadas a la función. Ninguna otra función puede cambiar su valor
place = place + (randomWalkLowRange - place); // reflect number back in positive direction
place = place + (randomWalkLowRange - place); // modifica el número a una dirección poisitiva
place = place - (place - randomWalkHighRange); // reflect number back in negative direction
place = place - (place - randomWalkHighRange); // modifica el número a una dirección negativa
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.
La palabra reservada static se utiliza para crear variables estáticas, estas variables tienen la peculiaridad de que no se crean y se destruyen cada vez que se llama al bloque de código en el que está definidas, sino que su valor se guarda para las sucesivas llamadas.
Las variables que se declaran como estáticas sólo se crearan e inicializarán la primera vez que se ejecute el bloque de código en el que están contenidas.
{ // tetst randomWalk function
{ // lanzamos la función randomWalk
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
static int place; // variable estática
// los valores se conservaron entre las siguientes llamadas a la función. Ninguna otra función puede cambiar su valor
if (place < randomWalkLowRange){ // check lower and upper limits
if (place < randomWalkLowRange){ // comprueba el límite mayor y el menor
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".
*/
Serial.println("start ");
total += thisTime; // aggregate values to see if function is really random
Serial.print(thisTime);
Serial.print(" ");
Serial.println(total);
delay(10);
Serial.println(thisTime); delay(10);
// values in between function calls, but no other functions can mess with its value
// values in between function calls, but no other functions can change its value
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".
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".
/* 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".
*/
int stepsize;
int thisTime; int total;
void setup() {
Serial.begin(9600);
Serial.println("start ");
}
void loop() { // tetst randomWalk function
stepsize = 5;
thisTime = randomWalk(stepsize);
total += thisTime; // aggregate values to see if function is really random
Serial.print(thisTime);
Serial.print(" ");
Serial.println(total);
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 mess with 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;
}
@]
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.
[@