It might worth a look at the (under documentation) library I have schematized and made.
It's called ArduinoSensors, and is made to serve as a pattern for future developers, and also to simplify the usage of classes and let code even more robust with Thread concepts.
I have implemented lots of sensors already that I used. I have also extended it and implemented my own classes for very specific purpose.
The repository can be found here: GitHub - ivanseidel/ArduinoSensors: A Library of Libraries of Sensors. Infrared, Ultrasonic, Compass and many others, ready to work with ArduinoThread and fully object oriented
Some cool classes that are included, are DigitalOut, DigitalIn, AnalogIn and AnalogVoltage.
To show how they work (for example):
DigitalIn myButton(9);
// Check if Pin is HIGH
if (myButton)...
// Check if Pin is LOW
if (myButton == LOW)...
// Reads it's value
boolean val = myButton.readValue();
DigitalOut myLed(13);
// Turns LED on
myLed = HIGH;
myLed.turn(HIGH);
myLed.turnOn();
// Turns LED off
myLed = LOW;
myLed.turn(LOW);
myLed.turnOff();
Or something complex, using Async sensor fetch in a Thread controller:
// Create a new ThreadController that will run always
// (means it will check if it's threads should be runned everytime)
ThreadController threads(0);
// Create some sensors
DistanceInterface *myDist1 = new SharpLong(A0);
DistanceInterface *myDist2 = new SharpShort(A1);
TemperatureInterface *thermometer = new MLX90614(0x32);
AngleInterface *myCompass = new HMC6352();
// Each sensor has it's interval set to minimum possible
// or something good to work with (20ms - 70ms).
// But they ARE Thread objects, so you can make use of
// it's methods:
// Compass should be read every 60ms
myCompass.setInterval(60);
// Add to the thread controller
threads.add(&myDist1);
threads.add(&myDist2);
threads.add(&thermometer);
threads.add(&myCompass);
// Now, just call threads.run(); to automatically run what is needed
// (perhaps in a Timer Callback? or in Loop.. whatever)
loop(){
threads.run();
// Get the most recend value WITHOUT reading the sensor
double currentAngle = myCompass->getAngle();
float distance = myDist1->getDistance();
[...]
}
Anyway, you can use it the way you want. It is not 100% documented since it's a LOT of work to do, but I'm shure it can help others.