|
| 1 | +/** |
| 2 | + * @file nano33ble.ino |
| 3 | + * An example of using tcMenu with the Nano33BLE Sense. This example uses some of the sensors on-board the device |
| 4 | + * along with an I2C LCD backpack based display and a rotary encoder. Although this is pre-generated for you, you |
| 5 | + * can load the example's emf file into TcMenu Designer and take a look around or re-build it. |
| 6 | + * |
| 7 | + * You can get or adjust the pin configurations by loading the emf file into the designer |
| 8 | + */ |
| 9 | + |
1 | 10 | #include "nano33ble_menu.h" |
2 | 11 | #include "SensorManager.h" |
3 | 12 | #include "MotionDetection.h" |
4 | 13 | #include <AnalogDeviceAbstraction.h> |
5 | 14 |
|
| 15 | +// on the analog menu, we both have an analog input and an analog output (PWM). You can configure those pins here. |
6 | 16 | const int analogInputPin = A0; |
7 | 17 | const int pwmOutputPin = 2; |
8 | 18 |
|
| 19 | +// We work with analog input and output here, so we use an analog device to make it easier. It provides the ability |
| 20 | +// to treat analog values as floats between 0..1 on any supported platform. |
| 21 | +ArduinoAnalogDevice analogDevice; |
| 22 | + |
| 23 | +// We create a class extending Executable for the temprature, humidity, and pressure sensors that are built in |
9 | 24 | SensorManager sensorManager; |
| 25 | + |
| 26 | +// We create an event class extending BaseEvent to manage the motion detection |
10 | 27 | MotionDetection motionDetection; |
11 | | -ArduinoAnalogDevice analogDevice; |
12 | 28 |
|
13 | 29 | void setup() { |
| 30 | + // First we set up the analog pins |
| 31 | + analogDevice.initPin(pwmOutputPin, DIR_OUT); |
| 32 | + analogDevice.initPin(analogInputPin, DIR_IN); |
| 33 | + |
| 34 | + // and set up the menu itself, so it starts displaying and accepting input |
14 | 35 | setupMenu(); |
| 36 | + |
| 37 | + // then we initialise our sensor and motion detection and register with task manager. |
15 | 38 | sensorManager.initialise(); |
16 | 39 | motionDetection.initialise(); |
17 | | - analogDevice.initPin(pwmOutputPin, DIR_OUT); |
18 | | - analogDevice.initPin(analogInputPin, DIR_IN); |
| 40 | + taskManager.registerEvent(&motionDetection); |
| 41 | + taskManager.scheduleFixedRate(1, &sensorManager, TIME_SECONDS); |
19 | 42 |
|
| 43 | + // lastly we set up something simple to read from analog in |
20 | 44 | taskManager.scheduleFixedRate(100, [] { |
21 | 45 | menuAnalogReadingsInA0.setFloatValue(analogDevice.getCurrentFloat(analogInputPin)); |
22 | 46 | }); |
23 | 47 | } |
24 | 48 |
|
| 49 | +// All TaskManager sketches must call runLoop very often from the loop method, you should not use any delays. |
25 | 50 | void loop() { |
26 | 51 | taskManager.runLoop(); |
27 | | - |
28 | 52 | } |
29 | 53 |
|
| 54 | +// And something to change the PWM output when the PWM menu item changes |
30 | 55 | void CALLBACK_FUNCTION onPWMChanged(int id) { |
31 | 56 | auto newPwm = menuAnalogReadingsOutputPWM.getCurrentValue() / 100.0F; |
32 | 57 | analogDevice.setCurrentFloat(pwmOutputPin, newPwm); |
|
0 commit comments