single_sensor.ino example

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>
6 *
7 * @brief An example using only sensor functions and no logging.
8 *
9 * See [the walkthrough page](@ref example_single_sensor) for detailed
10 * instructions.
11 *
12 * @m_examplenavigation{example_single_sensor,}
13 * ======================================================================= */
14
15// ==========================================================================
16// Include the base required libraries
17// ==========================================================================
18/** Start [includes] */
19// The Arduino library is needed for every Arduino program.
20#include <Arduino.h>
21
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>
25
26// Include the main header for ModularSensors
27#include <ModularSensors.h>
28/** End [includes] */
29
30// ==========================================================================
31// Board setup info
32// ==========================================================================
33/** Start [sketch_info] */
34// The name of this program file
35const char* sketchName = "single_sensor.ino";
36
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] */
41
42
43// ==========================================================================
44// Set up the sensor object
45// ==========================================================================
46/** Start [sensor] */
47#include <sensors/MaxBotixSonar.h>
48
49// Create a reference to the serial port for the sonar
50HardwareSerial& sonarSerial = Serial1; // Use hardware serial if possible
51
52const int8_t SonarPower = 22; // excite (power) pin
53const int SonarTrigger = -1; // Trigger pin
54
55// Create a new instance of the sonar sensor;
56MaxBotixSonar sonar(sonarSerial, SonarPower, SonarTrigger);
57
58// Create a new instance of the range variable;
59MaxBotixSonar_Range sonar_range(&sonar);
60/** End [sensor] */
61
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
65// bottom
66float calcDepth(void) {
67 float mountHeight = 5000;
68 float sonarRange = sonar_range.getValue();
69 return mountHeight - sonarRange;
70}
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] */
79
80
81// ==========================================================================
82// Working Functions
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);
90 delay(75);
91 digitalWrite(greenLED, LOW);
92 digitalWrite(redLED, HIGH);
93 delay(75);
94 }
95 digitalWrite(redLED, LOW);
96}
97/** End [working_functions] */
98
99
100// ==========================================================================
101// Arduino Setup Function
102// ==========================================================================
103/** Start [setup] */
104void setup() {
105 // Start the primary serial connection
106 Serial.begin(serialBaud);
107
108 // Print a start-up note to the first serial port
109 Serial.print(F("Now running "));
110 Serial.println(sketchName);
111
112 Serial.print(F("Using ModularSensors Library version "));
113 Serial.println(MODULAR_SENSORS_VERSION);
114
115 // Start the stream for the sonar
116 sonarSerial.begin(9600);
117
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
122 greenredflash();
123
124 // Print a start-up note to the first serial port
125 Serial.println(F("Single Sensor Example - Sonar Ranging"));
126
127 // Set up the sensor
128 sonar.setup();
129}
130/** End [setup] */
131
132
133// ==========================================================================
134// Arduino Loop Function
135// ==========================================================================
136/** Start [loop] */
137void loop() {
138 // Turn on the LED to show we're taking a reading
139 digitalWrite(greenLED, HIGH);
140
141 // Send power to the sensor
142 sonar.powerUp();
143
144 // Wake up the sensor
145 sonar.wake();
146
147 // Update the sensor value
148 sonar.update();
149
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());
155
156 // Put the sensor back to sleep
157 sonar.sleep();
158
159 // Cut the sensor power
160 sonar.powerDown();
161
162 // Turn off the LED to show we're done with the reading
163 digitalWrite(greenLED, LOW);
164
165 // Wait for the next reading
166 delay(5000);
167}
168/** End [loop] */