-
Notifications
You must be signed in to change notification settings - Fork 77
Changes needed to sketches
Unfortunately, there are times when changes are needed to sketches in order to make them work in the Pi Arduino environment. These restrictions / changes may be altered in the future but for now, here is a list of the known problems.
###The class constructor problem ... Imagine that we have a sketch that looks like:
MyClass myInstance;
void setup() {
myInstance.doSomething();
}
We see this often enough. For example, in LiquidCrystal
we find:
LiquidCrystal lcd(1,2,3 ...);
void setup() {
lcd.write("Hello World!\n");
}
The problem is that the instance of the class is created before initialization of the Arduino environment. If the class constructor assumes that the environment is in place (for example because it wishes to use pinMode(pin)
) it will fail. The workaround is to not use implicit construction of class instances but instead create them within setup.
MyClass *pMyInstance;
void setup() {
pMyInstance = new MyClass();
pMyInstance->doSomething();
}
This way we can be assured that the construction of the instance will have happened after environment initialization.