An example using only sensor functions and no logging.
An example using only sensor functions and no logging.=========================================================================
See the walkthrough page for detailed instructions.
1/** =========================================================================
2 * @example{lineno} single_sensor.ino
3 * @copyright Stroud Water Research Center
4 * @license This example is published under the BSD-3 license.
5 * @author Sara Geleskie Damiano <sdamiano@stroudcenter.org>
7 * @brief An example using only sensor functions and no logging.
9 * See [the walkthrough page](@ref example_single_sensor) for detailed
12 * @m_examplenavigation{example_single_sensor,}
13 * ======================================================================= */
15// ==========================================================================
16// Include the base required libraries
17// ==========================================================================
18/** Start [includes] */
19// The Arduino library is needed for every Arduino program.
22// EnableInterrupt is used by ModularSensors for external and pin change
23// interrupts and must be explicitely included in the main program.
24#include <EnableInterrupt.h>
26// Include the main header for ModularSensors
27#include <ModularSensors.h>
30// ==========================================================================
32// ==========================================================================
33/** Start [sketch_info] */
34// The name of this program file
35const char* sketchName = "single_sensor.ino";
37const int32_t serialBaud = 115200; // Baud rate for debugging
38const int8_t greenLED = 8; // Pin for the green LED
39const int8_t redLED = 9; // Pin for the red LED
40/** End [sketch_info] */
43// ==========================================================================
44// Set up the sensor object
45// ==========================================================================
47#include <sensors/MaxBotixSonar.h>
49// Create a reference to the serial port for the sonar
50HardwareSerial& sonarSerial = Serial1; // Use hardware serial if possible
52const int8_t SonarPower = 22; // excite (power) pin
53const int SonarTrigger = -1; // Trigger pin
55// Create a new instance of the sonar sensor;
56MaxBotixSonar sonar(sonarSerial, SonarPower, SonarTrigger);
58// Create a new instance of the range variable;
59MaxBotixSonar_Range sonar_range(&sonar);
62/* Start [calculated variables] */
63// Create a function to calculate the water depth from the sonar range
64// For this example, we'll assume that the sonar is mounted 5m above the stream
66float calcDepth(void) {
67 float mountHeight = 5000;
68 float sonarRange = sonar_range.getValue();
69 return mountHeight - sonarRange;
71// Create a calculated variable for the water depth
72// Variable calcVar(functionName, VariableName, VariableUnit, Resolution, UUID,
73// Code); VariableName must be a value from
74// http://vocabulary.odm2.org/variablename/ VariableUnit must be a value from
75// http://vocabulary.odm2.org/units/
76Variable waterDepth(calcDepth, 0, "waterDepth", "millimeter", "sonarDepth",
77 "12345678-abcd-1234-ef00-1234567890ab");
78/** End [calculated_variables] */
81// ==========================================================================
83// ==========================================================================
84/** Start [working_functions] */
85// Flashes to Mayfly's LED's
86void greenredflash(int numFlash = 4) {
87 for (int i = 0; i < numFlash; i++) {
88 digitalWrite(greenLED, HIGH);
89 digitalWrite(redLED, LOW);
91 digitalWrite(greenLED, LOW);
92 digitalWrite(redLED, HIGH);
95 digitalWrite(redLED, LOW);
97/** End [working_functions] */
100// ==========================================================================
101// Arduino Setup Function
102// ==========================================================================
105 // Start the primary serial connection
106 Serial.begin(serialBaud);
108 // Print a start-up note to the first serial port
109 Serial.print(F("Now running "));
110 Serial.println(sketchName);
112 Serial.print(F("Using ModularSensors Library version "));
113 Serial.println(MODULAR_SENSORS_VERSION);
115 // Start the stream for the sonar
116 sonarSerial.begin(9600);
118 // Set up pins for the LED's
119 pinMode(greenLED, OUTPUT);
120 pinMode(redLED, OUTPUT);
121 // Blink the LEDs to show the board is on and starting up
124 // Print a start-up note to the first serial port
125 Serial.println(F("Single Sensor Example - Sonar Ranging"));
133// ==========================================================================
134// Arduino Loop Function
135// ==========================================================================
138 // Turn on the LED to show we're taking a reading
139 digitalWrite(greenLED, HIGH);
141 // Send power to the sensor
144 // Wake up the sensor
147 // Update the sensor value
150 // Print the sonar result
151 Serial.print("Current sonar range: ");
152 Serial.println(sonar_range.getValueString());
153 Serial.print("Calculated water depth: ");
154 Serial.println(waterDepth.getValueString());
156 // Put the sensor back to sleep
159 // Cut the sensor power
162 // Turn off the LED to show we're done with the reading
163 digitalWrite(greenLED, LOW);
165 // Wait for the next reading