Skip to content

Commit 07bdb11

Browse files
FUNCTIONAL CODE
Code has been reviewed and now functional and ready for design review. Going to make small changes to comments, but code is pretty ready to go.
1 parent 4e05b81 commit 07bdb11

File tree

11 files changed

+216
-199
lines changed

11 files changed

+216
-199
lines changed

Examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <Wire.h>
33

44
STHS34PF80_I2C mySensor;
5-
// STHS34PF80_SPI mySensor;
65

76
// Values to fill with presence and motion data
87
int16_t presenceVal = 0;
@@ -15,12 +14,16 @@ void setup()
1514
Serial.println("STHS34PF80 Example 1: Basic Readings");
1615

1716
// Begin I2C
18-
Wire.begin();
17+
if(Wire.begin() == false)
18+
{
19+
Serial.println("I2C Error - check I2C Address");
20+
while(1);
21+
}
1922

2023
// Establish communication with device
2124
if(mySensor.begin() == false)
2225
{
23-
Serial.println("Error"); // fix this print message
26+
Serial.println("Error setting up device - please check wiring.");
2427
while(1);
2528
}
2629

@@ -31,12 +34,13 @@ void loop()
3134
{
3235
bool dataReady = mySensor.getDataReady();
3336

37+
// Check whether sensor has new data - run through loop if data is ready
3438
if(dataReady == 1)
3539
{
3640
sths34pf80_tmos_func_status_t status;
3741
mySensor.getStatus(&status);
3842

39-
// If the flag is high, then read out the information
43+
// If presence flag is high, then print data
4044
if(status.pres_flag == 1)
4145
{
4246
// Presence Units: cm^-1
@@ -51,4 +55,65 @@ void loop()
5155
}
5256
}
5357

54-
}
58+
}
59+
60+
61+
62+
63+
64+
65+
66+
// #include "SparkFun_STHS34PF80_Arduino_Library.h"
67+
// #include <Wire.h>
68+
69+
// STHS34PF80_I2C mySensor;
70+
71+
// // Global Presence Value
72+
// int16_t presenceVal = 0;
73+
74+
// void setup()
75+
// {
76+
// Serial.begin(115200);
77+
// Serial.println("STHS34PF80 Example 5: Arduino Serial Plotter Presence Output");
78+
79+
// // Begin I2C
80+
// if(Wire.begin() == false)
81+
// {
82+
// Serial.println("I2C Error - check I2C Address");
83+
// while(1);
84+
// }
85+
86+
// // Establish communication with device
87+
// if(mySensor.begin() != 0)
88+
// {
89+
// Serial.println("Sensor failed to begin - Check wiring.");
90+
// while(1);
91+
// }
92+
93+
// Serial.println("Open the Serial Plotter for graphical viewing");
94+
95+
// // Default ODR: 1Hz
96+
97+
// delay(1000);
98+
// }
99+
100+
// void loop()
101+
// {
102+
103+
// bool dataReady = mySensor.getDataReady();
104+
105+
// if(dataReady == 1)
106+
// {
107+
// sths34pf80_tmos_func_status_t status;
108+
// mySensor.getStatus(&status);
109+
110+
111+
// // If the flag is high, then read out the information
112+
// if(status.pres_flag == 1)
113+
// {
114+
// mySensor.getPresenceValue(&presenceVal);
115+
// Serial.println(presenceVal);
116+
// }
117+
// }
118+
119+
// }

examples/Example2_Interrupts/Example2_Interrupts.ino

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,81 @@
22
#include <Wire.h>
33

44
STHS34PF80_I2C mySensor;
5-
// STHS34PF80_SPI mySensor;
65

7-
// Values to fill with presence and motion data
6+
// Values to fill with presence data
87
int16_t presenceVal = 0;
9-
int16_t motionVal = 0;
108

119
// Change the pin number to the pin that has been chosen for your setup
1210
int intPin = 12;
1311

14-
sths34pf80_tmos_route_int_t interruptRoute = STHS34PF80_TMOS_INT_DRDY;
12+
// Star the flag as false
13+
bool volatile interruptFlag = false;
1514

15+
// ISR to set the triggered interrupt
16+
void isr1()
17+
{
18+
interruptFlag = true;
19+
}
1620

1721
void setup()
1822
{
23+
// Begin I2C transactions
1924
Serial.begin(115200);
2025
Serial.println("STHS34PF80 Example 2: Interrupts");
2126

22-
// Begin I2C transactions
23-
Wire.begin();
24-
25-
// Checks established connection
26-
if(mySensor.begin() == false)
27+
// Begin I2C
28+
if(Wire.begin() == false)
2729
{
28-
Serial.println("Error"); // fix this print message
30+
Serial.println("I2C Error - check I2C Address");
2931
while(1);
3032
}
3133

32-
// Data Ready signal routed to the INT pin
33-
mySensor.setTmosRouteInterrupt(interruptRoute);
34-
35-
// **FINISH READING INTERRUPT SECTION TO MAKE SURE NO MORE IS NEEDED**
34+
// Establish communication with device
35+
if(mySensor.begin() != 0)
36+
{
37+
Serial.println("Sensor failed to begin - Check wiring.");
38+
while(1);
39+
}
3640

3741
// Set INT pin to be triggered on rising and falling edges of INT pin
3842
pinMode(intPin, INPUT);
43+
// Attach interrupt to the pin as a digital pin that triggers on a change
44+
attachInterrupt(digitalPinToInterrupt(intPin), isr1, CHANGE);
45+
46+
// Route all interrupts from device to interrupt pin
47+
mySensor.setTmosRouteInterrupt(STHS34PF80_TMOS_INT_OR);
48+
49+
// Enable the presence interrupt source
50+
// (see page 17 of application note AN5867 for more information)
51+
mySensor.setTmosInterruptOR(STHS34PF80_TMOS_INT_PRESENCE);
3952

40-
delay(1000);
53+
// Set interrupt value to pulsed on the INT pin
54+
mySensor.setInterruptPulsed(0);
55+
56+
delay(500);
4157
}
4258

4359
void loop()
4460
{
45-
46-
// Value to read interrupt pin status
47-
int interruptVal = digitalRead(intPin);
48-
49-
if(interruptVal == HIGH)
61+
// If interrupt is triggered
62+
if(interruptFlag == true)
5063
{
51-
sths34pf80_tmos_func_status_t status = mySensor.getStatus();
64+
interruptFlag = false;
65+
66+
sths34pf80_tmos_func_status_t status;
67+
mySensor.getStatus(&status);
68+
69+
Serial.println("Data ready!");
70+
5271

5372
// If the flag is high, then read out the information
5473
if(status.pres_flag == 1)
5574
{
5675
// Presence Units: cm^-1
5776
mySensor.getPresenceValue(&presenceVal);
5877
Serial.print("Presence: ");
59-
Serial.println(presenceVal);
60-
}
61-
62-
if(status.mot_flag == 1)
63-
{
64-
mySensor.getMotionValue(&motionVal);
65-
Serial.print("Motion: ");
66-
Serial.println(motionVal);
78+
Serial.print(presenceVal);
79+
Serial.println("cm^-1");
6780
}
6881
}
69-
7082
}

examples/Example3_EmbeddedFunctions/Example3_EmbeddedFunctions.ino

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,55 @@
22
#include <Wire.h>
33

44
STHS34PF80_I2C mySensor;
5-
// STHS34PF80_SPI mySensor;
65

76
int16_t presenceVal = 0;
87
int16_t motionVal = 0;
98

10-
sths34pf80_mem_bank_t bankDisable = STHS34PF80_MAIN_MEM_BANK;
11-
sths34pf80_mem_bank_t bankEnable = STHS34PF80_EMBED_FUNC_MEM_BANK;
12-
13-
uint16_t threshold = 500;
9+
uint16_t threshold = 1000;
1410
uint8_t hysteresis = 100;
1511

