Hot answers tagged

32

How the IDE organizes things First thing, this is how the IDE organizes your "sketch": The main .ino file is the one of the same name as the folder it is in. So, for foobar.ino in foobar folder - the main file is foobar.ino. Any other .ino files in that folder are concatenated together, in alphabetic order, at the end of the main file (regardless of where ...


17

As taken from the accepted answer from When should you use a class vs a struct in C++? The only difference between a class and a struct in C++ is that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance, and ...


10

Unlike C, an instance of a struct in C++ is an object in exactly the same way as an instance of a class. From the point-of-view of the compiled code, they are identical. Memory usage, alignment, access times etc. are exactly the same (i.e. there are no overheads). From the programmer's point-of-view, there is a very minor difference. Members of a struct ...


6

My advice is to stick to the typical C++ way of doing things: separate interface and implementation into .h and .cpp files for each class. There are a few catches: you need at least one .ino file - I use a symlink to the .cpp file where I instantiate the classes. you must provide the callbacks that the Arduino environment expects (setu, loop, etc.) in some ...


6

I'm posting an answer just for completeness, after finding out and testing a way of declaring and implementing a class in the same .cpp file, without using a header. So, regarding the exact phrasing of my question "how many file types do I need to use classes", the present answer uses two files: one .ino with an include, setup and loop, and the .cpp ...


6

Unless you specifically limit yourself to one single class for a callback (i.e., include the class name in the function pointer specification) then you can ONLY use static member functions as callbacks. static member functions appear as a normal function, not a member function, so don't have the implied MyClass *this as a first parameter. If you want to ...


6

The Stream class has pure virtual methods which must be implemented in derived not abstract class. The pure virtual method from base class Print is: virtual size_t write(uint8_t) = 0; The pure virtual methods from Stream are: virtual int available() = 0; virtual int read() = 0; virtual int peek() = 0; additionally add in your class the line using Print:...


5

It looks this problem may be due to bad order of initializers calls for global variables. In C++, the order of global variable initialization is unpredictable across different compilation units (i.e. C++ source files, not header files). Initialization order is only respected inside one compilation unit. In your program, there are several global variables ...


5

Interrupt Service Routine (ISR) outside a class Let's consider a simple use of interrupts: volatile bool switchChanged; void switchPressed () { switchChanged = true; } // end of switchPressed void setup () { pinMode (2, INPUT_PULLUP); attachInterrupt (0, switchPressed, CHANGE); } // end of setup void loop () { // whatever } // ...


5

Without seeing the actual code (BTW why can't you add it? Is it top-secret?) I can't be sure, but usually this happens when you don't define a variable but just declare it. Example: // File myclass.h class A { public: static int i; }; // File myclass.cpp void myfunc() { A::i = 0; } If you actually declare it the error should go away: // File ...


5

As a matter of efficiency, I would favor chrisl’s advise to use the platform's low-level interrupts if at all possible. This, however, comes at the cost of portability: you would need an implementation specialized for each platform you want to support. If you don't want to or cannot maintain all this platform-specific code, attachInterrupt() may be a ...


4

It is all about calling conventions and argument binding. The code is using call-by-value when it should be call-by-reference. This is call-by-value (the incoming command is not updated, a copy is passed as parameter): void Widget::init(Command _command) ... widget.init(command); This is call-by-reference (a pointer to command is passed as parameter): ...


4

When you put the Serial.begin in the constructor and declare the Command object at file scope, the Serial.begin will occur before setup(). Not recommended, perhaps not allowed. If you do that, the compiler has to figure out that the Serial instance must be constructed before the Command instance is constructed. It can't always figure out the correct order....


4

I think these lines still need to be in your main sketch file (in addition to being in your custom class file): #include <Adafruit_LEDBackpack.h> #include <Adafruit_GFX.h> If I remember the Arduino IDE quirks correctly, it uses the #include directives in the main sketch file to determine which libraries to actually link. It's one of those ...


4

You need to include roundto() in your class declaration, e.g. something like this: class cSpeedOfSound { public: cSpeedOfSound(float *i,float *C); private: float roundto(float x, float dp); float *C; float *a; float *i; }; In C++, class structures are fixed. You can define the body of a function almost anywhere, but all member ...


4

As other answers have pointed out, your particular struct and class are indistinguishable performance wise (There are slight differences in the scopes of the type names, due to the way you defined your struct). The delineation in C++ is not between struct and class, but between types that are POD (plain old data) and types that are not, as explained in this ...


4

When you put the type there, you are creating new variables. SO in your constructor: MKSStepperMotors(int dirPin, int stepPin, int enablePin) { int _dirPin = dirPin; int _stepPin = stepPin; int _enablePin = enablePin; You start by creating three new variables with the same names as the member variables defined in the class. Throughout ...


4

The reason is that _Display will be used probably after its construction. So you pass the byte during construction, and afterwards the value is available during the object's lifetime. In C you probably will do this by: Passing the variable (byte DisplayType) in each function where the value is needed (you can use this way in C++ too but it's cumbersome ...


4

class Motor { This says to start defining a class. A class is like a "container" that holds both data and functions (methods). After defining the class, it becomes like a new variable type that you can declare multiple instances of. int enablePin; int directionPin1; int directionPin2; These define the 3 integer variables (int=integer; whole numbers ...


3

The problem is that you're declaring multiple pointer variables called CActiveLCD in the _UpdateLCD() function. You do it once at the start of the function like this: LiquidCrystal* CActiveLCD; You then declare a new and totally separate pointer inside the if block like this: LiquidCrystal** CActiveLCD = &_Clcd2; This is called masking. The original ...


3

In C++, a class that holds reference members (in your sample, LiquidCrystal& _Clcd1; and LiquidCrystal& _Clcd2;) must ensure these references are initialized at construction time, not later. This means your void init(LiquidCrystal& Clcd1, LiquidCrystal& Clcd2); method is not the right way to initialize _Clcd1 and _Clcd2 because it will be ...


3

uart::uart(void); That's a prototype, not a function. It needs a body. Also, using "void" to say "no parameters" is really confusing. Better to leave it out all together. uart::uart() { } uart.start(9600); uart.print("Hello world\n"); uart is a class, not an instance. You should be using uart::start(9600) if you first set your functions to be static, ...


3

As you have already noticed, you cannot call methods of a class, nor access its properties, outside a "block" (as you call it). More specifically, you can only perform "programatical" operations from within a function. Anything outside a function (known as the global scope) is purely for declaration and initialization of variables and types, etc. Any ...


3

In terms of memory, what difference will this make? None. Structs and classes are the same thing, differing only in protection levels, and instantiating either creates an 'object'. still see the struct technique being used heavily - why is this? Less typing if you're not trying to hide data members.


3

The Arduino build preprocessing cannot locate the sensor source and header files if they are located in another sketch directory. The files needs to be moved to separate directory if shared (as a library). --Sketchbook ---NodeIntercom ----NodeIntercom.ino ---MasterNode ----MasterNode.ino ---libraries ----Sensor -----Sensor.h -----Sensor.cpp The Sensor ...


3

Dave X is absolutely right. I never assigned the senRoll pointer to an object. Then when I tried to dereference it, it went to some random address. I guess there are very few memory protections, so rather than causing a seg fault it just caused erratic behavior. Thanks for the help.


3

I see two separate issues here. First of all your enum isn't available as a variable. By declaring it as a private type within your class the enum can't be used outside of that class. This forces a user of that class to handle unintended calls like (attention, bug): CandleRack(255); which can make code harder to read (and debugging more difficult, ...


3

Yes, it's possible. Just don't make a class. Just make functions instead. Like a class-based one, have a .cpp and a .h file. In the .cpp file (or .c file if you don't want any of the C++ functionality or 90% of the Arduino API available to you) place your functions. In the .h file place prototypes for them. I am in the habit of adding the extern keyword, ...


3

You can't see non-static member variables in a class like that. The closest is to make it static, which means you get one and only one copy of the variable across all instances of your class: class ClassB; class ClassA { public: int x; void foo(); }; class ClassB { public: static int bfoo; }; void ClassA::foo() { x = ...


3

Classes are used to hide away the complexity of a certain task. The user of the class should not worry about how the library exactly works in the inside, but instead only worry about how it should be used. In many cases directly writing to a member variable can be not only convenient, but hurtful if a wrong value is set. If you instead use a function to set ...


Only top voted, non community-wiki answers of a minimum length are eligible