I am a beginner in Arduino and am currently trying out the classes. However, when compiling the code I get this error:
C:\Users\adria\Documents\Arduino\test scetches\sketch_apr13a\sketch_apr13a.ino:3:9: error: expected initializer before 'leftLED'
MyClass leftLED(13);*
^~~~~~~*
C:\Users\adria\Documents\Arduino\Testsketches\sketch_apr13a\sketch_apr13a.ino: In function 'void loop()': C:\Users\adria\Documents\Arduino\test scetches\sketch_apr13a\sketch_apr13a.ino:11:3: error: 'leftLED' was not declared in this scope
leftLED.myFunction(1000);*
^~~~~~~*
Exit status 1
Compilation error: expected initializer before 'leftLED'
Here is my Code:
MyClass.h
#ifndef MyClass_h
#define MyClass_h
#include "Arduino.h"
class MyClass {
public:
MyClass(int pin);
void myFunction(int blinkrate);
private:
int _pin;
}
#endif
You are missing a semi Colon at the end of the class declaration in the .h
class MyClass {
public:
MyClass(int pin);
void myFunction(int blinkrate);
private:
int _pin;
}; // <==== add a semi colon here
Side note:
It’s a bad idea to set the pinMode in the constructor as the constructor (for global variables) is called very early in the code before main() and setup() are called. On some boards that means the arduino configuration process could undo your pinMode().
Typically one add a begin() method where this is handled and you call begin() in your instance in the setup.
Also use initializer lists for your variables in the constructor it’s the way to go
class MyClass {
public:
MyClass(byte pin) : _pin(pin) {} // no need to define it in the .cpp it does nothing else
void begin();
void myFunction(int blinkrate);
private:
byte _pin;
};
Then, is it also probable that pin assignment task (13 ----> ledPin) be undone?
class DigitalIo //Class Name (DigitalIO) is declared/created using class keyword
{
private:
int ledPin; //variable can only be accessed by the functions under public: specifier
public:
DigitalIo(int powerpin): ledPin(powerpin) {} //inline constructor to initialize ledPin
void ioDir(); //member function
void ledON();
void ledOFF();
};
DigitalIo led(13); //led is called object of type DigitalIO
void setup()
{
Serial.begin(9600);
led.ioDir(); //setting up the direction of IO line(13) as output
}
..........
why would that be? it's an instance variable so it will be assigned and keep the value 13.
this class seems to offer an ioDir() function that is probably equivalent to the begin() function I was referring to
the issue is more if you do a pinMode() call in the constructor as the initialisation phase of the board in main() could undo it on some boards (see this bug for example with some button libraries)
I mean --
for any variable (to be holding a numerical value), almost all the pupil immediately declare an int type variable as if they will never encounter any negative number in their process. For example, for the unipolar ADC of the Arduino UNO, it is very common to see this declarartion:
Not sure what a direct value is in c++
The doc just forgot to mention the type … as they say number you can assume they mean some sort of numerical value may be on the very secret KingsCross board the is a pin Nine and Three-Quarters and so you can secretly do