GUVA-S12SD UV sensor sketch





  • /**

    // Enable debug prints to serial monitor
    #define MY_DEBUG

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95

    #include <MySensors.h>

    #define UV_SENSOR_ANALOG_PIN 0

    #define CHILD_ID_UV 0

    uint32_t SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)

    MyMessage uvMsg(CHILD_ID_UV, V_UV);

    int lastSend =0;
    int sensorValue;
    long sum = 0;
    int vout = 0;
    int uv = 0;
    int lastUV = -1;

    void presentation()
    {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("UV Sensor", "1.3");

    // Register all sensors to gateway (they will be created as child devices)
    present(CHILD_ID_UV, S_UV);
    

    }
    void setup() {
    Serial.begin(115000);
    }
    void loop()
    {
    uint32_t currentTime = millis();
    sensorValue = 0;
    sum = 0;
    for (int i = 0 ; i < 1024 ; i++ ) {
    sensorValue = analogRead(UV_SENSOR_ANALOG_PIN);
    sum = sensorValue + sum;
    delay(2);
    }
    vout = sum >> 10;
    vout = vout * 4980.0 / 1024;
    Serial.print("The Photocurrent value:");
    Serial.print(vout);
    Serial.println("mV");
    if (vout < 50) {
    uv = 0;
    }
    else if (vout < 227) {
    uv = 1;
    }
    else if (vout < 318) {
    uv = 2;
    }
    else if (vout < 408) {
    uv = 3;
    }
    else if (vout < 503) {
    uv = 4;
    }
    else if (vout < 606) {
    uv = 5;
    }
    else if (vout < 696) {
    uv = 6;
    }
    else if (vout < 795) {
    uv = 7;
    }
    else if (vout < 881) {
    uv = 8;
    }
    else if (vout < 976) {
    uv = 9;
    }
    else if (vout < 1079) {
    uv = 10;
    }
    else {
    uv = 11;
    }

    //Send value to gateway if changed, or at least every 5 minutes
    if ((uv != lastUV)||(currentTime-lastSend >= 5UL*60UL*1000UL)) {
    	lastSend=currentTime;
    	send(uvMsg.set(uv));
    	lastUV = uv;
    }
    
    sleep(SLEEP_TIME);
    

    }


Log in to reply
 

Suggested Topics

  • 8
  • 3
  • 2
  • 1
  • 7
  • 5

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts