Skip to content

Added functionality to measure resistance #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const int VERSION = 0x00000001;
const float TEMPERATURE_CALIBRATION = -5.0;

#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8")
#define RESISTANCE_PIN A0
#define VOLTAGE_BUFFER_SIZE 16

//#define DEBUG 0

Expand All @@ -26,8 +28,12 @@ BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("001
BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID("0017"), BLENotify);
BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int));
BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify);
BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify);

byte voltageBufferIndex = 0;
bool voltageBufferFilled = false;
short soundSampleBuffer[256];
short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];

void onPDMdata() {
// query the number of bytes available
Expand All @@ -45,6 +51,23 @@ uint16_t getSoundAverage() {
return sqrt(avg);
}

void readVoltage() {
voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN);
if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE - 1) {
voltageBufferFilled = true;
}
voltageBufferIndex = (++voltageBufferIndex) % VOLTAGE_BUFFER_SIZE;
}

uint16_t getVoltageAverage() {
uint16_t avg = 0;
byte upperBound = voltageBufferFilled ? VOLTAGE_BUFFER_SIZE : voltageBufferIndex;
for (int i = 0; i < upperBound; i++) {
avg += voltageSampleBuffer[i];
}
return avg / upperBound;
}

// String to calculate the local and device name
String name;
unsigned long lastNotify = 0;
Expand Down Expand Up @@ -75,6 +98,8 @@ void setup() {

delay(2000);

pinMode(RESISTANCE_PIN, INPUT); // Used for reading resistance

if (!APDS.begin()) {
printSerialMsg("Failed to initialized APDS!");
blinkLoop();
Expand Down Expand Up @@ -142,6 +167,7 @@ void setup() {
service.addCharacteristic(proximityCharacteristic);
service.addCharacteristic(colorCharacteristic);
service.addCharacteristic(soundPressureCharacteristic);
service.addCharacteristic(resistanceCharacteristic);

versionCharacteristic.setValue(VERSION);

Expand Down Expand Up @@ -213,4 +239,11 @@ void updateSubscribedCharacteristics() {
float pressure = BARO.readPressure();
pressureCharacteristic.writeValue(pressure);
}

if(resistanceCharacteristic.subscribed()){
readVoltage();
uint16_t measuredValue = getVoltageAverage();
float voltageRatio = 1024.0f / measuredValue;
resistanceCharacteristic.writeValue(voltageRatio);
}
}