Error Compiling Blink sample

Hello! I want to use the Tx,Rx pins as "normal gpio" pins. I tested with the Blink example code. But, i got this error:

Arduino: 1.8.3 (Linux), Alaplap:"WeMos D1 R2 & mini, 80 MHz, 115200, 4M (3M SPIFFS)"

/tmp/arduino_modified_sketch_710267/Blink.ino: In function 'void loop()':
Blink:34: error: 'statepl' was not declared in this scope
   digitalWrite(statepl, HIGH);   // turn the LED on (HIGH is the voltage level)
                ^
exit status 1
'statepl' was not declared in this scope

The sample code:

// the setup function runs once when you press reset or power the board
void setup() {
  int statepl = 3;
  pinMode(statepl, FUNCTION_3);//rx pin!!!
  pinMode(statepl, OUTPUT);
}


// the loop function runs over and over again forever
void loop() {
  digitalWrite(statepl, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(statepl, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Only work when i declare the statepl variable in the void loop statement. This is not the best solution. I want all the variables declaring outside from the void loop. Is it possible? Thanks!

By declaring statepl inside setup() you make it local to setup() and thus you get the "not declared in this scope" error when you try to use it in loop(). You need to declare statepl in the global scope (outside of setup()) if you want to be able to use it in loop():

int statepl = 3;
// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(statepl, FUNCTION_3);//rx pin!!!
  pinMode(statepl, OUTPUT);
}


// the loop function runs over and over again forever
void loop() {
  digitalWrite(statepl, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(statepl, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

int statepl = 3; Since this is in setup() statepl is not reachable in loop. Look up C++ scope.

Put int statepl = 3; before setup() i.e. make it global.

.

Thanks! It works!

Why would you name a variable that holds a pin number "statep1"? Do you plan to store the state of the pin in "pinp1"?

Why are you setting the mode of the pin to FUNCTION_3? Where the hell is that defined?

PaulS: Why are you setting the mode of the pin to FUNCTION_3? Where the hell is that defined?

In the ESP8266 core. It has a lot of modes:

//GPIO FUNCTIONS
#define INPUT             0x00
#define INPUT_PULLUP      0x02
#define INPUT_PULLDOWN_16 0x04 // PULLDOWN only possible for pin16
#define OUTPUT            0x01
#define OUTPUT_OPEN_DRAIN 0x03
#define WAKEUP_PULLUP     0x05
#define WAKEUP_PULLDOWN   0x07
#define SPECIAL           0xF8 //defaults to the usable BUSes uart0rx/tx uart1tx and hspi
#define FUNCTION_0        0x08
#define FUNCTION_1        0x18
#define FUNCTION_2        0x28
#define FUNCTION_3        0x38
#define FUNCTION_4        0x48

Hi!

I try use ESP8266 with GPIO6-11. I use pinMode (9, FUNCTION_3); (or I tryed Function_4), but not work. Why?

// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode (6, FUNCTION_3); pinMode(6, OUTPUT); pinMode (7, FUNCTION_3); pinMode(7, OUTPUT); pinMode (8, FUNCTION_3); pinMode(8, OUTPUT); pinMode (9, FUNCTION_3); pinMode(9, OUTPUT); pinMode (10, FUNCTION_3); pinMode(10, OUTPUT); pinMode (11, FUNCTION_3); pinMode(11, OUTPUT); pinMode(2, OUTPUT); //built in led

}

// the loop function runs over and over again forever void loop() { digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(2, HIGH); delay(1000); // wait for a second digitalWrite(11, LOW); // turn the LED off by making the voltage LOW digitalWrite(2, LOW); delay(1000); // wait for a second }

but not work

Please describe what it does do

  pinMode (6, FUNCTION_3);

What is FUNCTION_3 and why are you assuming it makes sense as a pin mode?

PaulS: What is FUNCTION_3 and why are you assuming it makes sense as a pin mode?

Déjà vu

Explanation for the FUNCTION_n modes: https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations

An important note:

Note that GPIO6-GPIO11 are typically used to interface with the flash memory ICs on most esp8266 modules, so these pins should not generally be used.

Yes, I read it. I am not typically user :D . But theoretically, the opportunity is given to use them when we change the mode.(Function_3 is a normal IO mode). Why not work this mode?

I watch the pin2, this not working too. After start blink short(0,5sec), and 7sec pause, and again short blink, 7sec, and carry on....