1612

1713
void setup()
1814
{
1915
Serial.begin(115200);
20-
Serial.println("STHS34PF80 Example 3: Embedded Functions");
21-
22-
Wire.begin();
16+
Serial.println("STHS34PF80 Example 3: Using Embedded Functions");
2317

24-
if(mySensor.begin() == false)
18+
// Begin I2C
19+
if(Wire.begin() == false)
2520
{
26-
Serial.println("Error"); // fix this print message
21+
Serial.println("I2C Error - check I2C Address");
2722
while(1);
2823
}
2924

30-
// Enter power-down mode
31-
25+
// Establish communication with device
26+
if(mySensor.begin() != 0)
27+
{
28+
Serial.println("Sensor failed to begin - Check wiring.");
29+
while(1);
30+
}
3231

32+
// Steps below follow Section 2.1 in the Application Note AN5867
33+
// Enter power-down mode by setting ODR to 0
34+
mySensor.setTmosODR(STHS34PF80_TMOS_ODR_OFF);
3335
// Enable access to embedded functions registers
34-
mySensor.setMemoryBank(bankEnable);
35-
36-
// Set threshold for the presence sensing
36+
mySensor.setMemoryBank(STHS34PF80_EMBED_FUNC_MEM_BANK);
37+
38+
// Set threshold for the presence sensing (Default = 0xC8)
3739
// NOTE: Presence Flag goes high when presence read is above threshold
3840
mySensor.setPresenceThreshold(threshold);
39-
// Set threshold for the motion sensing
40-
mySensor.setMotionThreshold(threshold);
41+
Serial.print("Presence value set: ");
42+
Serial.println(threshold);
4143
// Set hysteresis for the presence sensing (default value: 50LSB)
4244
mySensor.setPresenceHysteresis(hysteresis);
43-
// Set hysteresis for the motion sensing
45+
// Set hysteresis for the motion sensing (Default = 0x32)
4446
mySensor.setMotionHysteresis(hysteresis);
47+
Serial.print("Presence and Motion Hysteresis Set: ");
48+
Serial.println(hysteresis);
4549

4650
// Disable access to embedded functions registers
47-
mySensor.setMemoryBank(bankDisable);
51+
mySensor.setMemoryBank(STHS34PF80_MAIN_MEM_BANK);
4852
// Enter continuous mode
53+
mySensor.setTmosODR(STHS34PF80_TMOS_ODR_AT_1Hz);
4954

5055
delay(1000);
5156
}
@@ -55,23 +60,25 @@ void loop()
5560
// General presence and motion read (from example 1)
5661
bool dataReady = mySensor.getDataReady();
5762

58-
if(dataReady == true)
63+
if(dataReady == 1)
5964
{
60-
sths34pf80_tmos_func_status_t status = mySensor.getStatus();
65+
sths34pf80_tmos_func_status_t status;
66+
mySensor.getStatus(&status);
6167

62-
// Presence Flag - Only reads if the flag is high
68+
// Check if the presence (data ready) flag is high. If so, print the presence value
6369
if(status.pres_flag == 1)
6470
{
71+
// Presence Units: cm^-1
6572
mySensor.getPresenceValue(&presenceVal);
6673
Serial.print("Presence: ");
67-
Serial.println(presenceVal);
74+
Serial.print(presenceVal);
75+
Serial.println("cm^-1");
6876
}
69-
// Motion Flag
77+
78+
// Motion detected or not
7079
if(status.mot_flag == 1)
7180
{
72-
mySensor.getMotionValue(&motionVal);
73-
Serial.print("Motion: ");
74-
Serial.println(motionVal);
81+
Serial.println("Motion detected! ");
7582
}
7683
}
7784
}

0 commit comments

Comments
 (0)