delay different functions in loop



  • Well, it's been a couple of hours now I've been trying to get things to work but now I have to ask for help.
    I understand that you can call a function from the loop.
    I have combined the sketches: RELAY, SOIL MOISTURE and SI7021 (which is a temperature/humidity sensor).

    all parts are working as expected but I want for instance the soil moisture to only be activated and do it's stuff every hour or so.
    While the temperature/humidity should be checked every 5 minutes and send info if changed + forced every 30 minutes..
    I have gotten it to work, but it's not a nice sketch, and I have problem with the timings.
    Because if I call the functions they only work if I add a serial print to see if it's actually counting.
    If I comment it out, it will show everything like every 3 seconds..
    I understand my sketch is aweful, but it's the best I can do at the moment (still learning).

    Someone that could take a look and/or point em in the right direction?
    Thanks.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Example sketch showing how to control physical relays.
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    #define MY_NODE_ID 62
    #define MY_PARENT_NODE_ID 0
    #define MY_PARENT_NODE_IS_STATIC
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Enable repeater functionality for this node
    //#define MY_REPEATER_FEATURE
    #include <SPI.h>
    #include <MySensors.h>
    #include <SI7021.h>
    #include <RunningAverage.h>
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_3  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 3 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    #define RELEASE "1.4"
    
    #define AVERAGES 2
    
    // Child sensor ID's
    #define CHILD_ID_TEMP  5
    #define CHILD_ID_HUM   6
    #define CHILD_ID_MOISTURE 7
    
    // How many milli seconds between each measurement
    #define MEASURE_INTERVAL 30000
    // FORCE_TRANSMIT_INTERVAL, this number of times of wakeup, the sensor is forced to report all values to the controller
    #define FORCE_TRANSMIT_INTERVAL 120000
    
    // HUMI_TRANSMIT_THRESHOLD tells how much the humidity should have changed since last time it was transmitted. Likewise with
    // TEMP_TRANSMIT_THRESHOLD for temperature threshold.
    #define HUMI_TRANSMIT_THRESHOLD 0.2
    #define TEMP_TRANSMIT_THRESHOLD 0.2
    #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
    #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
    #define MOISTURE_TIME 5000 //1800000 // Sleep time between reads (in milliseconds)
    #define STABILIZATION_TIME 1000 // Let the sensor stabilize before reading
    SI7021 humiditySensor;
    
    // Sensor messages
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msg(CHILD_ID_MOISTURE, V_HUM);
    // Global settings
    int measureCount = 0;
    int TempHum = 0;
    int Moisture = 0;
    boolean isMetric = true;
    boolean highfreq = true;
    boolean transmission_occured = false;
    
    // Storage of old measurements
    float lastTemperature = -100;
    int lastHumidity = -100;
    int oldMoistureLevel = -1;
    byte direction = 0;
    RunningAverage raHum(AVERAGES);
    const int SENSOR_ANALOG_PINS[] = {A0, A1}; // Sensor is connected to these two pins. Avoid A3 if using ATSHA204. A6 and A7 cannot be used because they don't have pullups.
    void before()
    {
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Then set relay pins in output mode
            pinMode(pin, OUTPUT);
            // Set relay to last known state (using eeprom storage)
            digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
    }
    
    void setup()
    {
    Serial.begin(115200);
      Serial.print(F("Sensebender Micro FW "));
      Serial.print(RELEASE);
      Serial.flush();
    
      humiditySensor.begin();
    
    
      Serial.flush();
      Serial.println(F(" - Online!"));
    
      isMetric = getControllerConfig().isMetric;
      Serial.print(F("isMetric: ")); Serial.println(isMetric);
      raHum.clear();
      sendTempHumidityMeasurements(false);
      present(CHILD_ID_MOISTURE, S_HUM);
      for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++) {
        pinMode(SENSOR_ANALOG_PINS[i], OUTPUT);
        digitalWrite(SENSOR_ANALOG_PINS[i], LOW);
    }
    }
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Relay", "1.0");
    
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Register all sensors to gw (they will be created as child devices)
            present(sensor, S_BINARY);
            present(CHILD_ID_TEMP,S_TEMP);
            present(CHILD_ID_HUM,S_HUM);
            present(CHILD_ID_MOISTURE, S_HUM);
        }
    }
    
    
    void loop()
    {
      TempHum ++;
      Moisture ++;
      measureCount ++;
      bool forceTransmitTemp = false;
      bool forceTransmitMoist = false;
      transmission_occured = false;
    
      if (TempHum > FORCE_TRANSMIT_INTERVAL) { // force a transmission
        //forceTransmitTemp = true; 
        TempHum = 0;
        sendTempHumidityMeasurements(forceTransmitTemp);
      }
      if (Moisture > MOISTURE_TIME) { // force a transmission
        //forceTransmitMoist = true;
        Moisture = 0;
        sendMoistureMeasurements(forceTransmitMoist);
      }
          Serial.print(F("TempHum interval  :"));Serial.println(TempHum); 
      //wait(MEASURE_INTERVAL);  
    }
    void sendTempHumidityMeasurements(bool force)
    {
      bool tx = force;
    
      si7021_env data = humiditySensor.getHumidityAndTemperature();
    
      raHum.addValue(data.humidityPercent);
    
      float diffTemp = abs(lastTemperature - (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths)/100.0);
      float diffHum = abs(lastHumidity - raHum.getAverage());
    
      Serial.print(F("TempDiff :"));Serial.println(diffTemp);
      Serial.print(F("HumDiff  :"));Serial.println(diffHum); 
    
      if (isnan(diffHum)) tx = true; 
      if (diffTemp > TEMP_TRANSMIT_THRESHOLD) tx = true;
      if (diffHum > HUMI_TRANSMIT_THRESHOLD) tx = true;
    
      if (tx) {
        TempHum = 0;
        float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths) / 100.0;
    
        int humidity = data.humidityPercent;
        Serial.print("T: ");Serial.println(temperature);
        Serial.print("H: ");Serial.println(humidity);
    
        send(msgTemp.set(temperature,1));
        send(msgHum.set(humidity));
        lastTemperature = temperature;
        lastHumidity = humidity;
        transmission_occured = true;
        }
      }
    void sendMoistureMeasurements(bool force) {
    pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor
      analogRead(SENSOR_ANALOG_PINS[direction]);// Read once to let the ADC capacitor start charging
      sleep(STABILIZATION_TIME);
      int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PINS[direction]));
    
      // Turn off the sensor to conserve battery and minimize corrosion
      pinMode(SENSOR_ANALOG_PINS[direction], OUTPUT);
      digitalWrite(SENSOR_ANALOG_PINS[direction], LOW);
    
      direction = (direction + 1) % 2; // Make direction alternate between 0 and 1 to reverse polarity which reduces corrosion
      // Always send moisture information so the controller sees that the node is alive
    
      // Send rolling average of 2 samples to get rid of the "ripple" produced by different resistance in the internal pull-up resistors
      // See http://forum.mysensors.org/topic/2147/office-plant-monitoring/55 for more information
      if (oldMoistureLevel == -1) { // First reading, save value
        oldMoistureLevel = moistureLevel;
      }
      send(msg.set((moistureLevel + oldMoistureLevel +  0.5) / 2 / 10.23, 1));
      oldMoistureLevel = moistureLevel;
      transmission_occured = true;
      Serial.print("M: ");Serial.println(moistureLevel);
        }
    
    void receive(const MyMessage &message)
    {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_STATUS) {
            // Change relay state
            digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
            // Store state in eeprom
            saveState(message.sensor, message.getBool());
            // Write some debug info
            Serial.print("Incoming change for sensor:");
            Serial.print(message.sensor);
            Serial.print(", New status: ");
            Serial.println(message.getBool());
      
        }
    }```


  • After some hours of sleep and refactoring everything, now I think I've managed to get everything working.
    I have some problem wrapping my head around "forceTransmit" command and how it works.
    Also, millis() is also something I have to learn :)

    Here is the code if anyone finds it interesting.
    What it does is measure soil moisture every hour and sends info (normal forkthingie).
    Also reading every 30 seconds for temperature and humidity (si7021)
    If humidity changes by 1 or temperature by 0.2 it will send info.
    If no info has been sent it will force send info every 20 minutes.

    3 Relays are there to power water pumps and a LED strip.

    i have now semi-learned the use of functions, which is fun :)
    cheers!

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Example sketch showing how to control physical relays.
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    //Network
    #define MY_NODE_ID 62
    #define MY_PARENT_NODE_ID 0
    #define MY_PARENT_NODE_IS_STATIC
    #define MY_RADIO_NRF24
    //Includes
    #include <SPI.h>
    #include <MySensors.h>
    #include <SI7021.h>
    #include <RunningAverage.h>
    //Define Relay
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_3  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 3 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    #define RELEASE "1.4"
    //Define Moisture things
    #define AVERAGES 2
    
    // Child sensor ID's
    #define CHILD_ID_TEMP  5
    #define CHILD_ID_HUM   6
    #define CHILD_ID_MOISTURE 7
    // How many milli seconds between each measurement of temp and humidity
    #define MEASURE_INTERVAL 30000
    // FORCE_TRANSMIT_INTERVAL, this number of times of wakeup, the sensor is forced to report all values to the controller
    #define FORCE_TRANSMIT_INTERVAL 2000 // 20minutes
    // HUMI_TRANSMIT_THRESHOLD tells how much the humidity should have changed since last time it was transmitted. Likewise with
    // TEMP_TRANSMIT_THRESHOLD for temperature threshold.
    #define HUMI_TRANSMIT_THRESHOLD 1.0
    #define TEMP_TRANSMIT_THRESHOLD 0.2
    #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
    #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
    #define STABILIZATION_TIME 1000 // Let the sensor stabilize before reading
    SI7021 humiditySensor;
    
    // Sensor messages
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgMoist(CHILD_ID_MOISTURE, V_HUM);
    MyMessage msgR1(RELAY_1, V_STATUS);
    MyMessage msgR2(RELAY_2, V_STATUS);
    // Global settings
    const unsigned long tUpdateTemp = 5000; // update interval
    unsigned long t0;
    const unsigned long tUpdateMoist = 360000; // update interval
    unsigned long t1;
    int measureCount = 0;
    boolean isMetric = true;
    boolean highfreq = true;
    boolean transmission_occured = false;
    
    // Storage of old measurements
    float lastTemperature = -100;
    int lastHumidity = -100;
    int oldMoistureLevel = -1;
    //Change direction on moisture
    byte direction = 0;
    //Moisture sensor pins
    RunningAverage raHum(AVERAGES);
    const int SENSOR_ANALOG_PINS[] = {A0, A1}; // Sensor is connected to these two pins. Avoid A3 if using ATSHA204. A6 and A7 cannot be used because they don't have pullups.
    
    void before()
    {
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++)
        {
            // Then set relay pins in output mode
            pinMode(pin, OUTPUT);
            // Set relay to last known state (using eeprom storage)
            digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
    }
    
    void setup()
    {
      Serial.begin(115200);
      humiditySensor.begin();
      isMetric = getControllerConfig().isMetric;
      Serial.print(F("isMetric: ")); Serial.println(isMetric);
      raHum.clear();
      t0=millis();
      t1=millis();
      //sendTempHumidityMeasurements(true);
      //sendMoistureMeasurements();
      delay(250);
      for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++)
      {
        pinMode(SENSOR_ANALOG_PINS[i], OUTPUT);
        digitalWrite(SENSOR_ANALOG_PINS[i], LOW); 
      }
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("RelayTempHumMoist", "1.0");
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++)
      {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_BINARY,"Relays");
      }
      present(CHILD_ID_TEMP,S_TEMP,"Temperature");
      present(CHILD_ID_HUM,S_HUM,"Humidity");
      present(CHILD_ID_MOISTURE, S_HUM,"Moisture");
    }
    
    
    void loop()
    {
      measureCount ++;
      bool forceTransmit = false;
      transmission_occured = false;
      if (measureCount > FORCE_TRANSMIT_INTERVAL)
      {
        forceTransmit = true; 
        measureCount = 0;
        Serial.print(F("inne i loopen :"));
      }
    
    if ((millis() - t1) > tUpdateMoist) sendMoistureMeasurements();
        sendTempHumidityMeasurements(forceTransmit);
    }
    
    void sendTempHumidityMeasurements(bool force)
    {
      wait(MEASURE_INTERVAL);
      bool tx = force;
      si7021_env data = humiditySensor.getHumidityAndTemperature();
      raHum.addValue(data.humidityPercent);
      float diffTemp = abs(lastTemperature - (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths)/100.0);
      float diffHum = abs(lastHumidity - raHum.getAverage());
      Serial.print(F("TempDiff :"));Serial.println(diffTemp);
      Serial.print(F("HumDiff  :"));Serial.println(diffHum);
      t0 = millis();
      if (isnan(diffHum)) tx = true; 
      if (diffTemp > TEMP_TRANSMIT_THRESHOLD) tx = true;
      if (diffHum > HUMI_TRANSMIT_THRESHOLD) tx = true;
    
      if (tx)
      {
        measureCount = 0;
        float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths) / 100.0;
    
        int humidity = data.humidityPercent;
        Serial.print("T: ");Serial.println(temperature);
        Serial.print("H: ");Serial.println(humidity);
    
        send(msgTemp.set(temperature,1));
        send(msgHum.set(humidity));
        lastTemperature = temperature;
        lastHumidity = humidity;
        transmission_occured = true;
      }
    }
        
    void sendMoistureMeasurements()
    {
      pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor
      analogRead(SENSOR_ANALOG_PINS[direction]);// Read once to let the ADC capacitor start charging
      sleep(STABILIZATION_TIME);
      int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PINS[direction]));
    
      // Turn off the sensor to conserve battery and minimize corrosion
      pinMode(SENSOR_ANALOG_PINS[direction], OUTPUT);
      digitalWrite(SENSOR_ANALOG_PINS[direction], LOW);
    
      direction = (direction + 1) % 2; // Make direction alternate between 0 and 1 to reverse polarity which reduces corrosion
      // Always send moisture information so the controller sees that the node is alive
    
      // Send rolling average of 2 samples to get rid of the "ripple" produced by different resistance in the internal pull-up resistors
      // See http://forum.mysensors.org/topic/2147/office-plant-monitoring/55 for more information
      if (oldMoistureLevel == -1)
      { // First reading, save value
        oldMoistureLevel = moistureLevel;
      }
      send(msgMoist.set((moistureLevel + oldMoistureLevel +  0.5) / 2 / 10.23, 1));
      oldMoistureLevel = moistureLevel;
      t1 = millis();
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_STATUS)
      {
        // Change relay state
        digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
        // Store state in eeprom
        saveState(message.sensor, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
      }
    }```

Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.