Was not declared in this scope --

Hi All,

I am getting the above error for the simple sketch below:



#include <SoftwareSerial.h>

void setup()
{

  SoftwareSerial myserial(0, 1); // set the serial connection

  myserial.begin(9600); // start serial connection with 9600 baud rate

}

void loop()
{

  myserial.println("helloworld!"); // print "helloworld" to serial

  delay(1000); // delay 1000ms

}

``

find the error below:


C:\Users\M17 R3\Documents\Arduino\sketch_apr05a\sketch_apr05a.ino: In function 'void loop()':

sketch_apr05a:19:3: error: 'myserial' was not declared in this scope

   myserial.println("helloworld!"); // print "helloworld" to serial

   ^~~~~~~~

C:\Users\M17 R3\Documents\Arduino\sketch_apr05a\sketch_apr05a.ino:19:3: note: suggested alternative: 'Serial'

   myserial.println("helloworld!"); // print "helloworld" to serial

   ^~~~~~~~

   Serial

exit status 1

'myserial' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Thank you

You declared myserial in setup() rather than globally. What board are you using? 0 and 1 pins are the hardware serial on most boards.

#include <SoftwareSerial.h>

SoftwareSerial myserial(0, 1); // set the serial connection
void setup()
{
   myserial.begin(9600); // start serial connection with 9600 baud rate
}

void loop()
{
  myserial.println("helloworld!"); // print "helloworld" to serial
  delay(1000); // delay 1000ms
}

Objects that are declared inside a function follow the same scoping rules as variables (cause they are the same). Here is a good page on scope (object visibility and lifetime).

From the Useful links page.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.