Some Arduino workarounds.

One issue when using an existing Arduino library to implement a device, is where the instance declaration requires a parameter such as a pin. This can be an issue where, for example, the pin is only known at runtime. The workaround is to declare it as a pointer then when it is to instantiated at runtime, it is created with a new() call with the parameter as an argument. For example, the Grove Ultrasonic Ranger:

Sample code typically will define it thus

#define PIN 7
Ultransonic ultrasonic(PIN);

The pointer approach is:


Ultrasonic * ultrasonic;

void Setup(int SetupPin)
{
    ultrasonic = new Ultrasonic(SetupPin);
}

Then change code such as
ultrasonic.MeasureInMillimeters()
to
ultrasonic->MeasureInMillimeters()


 TopicSubtopic
   
 This Category Links 
Category:Softata Index:Softata
  Next: > Softata
<  Prev:   Softata