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
...
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 = ...
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 ...
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 ...
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 {
...
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 ...
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 ...
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....
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 ...
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, ...
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'...
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 ...
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 = ...
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 ...
3
votes
Accepted
Class private variable values not accessible in public function
In your configure function:
void Motor::Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* SerPort)
{
uint16_t _inA_pin = inA_pin;
uint16_t _inB_pin = inB_pin;
...
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 ...
2
votes
Accepted
How come `MyClass.MyStaticMethod()` doesn't work, but `Serial.begin()` does?
. is used to call a method on a class instance whereas :: is used to call a static method within a class itself.
There is no Serial class - Serial is merely an instance of the HardwareSerial class.
#...
2
votes
Accepted
Run 'Serial.begin(9600)' before loop()
You can make your current object a pointer so it isn't instantiated:
ClassA *classA;
Then create an object with new:
classA = new ClassA();
The constructor is then only called when the object is ...
2
votes
Run 'Serial.begin(9600)' before loop()
In order to be sure that your classA object doesn't use the Serial.println() function before the baudrate is defined, it is necessary to add functions in your class ClassA and to inform from setup() ...
2
votes
Accepted
Function, struct or class?
Are you trying to use the same function for different arrays at different times, or are you trying to use a function that handles four different arrays at the same time?
If the former, you'd write ...
2
votes
Arduino classes, can't get property value
You have redeclared the class member fields in your constructor:
class MKSStepperMotors {
public:
int _dirPin;
int _stepPin;
int _enablePin;
MKSStepperMotors(int dirPin, int ...
2
votes
Accepted
Is there any way to group variables like group.variable as you can in Java/Delphi
What you are looking for is a struct:
struct foo {
uint8_t Ux;
uint8_t Uy;
uint8_t Yz;
};
Then:
struct foo A;
struct foo B;
And:
A.Ux = 3;
B.Uz = 48;
int c = A.Ux + B.Uz;
When ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
class × 121c++ × 45
library × 24
programming × 14
arduino-uno × 10
interrupt × 9
pointer × 9
arduino-mega × 8
arduino-ide × 8
serial × 6
timers × 6
variables × 6
led × 5
esp32 × 4
isr × 4
functions × 4
callbacks × 4
object-oriented × 4
array × 3
uart × 3
neopixel × 3
struct × 3
esp8266 × 2
arduino-nano × 2
lcd × 2