A custom structure in C++ (and various other languages) which can contain member data and functions. An instance of a class is called an object.
Questions tagged [class]
122 questions
22
votes
5 answers
What overheads and other considerations are there when using a struct vs a class?
C on embedded systems has traditionally use structs to hold structured data.
Arduino brings C++ to the table, so we can use classes instead.
Lets say we have two different data structures which could be considered very similar:
typedef struct
{
…
Cybergibbons
- 5,420
- 7
- 34
- 51
21
votes
3 answers
Classes and objects: how many and which file types do I actually need to use them?
I have no previous experience with C++ or C, but know how to program C# and am learning Arduino. I just want to organize my sketches and am quite comfortable with the Arduino language even with its limitations, but I really would like to have an…
heltonbiker
- 503
- 7
- 21
7
votes
1 answer
Arduino Servo won't move when using classes
I'm trying to make a class work with Arduino, however it doesn't seem to trigger properly. The code is compiling perfectly, and it's supposed to control the leg of an hexapod. Also, the example sweep works on this servo, so no problem here. However,…
Francisco Presencia
- 203
- 2
- 7
6
votes
2 answers
"undefined reference to" - C++
Hi fellow programmers,
I started to program Arduino a little deeply and popped out the following error when I was writing a class to read the pulses of a Hall effect flow rate sensor:
SensorDeVazao.cpp.o: In function…
Bruno Lobo
- 167
- 1
- 2
- 9
4
votes
2 answers
incompatible types in assignment of 'int*' to 'int [0]'
I was coding a class for "a kind of gun". I have this code for the classes:
Gun.h:
#ifndef Gun_h
#define Gun_h
#include "Arduino.h"
class Gun
{
public:
Gun(String identifier, String name, int teamIRCodes[]);
String getIdentifier();
…
Arnyminer Z
- 197
- 1
- 2
- 8
4
votes
2 answers
using enums in functions
Given the following enums
enum RelayState { RELAY_OFF = HIGH, RELAY_ON = LOW };
enum class CrossingZoneState: uint8_t {
CROSSINGZONE_CLEAR = 0, // no train in crossing area, also initialized state
CROSSINGZONE_OCCUPIED = 1, // train detected…
dinotom
- 360
- 2
- 6
- 18
3
votes
2 answers
Attach the Arduino ISR function to the class member
I intended to use a timer interrupt for limited instances of a class in my ESP32 Arduino project. My first code conception was as follows:
portEXIT_CRITICAL_ISR(&lock0);
}
hw_timer_t * timer[3] = NULL;
class myClass
{
private:
…
Hamid s k
- 143
- 1
- 4
3
votes
1 answer
Class is not tracking changes to a private field
I have a program where a device registers itself with a command object. The command object is losing track of the registrations. The following program repeats "0 devices registered" even though when it initializes it displays "Devices registered 1".…
CarbonMan
- 233
- 6
- 15
2
votes
1 answer
How to pass Encoder object to constructor of a different class
I am trying to create a class (Arm) that controls an arm. The arms have 2 motors, each with an encoder, so I pass 4 integers for the pins and I am trying to pass 2 pointers (one for each encoder object).
the .ino file is:
#include "Arm.h"
#include…
EricWeissman
- 31
- 1
2
votes
1 answer
Use ISRs inside a library more elegantly
At the moment I try to make a library which uses attachInterrupt(?,?,RISING) to read a sensor. The article of Nick Gammon on Calling an ISR from a class helped me a lot, but his "Glue routines" get pretty ugly with more than 30 potential Interrupt…
Adrian Immer
- 31
- 4
2
votes
1 answer
AttachInterrupt - fail to initiate inside a class
I'm trying to create a class that uses attachInterrupt, but get this error msg:
In member function 'void ledDoor::attach()':
ClosetLedStrip:31:82: error: invalid use of non-static member function
…
guyd
- 1,049
- 2
- 26
- 61
2
votes
0 answers
Using pointer to a Global function
I am trying to store and use a pointer to a global function.
The function global_f returns a String and takes an object as parameter:
String global_f(Request r){
// ...
return "This is it";
}
I have also declared a type:
typedef String…
Kostas C.
- 21
- 2
2
votes
0 answers
Millis() is Not Working
I have a sketch that uses two timers using millis() from a library I created to keep time. The timers seem to be working correctly, but when I print the start time of each timer, it is clearly wrong.
E.g. the timer is set to run for 3 seconds. When…
Tarlyo
- 21
- 2
2
votes
1 answer
C++ Undefined reference to 'Class:Function()'
I have these files:
Project.ino
Sensor.h
Sensor.cpp
IMU/L3G.h
IMU/L3G.cpp
Sensor.h
#ifndef Sensor_h
#define Sensor_h
#include
#include "IMU/L3G.h"
#define CUTOFF_FREQ 30
#define FREQ 400
class Sensor {
public:
Sensor();
…
user3552818
- 21
- 1
- 2
2
votes
2 answers
Arduino Libraries: Declaring variables as public?
How come I never see any Arduino Library expose some of it's internal variables as public in the header file? I only see functions which are declared as public, while every variable is declared private.
Background: I want to write my first Arduino…
Henry
- 123
- 3