|
| 1 | +/* |
| 2 | +SharpDistSensorArray.ino |
| 3 | +
|
| 4 | +Source: https://github.com/DrGFreeman/SharpDistSensor |
| 5 | +
|
| 6 | +MIT License |
| 7 | +
|
| 8 | +Copyright (c) 2018 Julien de la Bruere-Terreault <drgfreeman@tuta.io> |
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in all |
| 17 | +copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +SOFTWARE. |
| 26 | +*/ |
| 27 | + |
| 28 | +/* |
| 29 | +This example shows how to use the SharpDistSensor library with an array of |
| 30 | +multiple sensors. |
| 31 | +
|
| 32 | +See the library README for how to use pre-defined sensor models or custom fit |
| 33 | +functions. |
| 34 | +*/ |
| 35 | + |
| 36 | +#include <SharpDistSensor.h> |
| 37 | + |
| 38 | +// Define the number of sensors in the array as a constant |
| 39 | +const byte nbSensors = 2; |
| 40 | + |
| 41 | +// Window size of the median filter (odd number, 1 = no filtering) |
| 42 | +const byte medianFilterWindowSize = 5; |
| 43 | + |
| 44 | +// Define the array of SharpDistSensor objects |
| 45 | +SharpDistSensor sensorArray[] = { |
| 46 | + SharpDistSensor(A1, medianFilterWindowSize), // First sensor using pin A1 |
| 47 | + SharpDistSensor(A2, medianFilterWindowSize), // Second sensor using pin A2 |
| 48 | + // Add as many sensors as required |
| 49 | +}; |
| 50 | + |
| 51 | +// Alternatively, the array can be defined this way |
| 52 | +// SharpDistSensor sensorArray[nbSensors] = {{A1, medianFilterWindowSize}, |
| 53 | +// {A2, medianFilterWindowSize}}; |
| 54 | + |
| 55 | +// Define an array of integers that will store the measured distances |
| 56 | +uint16_t distArray[nbSensors]; |
| 57 | + |
| 58 | +void setup() { |
| 59 | + // Set some parameters for each sensor in array |
| 60 | + for (byte i = 0; i < nbSensors; i++) { |
| 61 | + sensorArray[i].setModel(SharpDistSensor::GP2Y0A710K0F_5V_DS); // Set sensor model |
| 62 | + // Set other parameters as required |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + // Read distance for each sensor in array into an array of distances |
| 68 | + for (byte i = 0; i < nbSensors; i++) { |
| 69 | + distArray[i] = sensorArray[i].getDist(); |
| 70 | + } |
| 71 | +} |
0 commit comments