Error 'opt' was not declared in this scope

I'm getting this error when I try to use this code:

void setup() { // put your setup code here, to run once: pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,INPUT); pinMode(7,INPUT); int opt=0; }

void loop() { // put your main code here, to run repeatedly: if(digitalRead(7)==HIGH){ while(digitalRead(7)==HIGH){} opt++; if(opt>=16){opt=0} } if(digitalRead(6)==HIGH){ while(digitalRead(6)==HIGH){} opt=0 }

digitalWrite(2,bitRead(opt,0)) digitalWrite(3,bitRead(opt,1)) digitalWrite(4,bitRead(opt,2)) digitalWrite(5,bitRead(opt,3))

}

This is the declaration of the opt variable:

scmcgowen: int opt=0;

It's declared inside the setup function, so you can only use it in the setup function. You are trying to use it in the loop function, thus the error. If you want to use the variable in both the setup and loop functions, you should make the variable global by moving the declaration outside the setup function.

This is a concept called scope, which is very important to understand. More information: https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/