Skip to content

Commit 00bd605

Browse files
committed
Merge pull request arduino#94 from gizmocuz/master
Enhanced the UVSensor example with 1 decimal to the UVI index.
2 parents ce597b7 + e908e85 commit 00bd605

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

libraries/MySensors/examples/UVSensor/UVSensor.ino

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,85 @@
1+
#include <SPI.h>
2+
#include <MySensor.h>
13
/*
2-
Vera Arduino UVM-30A
4+
Arduino UVM-30A
35
46
connect the sensor as follows :
57
68
+ >>> 5V
79
- >>> GND
810
out >>> A0
911
10-
Contribution: epierre, bulldoglowell
11-
Converted to 1.4 by Henrik Ekblad
12+
Contribution: epierre, bulldoglowell, gizmocuz
13+
14+
Index table taken from: http://www.elecrow.com/sensors-c-111/environment-c-111_112/uv-sensor-moduleuvm30a-p-716.html
15+
Because this table is pretty lineair, we can calculate a UVI with one decimal
1216
1317
License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
14-
1518
*/
1619

1720
#include <MySensor.h>
1821
#include <SPI.h>
1922

20-
#define CHILD_ID_UV 0
2123
#define UV_SENSOR_ANALOG_PIN 0
24+
25+
#define CHILD_ID_UV 0
26+
2227
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
2328

2429
MySensor gw;
2530
MyMessage uvMsg(CHILD_ID_UV, V_UV);
26-
int lastUV = -1;
27-
int uvIndexValue [13] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170, 3000};
28-
int uvIndex;
31+
32+
unsigned long lastSend =0;
33+
float uvIndex;
34+
float lastUV = -1;
35+
int uvIndexValue [12] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170};
2936

3037
void setup()
3138
{
3239
gw.begin();
3340

3441
// Send the sketch version information to the gateway and Controller
35-
gw.sendSketchInfo("UV Sensor", "1.1");
42+
gw.sendSketchInfo("UV Sensor", "1.2");
3643

3744
// Register all sensors to gateway (they will be created as child devices)
3845
gw.present(CHILD_ID_UV, S_UV);
39-
4046
}
4147

4248
void loop()
43-
{
44-
uint16_t uv = analogRead(0);// Get UV value
45-
Serial.print("Uv reading: ");
46-
Serial.println(uv);
47-
for (int i = 0; i < 13; i++)
49+
{
50+
unsigned long currentTime = millis();
51+
52+
uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);// Get UV value
53+
if (uv>1170)
54+
uv=1170;
55+
56+
//Serial.print("UV Analog reading: ");
57+
//Serial.println(uv);
58+
59+
int i;
60+
for (i = 0; i < 12; i++)
4861
{
4962
if (uv <= uvIndexValue[i])
5063
{
5164
uvIndex = i;
5265
break;
5366
}
5467
}
55-
Serial.print("Uv index: ");
56-
Serial.println(uvIndex);
68+
69+
//calculate 1 decimal if possible
70+
if (i>0) {
71+
float vRange=uvIndexValue[i]-uvIndexValue[i-1];
72+
float vCalc=uv-uvIndexValue[i-1];
73+
uvIndex+=(1.0/vRange)*vCalc-1.0;
74+
}
75+
76+
//Serial.print("UVI: ");
77+
//Serial.println(uvIndex,2);
5778

58-
if (uvIndex != lastUV) {
59-
gw.send(uvMsg.set(uvIndex));
79+
//Send value to gateway if changed, or at least every 5 minutes
80+
if ((uvIndex != lastUV)||(currentTime-lastSend >= 5*60*1000)) {
81+
lastSend=currentTime;
82+
gw.send(uvMsg.set(uvIndex,2));
6083
lastUV = uvIndex;
6184
}
6285

0 commit comments

Comments
 (0)