2

I am using the mega 2560. When I just use .ino and .h files, if I use Serial, it works fine, without even using:

#include <SoftwareSerial.h>

However, when I add a .cpp file I start getting 2 errors, one is solved by adding the aforementioned include, the second one, after adding it, is :

'Serial' was not declared in this scope

I know I can declare it manually and use the RX/TX pins, but I would like to be able to use the default Serial so that I don't have to occupy more pins. Especially since it works file w/o the .cpp file. Can anyone explain to me why this happens? Is it because I specify a C++ file so the compiler changes?

asked Jul 4, 2020 at 12:52

1 Answer 1

3

A CPP file isn't an Arduino file. It doesn't know about anything Arduino-esque unless you tell it about it.

The simplest way is to just add, at the top of the file:

#include <Arduino.h>
answered Jul 4, 2020 at 13:35
1
  • Ah, damn once you say it it becomes so obvious, thank you. Commented Jul 4, 2020 at 15:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.