error: 'variable' was not declared in this scope

Arduino IDE shows an error:

C:\Users\Documents\Arduino\Code\Code.ino: In function 'void setup()': Code:3:2: error: 'myVal' was not declared in this scope myVal = 5; ^~~~~ exit status 1 'myVal' was not declared in this scope

My code is:

void setup() { // put your setup code here, to run once: myVal = 5; } void loop() { // put your main code here, to run repeatedly: }

How can I solve it?

Answer

This is a common mistake for beginners who just got started to learn programming.

It is very easy to solve. You just need to declare variable by one of two following ways:

int myVal; void setup() { // put your setup code here, to run once: myVal = 5; } void loop() { // put your main code here, to run repeatedly: }
void setup() { // put your setup code here, to run once: int myVal = 5; } void loop() { // put your main code here, to run repeatedly: }

See global variable vs local variable

Buy Arduino

1 × Arduino UNO Buy on Amazon
1 × USB 2.0 cable type A/B Buy on Amazon
1 × Jumper Wires Buy on Amazon
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

The Best Arduino Starter Kit

※ OUR MESSAGES