Cant find the logic about this failure


hello,

im trying simple sketch can't understand logic behind failure:

if execute:

code: [select]
const int nav = 8 ;
string selecopt = "default";
long previousmillis = 0;

void setup()
{
serial.begin(9600);
  pinmode(nav, input);
  pinmode(13, output);
}

void loop () {  
if(digitalread(nav) == high) { string selecopt = "cmon" ; digitalwrite(13, high);  }

if ( (millis() - previousmillis > 1000)) { previousmillis = millis(); serial.println(selecopt);  }
}


when press button, led goes light serial monitor wont show me "cmon" "default", no matter how many times hit digital input on pin 13.

but if run code:
code: [select]
const int nav = 8 ;
string selecopt = "default";
long previousmillis = 0;

void setup()
{
serial.begin(9600);
  pinmode(nav, input);
  pinmode(13, output);
}

void loop () {  
if(digitalread(nav) == high) {  digitalwrite(13, high);  }
string selecopt = "cmon" ;
if ( (millis() - previousmillis > 1000)) { previousmillis = millis(); serial.println(selecopt);  }
}



"cmon" thing show on serial monitor, naturally.

why doesnt work inside if statement led does?
what missing here?

appreciate clue.

rodrigo

code: [select]
string selecopt = "default";
here have global variable.

code: [select]
{ string selecopt = "cmon" ; digitalwrite(13, high);  }
those open , close curly braces define scope of selecopt (local) variable. ceases exist after }, global (unmodified) variable accessible.

try losing string variable declaration in if block. then, global variable modified.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Cant find the logic about this failure


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums