ANSI C & Arduino

Hello, I made this function and want to know because if don´t place the variable i as volatile, the function does not work. Variable i is local.

void MiDelay(unsigned long Tiempo){
volatile unsigned long i;
unsigned long endTime = 1000 * Tiempo;

for(i=0; i<endTime; i++);

Thanks… Jorge

Your for loop does nothing. When i is not volatile, the compiler sees that changing i serves no purpose, so it optimizes away the whole for loop.

By declaring i volatile, the compiler is told that the variable might be altered by external action during the for loop, so the loop can't be optimized away.