Framework (A-Z)

Reference for Wiring version 1.0 Build 0100+ If you have a previous version, use the reference included with your software. If see any errors or have any comments, let us know.

Name

volatile

Examples
volatile int val;
	
void setup() {
  // set myFunction to be called everytime 
  // interrupt 2 is generated (everytime the pin gets from HIGH to LOW)    
  attachInterrupt(EXTERNAL_INTERRUPT_2, myFunction, FALLING); 
  
  // Starts serial to print data                                 
  Serial.begin(9600);                  
}

void loop() { 
  // sets val to 0 and polls val until reaches the value 255
  val = 0; 
  while (val!=255) 
    continue; 
	
  // prints that val changed
  Serial.println("Finally val is 255");
}

void myFunction() {
  // WARNING: you should avoid doing long procedures in interrupt routines
  val = 255;
}
Description the volatile qualifier is applied to a variable when it is created. It tells the compiler the value of the variable may change at any time wihout any action being taken by the code around it. A variable has to be declared volatile whenever its value might change unexpectedly, like an interrupt service function (see the example above).
Syntax
volatile datatype variablename
Parameters
datatype any data type: int, double, long, char, byte etc
variablename a valid variable name
Usage Application
Updated on July 07, 2011 11:09:31pm PDT

Creative Commons License