'pinValue' Not declared error

Hi guys. I get a ‘pinValue’ was not declaed in this scope’ error when trying to compile the below code. I am new to this, and might have done lots of things wrong, so please bear with me. I am using a Wemos D1 board (ESP8266 Wifi) and using the Blynk server. Library version is v0.5.4

Thank you very much



#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos= 0; // variable to store the servo position

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "c69a278c02814112bf20f7f4f9318ac9";
BlynkTimer timer; // Create a Timer object called "timer"!

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Legere";
char pass[] = "5596226781";


// in Blynk app write values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable

}

void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  myservo.attach(5); // attaches the servo on pin 5 to the servo object

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

   if (pinValue == "1") {       
      digitalWrite(4,HIGH); //switches external led on     
    for(pos = 0; pos < 180; pos += 4) // goes from 0 degrees to 180 degrees
    // in steps of 4 degrees
    myservo.write(pos); // tell servo to go to position in variable 'pos'
   }
   
    else  {
        digitalWrite(4, LOW); //switches external led off
    for(pos = 180; pos>=1; pos-=4) // goes from 180 degrees to 0 degrees
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    }

void loop()
{
  Blynk.run();
  timer.run();
}

It looks like you haven’t closed off the setup loop with a closing } This will throw your compiling off track and it will spout incorrect errors.

1 Like

Hmmmm…
Your error message is caused by a rookie error and is to do with the scope of your pinValue variable.
You’re declaring the pinValue variable as an integer within the BLYNK_WRITE(V1) function…

this means that the pinValue variable, and the value that’s been assigned to it, only exist within that function.
The solution to this is declare the variable at the top of your code. I’d put it immediately after the declaration for your ‘pos’ variable. You then remove the ‘int’ from in front of the pinValue in the BLYNK_WRITE(V1) function.

At the top of your code:

int pinValue;

New version of BLYNK_WRITE(V1) function:

BLYNK_WRITE(V1)
{
pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
}

However, this won’t fix the other problem in your code.
Your if (pinValue == "1") chunk of code is inside your void setup. Void setup only runs once, at startup, so putting your code here won’t work the way you want it to.

You have two options:

  1. create a function and put your code in it, then call it from BLYNK_WRITE(V1).
  2. move your code directly into the existing BLYNK_WRITE(V1) function, so that it executes automatically every time the value of V1 changes.

The choice of which option to choose depends on how your code is going to be developed. If you wanted to execute this code in various ways (not just when the value of V1 changes) then option 1 is better.

As your code stands at the moment, option 2 is easier and neater. Incidentally, putting the code in the BLYNK_WRITE(V1) function actually fixes your initial error message, as it’s all taking place within the same function where the pinValue declaration is taking place. Having said that, it’s often better to do your variable declarations at the top of the code.

The revised code would look like this:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos= 0; // variable to store the servo position
int pinValue; // incoming value from pin V1

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "c69a278c02814112bf20f7f4f9318ac9";
BlynkTimer timer; // Create a Timer object called "timer"!

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Legere";
char pass[] = "5596226781";


// in Blynk app write values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable

  if (pinValue == "1")
  {       
    digitalWrite(4,HIGH); //switches external led on     
    for(pos = 0; pos < 180; pos += 4) // goes from 0 degrees to 180 degrees
    // in steps of 4 degrees
    myservo.write(pos); // tell servo to go to position in variable 'pos'
  }
   else
  {
    digitalWrite(4, LOW); //switches external led off
    for(pos = 180; pos>=1; pos-=4) // goes from 180 degrees to 0 degrees
    myservo.write(pos); // tell servo to go to position in variable 'pos'
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  myservo.attach(5); // attaches the servo on pin 5 to the servo object

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop()
{
  Blynk.run();
  timer.run();
}

and as @Gunner has just pointed out, there was a bracket missing as well :wink:

BTW, its not good practice to share our Auth code on a public forum. It;s easy enough to generate a new one, so you should do that and use it in your updated code.

Pete.

Thanks a lot PeteKnight, and also Gunner.

I really do appreciate the help and education.
I will upload the new code, and give feedback on how it works in my project.

I forgot to edit out the Aut code, and the Wifi credetials due to tiredness. Long hours tends to make me overlook stuff :blush:

1 Like

Righto, so I uploaded the code, and a new error message sprung up.

"Blynk_Lock:24:19: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

if (pinValue == “1”)

I do not have the faintest idea how to fix that. Seems I am more of a rookie than anticipated.

1 Like

Okay, didn’t spot that error first time around, and I didn’t compile the code as I wrote the response on my iPad.

We’ve declared pinValue as an Integer:

but by having the 1 in quotes you’re telling the compiler that you want this to be treated as a string:

You’re not allowed to say ‘if integer = string’, hence the error.

Remove the quotes and it’ll be fine:

if (pinValue ==1)

Pete.

Hi Pete,

Worked like a charm. Will upload it now, and test the hardware.
Thanks a lot for the help, and the valuable lessons.
I really do appreciate it :slight_smile:

1 Like

No problem. It can be a bit daunting when you’re trying to get to grips with C++, especially if you’ve not done any coding before.

Hopefully, by giving a comprehensive explanation you and others can learn from the issues you were having (assuming of course that they bother to do a search :roll_eyes:).

Pete.

1 Like

Oh, I learned indeed. And you are right, very daunting indeed, since I only started studying about two months ago. I am all over this forum now, looking at all different ideas and issues :slight_smile:
Amazing forum. Thanks for making my first experience a great one!

1 Like

Update on the project;

Uploaded the code, with the new Auth code, and everything worked as planned :):star_struck:
Now on to build the actual lock…

2 Likes