Merge branch 'master' of mjesec.ffzg.hr:/git/GroveSensor
[GroveSensor] / GroveSensor.ino
index 204553a..5272ccc 100644 (file)
@@ -20,6 +20,9 @@ Adafruit_BMP280 bmp; // I2C
 
 
 #define BUTTON_PIN 6 
+
+#define SOUND_PIN A2
+#define LIGHT_PIN A6
  
 void setup(void) {
   //u8x8.setBusClock(100000);  // If you breakout other modules, please enable this line
@@ -43,13 +46,17 @@ void setup(void) {
                   Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
 
   pinMode(BUTTON_PIN, OUTPUT);
+  pinMode(SOUND_PIN, INPUT);
+  pinMode(LIGHT_PIN, INPUT);
 
 }
 
-unsigned long button_timeout;
+unsigned long t;
  
 void loop(void) {
 
+  t = millis();
+
   int oled_active = ! digitalRead(BUTTON_PIN);
   u8x8.setPowerSave( oled_active );
 
@@ -72,7 +79,7 @@ void loop(void) {
 
   u8x8.print(" ");
 
-  u8x8.setCursor(0,9);
+  u8x8.setCursor(0,1 * 9);
   u8x8.print("Humidity:");
   u8x8.print(humi);
   u8x8.print("%");
@@ -82,15 +89,38 @@ void loop(void) {
   float pressure;
   pressure = bmp.readPressure();
 
-  u8x8.setCursor(0,18);
+  u8x8.setCursor(0,2 * 9);
   u8x8.print( pressure );
   Serial.print(",bmp_pressure=");
   Serial.print(pressure);
 
+  unsigned long sumSquare;
+  for(int i=0; i++; i<1000) {
+    int sound = analogRead(SOUND_PIN);
+    sumSquare += sound * sound;
+  }
+  int rms_sound = sqrt(sumSquare / 1000);
+  Serial.print(",rms_sound=");
+  Serial.print(rms_sound);
+
+  int sound = analogRead(SOUND_PIN);
+  u8x8.setCursor(0,3 * 9);
+  u8x8.print( sound );
+  Serial.print(",sound=");
+  Serial.print(sound);
+
+  int light = analogRead(LIGHT_PIN);
+  u8x8.setCursor(0,3 * 9);
+  u8x8.print( light );
+  Serial.print(",light=");
+  Serial.print(light);
   Serial.println();
 
   u8x8.refreshDisplay();
-  delay(1000);
+
+  // next loop in 1000ms from start
+  t = t + 1000 - millis();
+  delay(t);
 
 }