while working on my line follower robot project, I had to make a class for reading the sensors of my line detector.
The idea is to read all sensors and send a grouped message to the main program in the form of an int.
I thought I'd make it as generic as possible for any sensor array that would use one digital input per sensor.
Here is the code :
/// <summary> /// This class represents the reflectance array - or line detector. /// I made it with the Parallax 8-reflectance sensors array in mind, but I'm trying to make it /// as generic as possible. /// </summary> class CLineDetector { private InputPort[] sensorArray; /// <summary> /// Represents the arrays of inputs used in the Netduino for the sensor array /// </summary> public InputPort[] SensorArray { get { return sensorArray; } set { value = sensorArray; } } private int arrayLength; /// <summary> /// Represents the numbers of sensors in the Line detector. /// </summary> public int ArrayLength { get { return arrayLength; } set { value = arrayLength; } } public CLineDetector() { } public CLineDetector(InputPort[] sensorarray) { sensorArray = sensorarray; } /// <summary> /// Reads all sensors and returns their grouped readings. /// </summary> /// <returns>A reading of the whole array</returns> public int ReadAll() { int iRes = 0; int iCount, iReading; //Reads all the sensors and put them all in a single int for (iCount = 0; iCount < sensorArray.Length; iCount++) { iReading = sensorArray[iCount].Read() ? 1 : 0; iRes += iReading << iCount; } // Put them in form return iRes; } }
I'm posting it for critics as to the generic-ness of the class, and also for two specific questions :
- It seems to me the SensorArray member really doesn't need to be public, since the rest of the program will only need to know the end result and the number of sensors. What do you think?
- The time factor, I didn't use any "sleep" in my "ReadAll" method, thinking it's more the main program's job. Am I right?
Thanks you for comments and critics :-).