accelerometer accumulates 300 readings
[GroveSensor] / GroveSensor.ino
1 #include <Arduino.h>
2 #include <U8x8lib.h>
3
4 #include "DHT.h"
5
6 #include <Adafruit_BMP280.h>
7
8 U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
9
10 // U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // OLEDs without Reset of the Display
11  
12
13 #define DHTPIN 3     // what pin we're connected to
14 #define DHTTYPE DHT11   // DHT 11 
15
16 DHT dht(DHTPIN, DHTTYPE);
17
18
19 Adafruit_BMP280 bmp; // I2C
20
21
22 #include "LIS3DHTR.h"
23 #include <Wire.h>
24 LIS3DHTR<TwoWire> LIS; //IIC
25 #define WIRE Wire
26
27
28 #define BUTTON_PIN 6 
29
30 #define SOUND_PIN A2
31 #define LIGHT_PIN A6
32  
33 void setup(void) {
34   //u8x8.setBusClock(100000);  // If you breakout other modules, please enable this line
35   u8x8.begin();
36   u8x8.setPowerSave(0);
37   u8x8.setFlipMode(1);
38
39   Serial.begin(115200);
40
41   dht.begin();
42
43   if (!bmp.begin()) {
44     Serial.println(F("Could not find a valid BMP280 sensor!"));
45   }
46
47   /* Default settings from datasheet. */
48   bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
49                   Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
50                   Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
51                   Adafruit_BMP280::FILTER_X16,      /* Filtering. */
52                   Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
53
54   pinMode(BUTTON_PIN, OUTPUT);
55   pinMode(SOUND_PIN, INPUT);
56   pinMode(LIGHT_PIN, INPUT);
57
58
59   LIS.begin(WIRE, 0x19);
60   LIS.openTemp();
61   delay(100);
62   LIS.setFullScaleRange(LIS3DHTR_RANGE_2G);
63   LIS.setOutputDataRate(LIS3DHTR_DATARATE_5KHZ);
64   LIS.setHighSolution(true); //High solution enable
65
66 }
67
68 unsigned long t;
69  
70 void loop(void) {
71
72   t = millis();
73
74   int oled_active = ! digitalRead(BUTTON_PIN);
75   u8x8.setPowerSave( oled_active );
76
77   float temp, humi;
78   temp = dht.readTemperature();
79   humi = dht.readHumidity();
80
81   u8x8.setFont(u8x8_font_chroma48medium8_r);
82   u8x8.setCursor(0, 0);
83   //u8x8.print("Temp:");
84   u8x8.print(temp);
85   u8x8.print("C ");
86   Serial.print("dht_temp=");
87   Serial.print(temp);
88
89   temp = bmp.readTemperature();
90   u8x8.print( temp );
91   Serial.print(",bmp_temp=");
92   Serial.print(temp);
93
94   u8x8.print(" ");
95
96   u8x8.setCursor(0,1 * 9);
97   u8x8.print("Humidity:");
98   u8x8.print(humi);
99   u8x8.print("%");
100   Serial.print(",dht_humidity=");
101   Serial.print(humi);
102
103   float pressure;
104   pressure = bmp.readPressure();
105
106   u8x8.setCursor(0,2 * 9);
107   u8x8.print( pressure );
108   Serial.print(",bmp_pressure=");
109   Serial.print(pressure);
110
111   unsigned long sumSquare;
112   for(int i=0; i++; i<1000) {
113     int sound = analogRead(SOUND_PIN);
114     sumSquare += sound * sound;
115   }
116   int rms_sound = sqrt(sumSquare / 1000);
117   Serial.print(",rms_sound=");
118   Serial.print(rms_sound);
119
120   int sound = analogRead(SOUND_PIN);
121   u8x8.setCursor(0,3 * 9);
122   u8x8.print( sound );
123   Serial.print(",sound=");
124   Serial.print(sound);
125
126   int light = analogRead(LIGHT_PIN);
127   u8x8.setCursor(0,3 * 9);
128   u8x8.print( light );
129   Serial.print(",light=");
130   Serial.print(light);
131
132   u8x8.refreshDisplay();
133
134   float x = 0, y = 0, z = 0;
135   for(int i=0; i<=300; i++) {
136         x += LIS.getAccelerationX();
137         y += LIS.getAccelerationY();
138         z += LIS.getAccelerationZ();
139   }
140
141   Serial.print(",x="); Serial.print(x);
142   Serial.print(",y="); Serial.print(y);
143   Serial.print(",z="); Serial.print(z);
144
145   Serial.println();
146
147   t = t + 1000 - millis();
148   delay(t);
149
150 }
151