|
| 1 | +#include <Arduino_LSM6DSOX.h> |
| 2 | +#include <PDM.h> |
| 3 | + |
| 4 | +#include <ArduinoBLE.h> |
| 5 | + |
| 6 | +const int VERSION = 0x00000001; |
| 7 | + |
| 8 | +#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8") |
| 9 | +#define RESISTANCE_PIN A0 |
| 10 | +#define VOLTAGE_BUFFER_SIZE 16 |
| 11 | + |
| 12 | +//#define DEBUG 0 |
| 13 | + |
| 14 | +BLEService service (SCIENCE_KIT_UUID("0000")); |
| 15 | +BLEUnsignedIntCharacteristic versionCharacteristic (SCIENCE_KIT_UUID("0001"), BLERead); |
| 16 | +BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID("0011"), BLENotify, 3 * sizeof(float)); |
| 17 | +BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID("0012"), BLENotify, 3 * sizeof(float)); |
| 18 | +BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify); |
| 19 | +BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify); |
| 20 | + |
| 21 | +byte voltageBufferIndex = 0; |
| 22 | +bool voltageBufferFilled = false; |
| 23 | +short soundSampleBuffer[256]; |
| 24 | +short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE]; |
| 25 | + |
| 26 | +void onPDMdata() { |
| 27 | + // query the number of bytes available |
| 28 | + int bytesAvailable = PDM.available(); |
| 29 | + |
| 30 | + // read into the sample buffer |
| 31 | + PDM.read(soundSampleBuffer, bytesAvailable); |
| 32 | +} |
| 33 | + |
| 34 | +uint16_t getSoundAverage() { |
| 35 | + uint32_t avg = 0; |
| 36 | + for (int i = 0; i < sizeof(soundSampleBuffer)/sizeof(soundSampleBuffer[0]); i++) { |
| 37 | + avg += soundSampleBuffer[i]*soundSampleBuffer[i]; |
| 38 | + } |
| 39 | + return sqrt(avg); |
| 40 | +} |
| 41 | + |
| 42 | +void readVoltage() { |
| 43 | + voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN); |
| 44 | + if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE - 1) { |
| 45 | + voltageBufferFilled = true; |
| 46 | + } |
| 47 | + voltageBufferIndex = (++voltageBufferIndex) % VOLTAGE_BUFFER_SIZE; |
| 48 | +} |
| 49 | + |
| 50 | +uint16_t getVoltageAverage() { |
| 51 | + uint16_t avg = 0; |
| 52 | + byte upperBound = voltageBufferFilled ? VOLTAGE_BUFFER_SIZE : voltageBufferIndex; |
| 53 | + for (int i = 0; i < upperBound; i++) { |
| 54 | + avg += voltageSampleBuffer[i]; |
| 55 | + } |
| 56 | + return avg / upperBound; |
| 57 | +} |
| 58 | + |
| 59 | +// String to calculate the local and device name |
| 60 | +String name; |
| 61 | +unsigned long lastNotify = 0; |
| 62 | + |
| 63 | +void printSerialMsg(const char * msg) { |
| 64 | + #ifdef DEBUG |
| 65 | + if (Serial) { |
| 66 | + Serial.println(msg); |
| 67 | + } |
| 68 | + #endif |
| 69 | +} |
| 70 | + |
| 71 | +void blinkLoop() { |
| 72 | + while (1) { |
| 73 | + digitalWrite(LED_BUILTIN, HIGH); |
| 74 | + delay(500); |
| 75 | + digitalWrite(LED_BUILTIN, LOW); |
| 76 | + delay(500); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void setup() { |
| 81 | + #ifdef DEBUG |
| 82 | + Serial.begin(9600); |
| 83 | + while (!Serial); |
| 84 | + Serial.println("Started"); |
| 85 | + #endif |
| 86 | + |
| 87 | + delay(2000); |
| 88 | + |
| 89 | + pinMode(RESISTANCE_PIN, INPUT); // Used for reading resistance |
| 90 | + |
| 91 | + if (!IMU.begin()) { |
| 92 | + printSerialMsg("Failed to initialize IMU!"); |
| 93 | + blinkLoop(); |
| 94 | + } |
| 95 | + |
| 96 | + PDM.onReceive(onPDMdata); |
| 97 | + if (!PDM.begin(1, 16000)) { |
| 98 | + printSerialMsg("Failed to start PDM!"); |
| 99 | + blinkLoop(); |
| 100 | + } |
| 101 | + |
| 102 | + if (!BLE.begin()) { |
| 103 | + printSerialMsg("Failed to initialize BLE!"); |
| 104 | + blinkLoop(); |
| 105 | + } |
| 106 | + |
| 107 | + String address = BLE.address(); |
| 108 | + #ifdef DEBUG |
| 109 | + if (Serial) { |
| 110 | + Serial.print("address = "); |
| 111 | + Serial.println(address); |
| 112 | + } |
| 113 | + #endif |
| 114 | + address.toUpperCase(); |
| 115 | + |
| 116 | + name = "BLE Sense - "; |
| 117 | + name += address[address.length() - 5]; |
| 118 | + name += address[address.length() - 4]; |
| 119 | + name += address[address.length() - 2]; |
| 120 | + name += address[address.length() - 1]; |
| 121 | + |
| 122 | + #ifdef DEBUG |
| 123 | + if (Serial) { |
| 124 | + Serial.print("name = "); |
| 125 | + Serial.println(name); |
| 126 | + } |
| 127 | + #endif |
| 128 | + |
| 129 | + BLE.setLocalName(name.c_str()); |
| 130 | + BLE.setDeviceName(name.c_str()); |
| 131 | + BLE.setAdvertisedService(service); |
| 132 | + |
| 133 | + service.addCharacteristic(versionCharacteristic); |
| 134 | + service.addCharacteristic(accelerationCharacteristic); |
| 135 | + service.addCharacteristic(gyroscopeCharacteristic); |
| 136 | + service.addCharacteristic(soundPressureCharacteristic); |
| 137 | + service.addCharacteristic(resistanceCharacteristic); |
| 138 | + |
| 139 | + versionCharacteristic.setValue(VERSION); |
| 140 | + |
| 141 | + BLE.addService(service); |
| 142 | + BLE.advertise(); |
| 143 | +} |
| 144 | + |
| 145 | +void loop() { |
| 146 | + BLE.poll(1000); |
| 147 | + while (BLE.connected()) { |
| 148 | + updateSubscribedCharacteristics(); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void updateSubscribedCharacteristics() { |
| 153 | + if (accelerationCharacteristic.subscribed()) { |
| 154 | + float acceleration[3]; |
| 155 | + if (IMU.accelerationAvailable() && IMU.readAcceleration(acceleration[0], acceleration[1], acceleration[2])) { |
| 156 | + accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (gyroscopeCharacteristic.subscribed()) { |
| 161 | + float gyroscope[3]; |
| 162 | + if (IMU.gyroscopeAvailable() && IMU.readGyroscope(gyroscope[0], gyroscope[1], gyroscope[2])) { |
| 163 | + gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if (soundPressureCharacteristic.subscribed()) { |
| 168 | + uint16_t sound = getSoundAverage(); |
| 169 | + soundPressureCharacteristic.writeValue(sound); |
| 170 | + } |
| 171 | + |
| 172 | + if(resistanceCharacteristic.subscribed()){ |
| 173 | + readVoltage(); |
| 174 | + uint16_t measuredValue = getVoltageAverage(); |
| 175 | + float voltageRatio = 1024.0f / measuredValue; |
| 176 | + resistanceCharacteristic.writeValue(voltageRatio); |
| 177 | + } |
| 178 | +} |
0 commit comments