12 votes

Attach the Arduino ISR function to the class member

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 ...
Nick Gammon's user avatar
  • 38.2k
7 votes
Accepted

Create New Serial Class Inheriting from Stream

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 ...
Juraj's user avatar
  • 18.1k
6 votes
Accepted

"undefined reference to" - C++

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 ...
frarugi87's user avatar
  • 2,721
6 votes
Accepted

Use and definition of a destructor class

Destructors, despite their name, are not used to destroy the object. They are, instead, a "hook" that is called when the object is destroyed. Much like the "constructor" is called ...
Majenko's user avatar
  • 105k
5 votes
Accepted

Pass class's public function as an argument to external command

Interrupt Service Routine (ISR) outside a class Let's consider a simple use of interrupts: volatile bool switchChanged; void switchPressed () { switchChanged = true; } // end of ...
Nick Gammon's user avatar
  • 38.2k
4 votes
Accepted

How do I correctly use enum within a class?

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 ...
sekdiy's user avatar
  • 399
4 votes
Accepted

Why to store a function parameter value in a class private variable?

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 ...
Michel Keijzers's user avatar
4 votes

Trying to understand Classes

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 ...
jose can u c's user avatar
  • 6,974
4 votes
Accepted

Use ISRs inside a library more elegantly

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 ...
Edgar Bonet's user avatar
  • 43.1k
4 votes

Instances of a class inside another class - is there a way to control the amount?

You can use a template class to provide compile time parameters to your class to tune it. This is most often used in the context of providing a type for the class to operate on. However it's not ...
Majenko's user avatar
  • 105k
4 votes
Accepted

Instances of a class inside another class - is there a way to control the amount?

As pointed out in a comment on your original question, a template is likely the best solution. Templates allow you to write highly flexible code. I won't explain why; that's something that goes a ...
CharlieHanson's user avatar
3 votes
Accepted

Create a library without a class?

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++ ...
Majenko's user avatar
  • 105k
3 votes
Accepted

Mysterious behavior when using objects

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 ...
Charles Vorbach's user avatar
3 votes

Call class B from Class A

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 ...
Majenko's user avatar
  • 105k
3 votes
Accepted

Arduino classes, can't get property value

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 = ...
Delta_G's user avatar
  • 3,270
3 votes

Arduino Libraries: Declaring variables as public?

The Arduino ecosystem is meant mostly for beginners. This often means that people with very little knowledge will be using it and will often try random things to make stuff work. (and once they found ...
ratchet freak's user avatar
3 votes
Accepted

Passing HardwareSerial and SoftwareSerial as a Class Property

A base type for Arduino hardware serial classes, SoftwareSerial and other software serial classes and some other Arduino classes is Stream (reference). Stream.h on GitHub class CustomSerial { ...
Juraj's user avatar
  • 18.1k
3 votes
Accepted

give a class's function name as a parameter

CRGB::Red, etc, are just numbers. You need to treat them as such. uint32_t colorPallette[2] = { CRGB::Red, CRGB::Blue }; And then: leds[0] = colorPallete[0]; From pixeltypes.h: /// Predefined RGB ...
Majenko's user avatar
  • 105k
3 votes

AttachInterrupt - fail to initiate inside a class

Firstly, if detection_door is a non-static member function, then C++ simply has no such expression syntax as just detection_door. Such functions have to either be called (with () operator) or be taken ...
AnT stands with Russia's user avatar
3 votes
Accepted

Having an issue accessing values from a pointer

As Majenko mentioned in the comment, you created new local arrays in the constructor, but never put anything in the member arrays of the classes. They have the same name, but are not the same variable....
gfrodo's user avatar
  • 46
3 votes

How do arrays work?

BUZZER_NOTE notes[]; //-- Funny how the same declaration tried below DOES work if it's inside a class It doesn't really. C++ language prohibits that. It is just a non-standard quirk of GCC compiler ...
AnT stands with Russia's user avatar
3 votes
Accepted

Interrupts inside a class attaching to the function of the class

I have a post about this problem. Basically you cannot attach a class function with attachInterrupt because of the hidden "this" pointer which needs to be supplied to a (non-static) class function, ...
Nick Gammon's user avatar
  • 38.2k
3 votes
Accepted

Public variable in class not incrementing

It's difficult to pin down the exact details as to why the problem it's manifesting exactly the way it is. But, in short, it's fair to say you're running out of memory. First, just a note: The code I'...
timemage's user avatar
  • 5,231
3 votes
Accepted

Passing a method pointer

The easiest method I know to do this would be to use a "C"-style wrapper for the member function call. This is in fact done to interface C callbacks with C++ objects. You could do it like ...
Mat's user avatar
  • 464
3 votes

Failure using references variables in classes

I'm not sure what value this is going to have for Arduino users, but here goes. Your question seem to be an over-complicated form of this the following: My code: struct MessageTFT {uint8_t txt_size = ...
timemage's user avatar
  • 5,231
3 votes
Accepted

How do I properly use an ISR inside a class definition?

You have three different variables named stAng here: a static member of the Bike class, which has been declared but not defined, and is never used a local variable of the Bike::incStAng() method a ...
Edgar Bonet's user avatar
  • 43.1k
2 votes

Possible Arduino Uno c++ compiler bug?

That means the test failures were caused by some subtle difference between the Arduino Uno and the Mega 2560—either in the hardware or in the compiler. Not a subtle difference - a big difference. ...
Majenko's user avatar
  • 105k
2 votes
Accepted

using enums in functions

The solution was to move the enum declarations to a header file, due to the fact this was a VM add-in issue which will be resolved once they've implemented arduino version 1.6.8. Adding this code to ...
dinotom's user avatar
  • 360
2 votes
Accepted

Object can't get the right value of its property

My guess: controller_right.Motor = motor_right; makes a copy of motor_right. So you don't have the same class instance. Basically Controller controller_right; calls default constructor which includes ...
KIIV's user avatar
  • 4,752
2 votes

Dynamicly sized array as a class member

Many programmers have a disjoint between a pointer and an array. Here's an explanation. Here is a variable of type char, initialised with the value 'A': char c = 'A'; 'A' has the ASCII value of 65, ...
John Burger's user avatar
  • 1,875

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