Merge pull request #94 from gizmocuz/master

Enhanced the UVSensor example with 1 decimal to the UVI index.
This commit is contained in:
Henrik Ekblad
2015-03-24 10:20:48 +01:00
@@ -1,5 +1,7 @@
#include <SPI.h>
#include <MySensor.h>
/* /*
Vera Arduino UVM-30A Arduino UVM-30A
connect the sensor as follows : connect the sensor as follows :
@@ -7,44 +9,55 @@
- >>> GND - >>> GND
out >>> A0 out >>> A0
Contribution: epierre, bulldoglowell Contribution: epierre, bulldoglowell, gizmocuz
Converted to 1.4 by Henrik Ekblad
Index table taken from: http://www.elecrow.com/sensors-c-111/environment-c-111_112/uv-sensor-moduleuvm30a-p-716.html
Because this table is pretty lineair, we can calculate a UVI with one decimal
License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
*/ */
#include <MySensor.h> #include <MySensor.h>
#include <SPI.h> #include <SPI.h>
#define CHILD_ID_UV 0
#define UV_SENSOR_ANALOG_PIN 0 #define UV_SENSOR_ANALOG_PIN 0
#define CHILD_ID_UV 0
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds) unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
MySensor gw; MySensor gw;
MyMessage uvMsg(CHILD_ID_UV, V_UV); MyMessage uvMsg(CHILD_ID_UV, V_UV);
int lastUV = -1;
int uvIndexValue [13] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170, 3000}; unsigned long lastSend =0;
int uvIndex; float uvIndex;
float lastUV = -1;
int uvIndexValue [12] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170};
void setup() void setup()
{ {
gw.begin(); gw.begin();
// Send the sketch version information to the gateway and Controller // Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("UV Sensor", "1.1"); gw.sendSketchInfo("UV Sensor", "1.2");
// Register all sensors to gateway (they will be created as child devices) // Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_UV, S_UV); gw.present(CHILD_ID_UV, S_UV);
} }
void loop() void loop()
{ {
uint16_t uv = analogRead(0);// Get UV value unsigned long currentTime = millis();
Serial.print("Uv reading: ");
Serial.println(uv); uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);// Get UV value
for (int i = 0; i < 13; i++) if (uv>1170)
uv=1170;
//Serial.print("UV Analog reading: ");
//Serial.println(uv);
int i;
for (i = 0; i < 12; i++)
{ {
if (uv <= uvIndexValue[i]) if (uv <= uvIndexValue[i])
{ {
@@ -52,11 +65,21 @@ void loop()
break; break;
} }
} }
Serial.print("Uv index: ");
Serial.println(uvIndex);
if (uvIndex != lastUV) { //calculate 1 decimal if possible
gw.send(uvMsg.set(uvIndex)); if (i>0) {
float vRange=uvIndexValue[i]-uvIndexValue[i-1];
float vCalc=uv-uvIndexValue[i-1];
uvIndex+=(1.0/vRange)*vCalc-1.0;
}
//Serial.print("UVI: ");
//Serial.println(uvIndex,2);
//Send value to gateway if changed, or at least every 5 minutes
if ((uvIndex != lastUV)||(currentTime-lastSend >= 5*60*1000)) {
lastSend=currentTime;
gw.send(uvMsg.set(uvIndex,2));
lastUV = uvIndex; lastUV = uvIndex;
} }