I'm trying to do Serial.println() within a class in the Arduino IDE. However, the compiler is saying that Serial was not declared in this scope. Here is the code:
Code in Menu.h
class Menu
{
public:
int options[4];
void test() {
Serial.println("here");
}
private:
};
Code in main file:
#include "Menu.h"
Menu menu;
void setup() {
Serial.begin(9600);
menu.test();
}
void loop() {
}
The right #include is probably added automatically by Arduino to your .pde. Try #include <WProgram.h> in the top of your Menu.h.
I'm not sure if the code snippet is complete (if it isn't, please post a complete one) but it looks like you forgot to include the appropriate header file which declares the class Serial.
Related
I have noticed the following code, which is obviously invalid C++, compiles in Arduino IDE (using AVR-GCC):
// The program compiles even when the following
// line is commented.
// void someRandomFunction();
void setup() {
// put your setup code here, to run once:
someRandomFunction();
}
void loop() {
// put your main code here, to run repeatedly:
}
void someRandomFunction() {
}
What is going on here? C++ requires functions to be declared before they are used. When the compiler comes to the line someRandomFunction() in the setup() function, how does it know it will be declared?
This is what we call Forward declaration and in C++ it only requires you to declare the prototype of the function before attempting to use it, instead of defining the whole function.:
Taking as example the following two pieces of code:
CODE A:
#include <Arduino.h>
void setup(){}
void AA(){
// any code here
}
void loop(){
AA();
}
CODE B:
#include <Arduino.h>
void setup(){}
void loop(){
BB();
}
void BB(){
// any code here
}
Strictly speaking C requires that functions be forward declared for the compiler to compile and link them. So in CODE A we do not have declared the function but it defined, which makes it legal for proper C code. But the CODE B has the function definition after the loop, which would be illegal for plain C. A solution would be the following one:
#include <Arduino.h>
void BB();
void setup(){}
void loop(){
BB();
}
void BB(){
// any code here
}
This, however, to fit the Arduino script format of having a void setup() following from a void loop(), required Arduino to include a script on its IDE that automatically looks for functions and generate prototypes up top for you so you do not need to worry about it. So despite being written in C++, you will NOT see Arduino sketches using Forward declaration often in their sketches, as it works out fine and it is often easier to read having first setup() and loop().
Your file is .ino not .cpp. .ino is 'Arduino language'.
The main ino file with additional ino files are processed by the Arduino builder to C++ source and only then processed as C++ (preprocessor, compilation).
Although I read a few google-results for that error I can't find my problem for this error, not even while trying to reduce everything to its very basic content.
That's my testclass.h:
class TESTCLASS {
public:
TESTCLASS();
};
int x; // I added this for testing if the file is included from my main code file
x=10; // It is and throws this error: testclass.h:8:1: error: 'x' does not name a type, which I don't understand neither, but it't not the main problem here
testclass.cpp:
#include "testclass.h"
TESTCLASS::TESTCLASS() {
// do some stuff
}
and here's my main code file:
#include "lib/testclass.h"
TESTCLASS test;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
This throws the error
/var/folders/b5/qc8dstcn02v_hyvgxsq4w9vr0000gq/T//ccQOziAu.ltrans0.ltrans.o: In function `_GLOBAL__sub_I_test':
/Volumes/Daten/stefanherzog/Documents/Nextcloud/Programmierung/Arduino/200515_growboxLibrary_test/200515_growboxLibrary_test.ino:3: undefined reference to `TESTCLASS::TESTCLASS()'
collect2: error: ld returned 1 exit status
exit status 1
So even this is very basic I can't see the problem! I'm using an avr-g++ compiler within my Arduino IDE (v1.8.12).
Can someone please explain me what I'm doing wrong?
it looks like you don't send testclass.cpp to your compiler. If so, your problem does'nt come from your code but your compiling command line.
Using gcc you should have something like :
g++ main.cpp lib/testclass.cpp -o testclass
I don't know the compilation process for arduino but i hope it will helps you finding the solution.
Easiest put testclass.cpp into the same folder as your .ino file. It should show up as a separate tab.
Put testclass.h there as well. and remove the lib subfolder.
And remove the int x=10; definition from the .h file. If both units are including testclass.h, that should end up in a duplicate error.
BTW: an assignment x=10; outside a function is nonsense anyway.
When using subdirectories with the Arduino IDE the subdirectory needs to be named as utility. That's actually it!
Having this structure for example (in ../Arduino/libraries/):
./testclass
./testclass/testclass.h
./testclass/testclass.cpp
./testclass/sub
./testclass/sub/sub.h
./testclass/sub/sub.cpp
testclass.h:
#ifndef __TESTCLASS_H__
#define __TESTCLASS_H__
#include "utility/sub.h"
class TESTCLASS {
public:
TESTCLASS();
};
#endif
testclass.cpp:
#include "testclass.h"
TESTCLASS::TESTCLASS() {
// do some stuff
}
sub.h:
class SUBCLASS {
public:
SUBCLASS();
};
sub.cpp:
#include "sub.h"
SUBCLASS::SUBCLASS() {
// do some stuff
}
You can simply include the "main" testclass.h in your project and instantiate the class and even the subclass:
#include <testclass.h>
TESTCLASS test;
SUBCLASS sub;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
I have this problem:
Problem:
I am trying to create a library (ard33WiFi) that manages and handles
a couple of other libraries (WiFiServer library for example)
I need to create the server object that I then use in functions in my Library (ard33WiFi):
WiFiServer myServer(iPort);
The problem is that when I call myServer in the members of the class I get:
'myServer' was not declared in this scope
Where/how do I declare myServer so that is becomes available to the entire class (ard33WiFi)? I have taken out any decleration because whatever I was trying was wrong. I have pasted a skeleton code below.
// HEADER FILE (.h)
// ----------------------------------------------------------------------------------------------
#ifndef Ard33WiFi_h
#define Ard33WiFi_h
#include <WiFiNINA.h>
#include <WiFiUdp.h>
class ard33WiFi{
public:
ard33WiFi(int iPort)
void someFunction();
void serverBegin();
private:
int _iPort;
};
#endif
// ----------------------------------------------------------------------------------------------
// C++ FILE (.cpp)
// -----------------------------------------------------------------------------------------------
#include <Ard33Wifi.h>
ard33WiFi::ard33WiFi(int iPort){
_iPort = iPort;
}
void ard33WiFi::someFunction(){
// code here required to prepare the server for initializing
// but ultimately not relevant to the question
}
void ard33WiFi::serverBegin(){
myServer.begin();
Serial.println("Server Online");
}
I run into the same problem with the UDP library as I need to call the UDP object in various functions to do UDP things.
Any help would be greatly appreciated.
I suppose you are using this:
https://www.arduino.cc/en/Reference/WiFiServer
I can see that you are not declaring the myServer in your class; that I guess is the error in your code. If I am not wrong, it should be something like this:
#ifndef Ard33WiFi_h
#define Ard33WiFi_h
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <WiFi.h> // Not sure if you have to append this include
class ard33WiFi{
public:
ard33WiFi(int iPort)
void someFunction();
void serverBegin();
private:
int _iPort;
WiFiServer myServer;
};
#endif
The implementation, you would need to initialise the instance:
#include <Ard33Wifi.h>
ard33WiFi::ard33WiFi(int iPort):myServer(iPort), _iPort(iPort) {
}
void ard33WiFi::someFunction(){
// code here required to prepare the server for initializing
// but ultimately not relevant to the question
}
void ard33WiFi::serverBegin(){
myServer.begin();
Serial.println("Server Online");
}
I have a problem trying to use classes in Arduino and my particular problem is that my code won't work as i have thought. I'll explain:
I made a library that allows me to control a series of LEDs making them blink, and I'd like to repeat them as many times as programmed. However, I've found a problem with it: when I run the code the cycle will repeat just once and it won't blink all of the LEDs again, as the code is supposed to do. This is the code:
Header
#ifndef Cluster_h
#define Cluster_h
#include "Storm.h"
#include "Arduino.h"
class Cluster{
public:
Cluster(int pin[]);
void lightning(unsigned long g_glag[]);
private:
unsigned long currentmillis,prevmillis,g_flag[];
int counter,u_flag;
Storm* _led;
};
#endif
CPP file
#include <Cluster.h>
Cluster::Cluster(int pin[]){
Storm led[7]={Storm(pin[0]),Storm(pin[1]),Storm(pin[2]),Storm(pin[3]),Storm(pin[4]),Storm(pin[5]),Storm(pin[6])};
_led=led;
prevmillis=0;
counter=0;
}
void Cluster::lightning(unsigned long g_flag[]){
if(counter<=5){
currentmillis=millis();
if(currentmillis-prevmillis>=g_flag[0]){
_led[0].blinkled();
}
if(currentmillis-prevmillis>=g_flag[1]){
_led[1].blinkled();
}
if(currentmillis-prevmillis>=g_flag[2]){
_led[2].blinkled();
}
if(currentmillis-prevmillis>=g_flag[3]){
_led[3].blinkled();
}
if(currentmillis-prevmillis>=g_flag[4]){
_led[4].blinkled();
}
if(currentmillis-prevmillis>=g_flag[5]){
_led[5].blinkled();
}
if(currentmillis-prevmillis>=g_flag[6]){
prevmillis=currentmillis;
counter=counter+1;
}
}
}
The blinkled function from the Storm class is imported from the header file and what this function does is blinking a particular LED (this is already defined). With this code I would expect that the blinking sequence would repeat for 5 times but as I mentioned at the beginning it will blink all the LEDs just once. I have not found what could be wrong with the code, if you could help me that would be awesome.
I appreciate your time, I'll also put the code in the implementation part, just in case.
#include <Cluster.h>
int p[7]={2,3,4,5,6,7,8};
unsigned long gf[7]={50,70,110,150,220,240,1000};
Cluster cluster(p);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
cluster.lightning(gf);
}
You are not setting prevmillis
Change your function
void Cluster::lightning(unsigned long g_flag[]){
static unsigned long prevmillis=millis();
if(counter<=5){
currentmillis=millis();
if(currentmillis-prevmillis>=g_flag[0]){
_led[0].blinkled();
}
if(currentmillis-prevmillis>=g_flag[1]){
_led[1].blinkled();
}
if(currentmillis-prevmillis>=g_flag[2]){
_led[2].blinkled();
}
if(currentmillis-prevmillis>=g_flag[3]){
_led[3].blinkled();
}
if(currentmillis-prevmillis>=g_flag[4]){
_led[4].blinkled();
}
if(currentmillis-prevmillis>=g_flag[5]){
_led[5].blinkled();
}
if(currentmillis-prevmillis>=g_flag[6]){
prevmillis=currentmillis;
counter=counter+1;
}
}
}
I want to make a class in cpp for arduino uno that writes on a display. I'm using the LiquidCrystal_I2C library but I can't use it in my class. I know how to do it without a class, but right now I want to build a class and I cant get it to work.
My .h file:
// WriteDisplay.h
#ifndef _WRITEDISPLAY_h
#define _WRITEDISPLAY_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire/Wire.h>
#include <LiquidCrystal_I2C2004V1/LiquidCrystal_I2C.h>
class WriteDisplayClass
{
public:
WriteDisplayClass();
void write(String text);
private:
LiquidCrystal_I2C lcd(0x27,20,4);
};
extern WriteDisplayClass WriteDisplay;
#endif
My .cpp:
#include "WriteDisplay.h"
WriteDisplayClass::WriteDisplayClass()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
}
WriteDisplayClass::write(String text)
{
lcd.clear();
lcd.print(text);
}
WriteDisplayClass WriteDisplay;
My .ino:
#include "WriteDisplay.h"
WriteDisplayClass wdc;
void setup()
{
wdc.write("Hello World");
}
void loop()
{
}
I'm using AtmelStudio with Visual Micro. I'm getting it to work when I'm only using my .ino-file, but I can't do the same thing in cpp. I'm getting errors that LiquidCrystal_I2C.h can't be found and stuff like that. How should I do to get it to work the way I want it to? Or is it even possible?
Thanks for answer.
Sorry I misread the question the first time.
To use libraries in the .cpp file of an Arduino sketch you must also include them in the master .ino file. They are only compiled if found in the .ino
You can add the includes manually or use the "Project>Add/Import Sketch Library" menu item which will add them to the .ino for you.
Based on this post here the problem with your code is related the class being instantiated as global. The problem comes that the compiler doesn't guarantee the order of global variables processing, so in order to guarantee that the object concerning the display is executed lastly after all the library ones, you have to instantiate it in the setup() function!
The solution to your .ino code is to set a global pointer and then you assign the object inside the setup() function, like so:
#include "WriteDisplay.h"
WriteDisplayClass *wdc;
void setup()
{
wdc = new WriteDisplayClass();
wdc->write("Hello World");
}
void loop()
{
}