Crazy timer on my sketch



  • hello . i'm finishing an second version from my water irrigation(1 valve control) and i have an issue with one of the timers that i can't understand.

    its set for 1 hour,but never reach 1 hour, sometimes 47minutes ,other times 5 minutes.... annoying.

    Can anyone understand this ? i'm too limited on programming :s

    this is the crazy timer that i talk about:
    this timer close the valv after 3600second(1hour) but won't work..

      unsigned long nowMillis = millis();
        
       if ((timer==true) && (nowMillis - startMillis >= VALVE_TIME))          
        {
          startMillis =  nowMillis ;
           VALV1_OFF();
           timer==false;
    

    full code:

    // Set blinking period (in milliseconds)
    #define MY_DEFAULT_LED_BLINK_PERIOD 500
    #define MY_WITH_LEDS_BLINKING_INVERSE
    //#define MY_DEFAULT_ERR_LED_PIN 17
    #define MY_DEFAULT_TX_LED_PIN 17
    #define MY_DEFAULT_RX_LED_PIN 17
    
    //#define MY_DEBUG
    #define MY_RADIO_RFM69
    //#define MY_REPEATER_FEATURE
    
    #include <TimeLib.h>
    #include <SPI.h>
    #include <OneWire.h> 
    #include <DallasTemperature.h>
    #include <MySensors.h>
    
    
    #define ONE_WIRE_BUS 6 
    OneWire oneWire(ONE_WIRE_BUS); 
    DallasTemperature sensors(&oneWire);
    //#define CHILD_ID_MOISTURE 4
    #define CHILD_ID_TEMP 21
    #define CHILD_ID_VALV1 10
    //#define CHILD_ID_VALV2 11
    
    
    #define RELAY_PIN  3  // Arduino Digital I/O pin number for relay  VALV 1 
    #define RELAY_PIN2  5  // Arduino Digital I/O pin number for relay VALV 2
    #define BUTTON_PIN  4  // Arduino Digital I/O pin number for button 
    #define LEDR1   16  // LED A2 pin
    #define LEDR2  15// LED A1 pin
    //#define LEDERRO 17  // LED A3  pin
    
    
    
    #define VALVE_TIME 3600000UL    //selenoid 1 goes off after () time  (7200000UL)-2HOUR
    #define readtime 1800000UL  //time between read TEMPERATURE (3600000UL) - 1HOUR
    
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxx |SET TIME HERE|     - valve1-  xxxxxxxxxxxxxxxxxxxxxxxx |
    //TIME1
    const int H1 = 17; //HOURS START PROGRAM     //time to start  selenoid1                 |
    const int M1 = 00; //MINUTES START PROGRAM                                              |
    //TIME2
    const int H2 = 8; //HOURS START PROGRAM     //time to start  selenoid1                 |
    const int M2 = 00; //MINUTES START PROGRAM 
    //----------------------------------------valve2--------------------------------------- |                                         
    
    unsigned long startMillis = 0;
    unsigned long startMillisA = 0;
     
    int reading;// the current reading from the input pin
    bool RH = false;         
    int previous = LOW;
    int timer = false;
    bool timeReceived = false;
    unsigned long  lastRequest=0;
    
    MyMessage msg1(CHILD_ID_TEMP,V_TEMP);
    MyMessage msg2(CHILD_ID_VALV1,V_LIGHT);
    
    bool state;
     
    
    void before()
    {
      // Startup up the OneWire library
      sensors.begin();  //DS18B20 LIBRARY START
    }
    
    void setup()  
        {  
    //request time from gw
       requestTime(receiveTime);
              
      // Setup the button
       pinMode(BUTTON_PIN,INPUT);  
       pinMode(RELAY_PIN, OUTPUT);   
       pinMode(RELAY_PIN2, OUTPUT);  
    
       pinMode(LEDR1, OUTPUT); 
       pinMode(LEDR2, OUTPUT); 
       sensors.begin();  //DS18B20 LIBRARY START
    
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN,LOW);
      
        }
    
    
    
    void presentation()
        {
      sendSketchInfo("irrigation_temp_soilMoisture", "2.0");
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_ID_VALV1, S_LIGHT);
        }
    void receiveTime(unsigned long time)
    {
      // Ok, set incoming time 
      setTime(time);
      timeReceived = true;
    
    Serial.print("Time now is: ");
    
      Serial.print(hour());
      Serial.print(":");
      Serial.print(minute());
      Serial.print(":");
      Serial.print(second());
    }
    //#################################################################################################
     void VALV1_ON()
        {
         analogWrite(RELAY_PIN,255);
         delay(400);
         analogWrite(RELAY_PIN,90);
         digitalWrite(LEDR1,HIGH);
         send(msg2.set(true),false);
          
        }
    
    void VALV1_OFF()
       {
          analogWrite(RELAY_PIN,LOW);
          digitalWrite(LEDR1,LOW);
          send(msg2.set(false),false);
        }
    //#########################################################################################################3
    // ✓ ORDER RECEIVED FROM GW  #   ORDER RECEIVED FROM GW  #  ORDER RECEIVED FROM GW  ORDER RECEIVED FROM GW  ORDER RECEIVED FROM GW ✓
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
        // Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
             
          if (state == 1) 
          {
            VALV1_ON();
          }
       
        else if (state == 0)
         { 
          VALV1_OFF();
         }
       
        } 
    }
    void loop() //loop    loop         loop        loop        loop        loop        loop        loop        loop        loop        loop    
    {
        
       
       unsigned long nows = millis();
       // If no time has been received yet, request it every 10 second from controller
      // When time has been received, request update every 12h
      if ((!timeReceived && (nows-lastRequest) > (10UL*1000UL))
        || (timeReceived && (nows-lastRequest) > (43200000UL)))
        {
        // Request time from controller. 
     //   Serial.println("requesting time");
        requestTime(receiveTime);  
        lastRequest = nows;
        }
    
     
    
      
       //       toogle  switch   toogle  switch   toogle  switch   toogle  switch   toogle  switch  
    //toogle  switch----------selenoid 1------------------------------- 
    
     reading = digitalRead(BUTTON_PIN);
    
     
       if (reading == HIGH && RH == false)
      {
           VALV1_ON();
          RH = true;
          timer = true;
           delay(400);
      }
      else if(reading == HIGH &&RH == true)
      {
        VALV1_OFF();
        RH = false;
        timer==false;
        delay(400);
      }
    
    // ✓ time out ✓
        unsigned long nowMillis = millis();
        
       if ((timer==true) && (nowMillis - startMillis >= VALVE_TIME))          
        {
          startMillis =  nowMillis ;
           VALV1_OFF();
           timer==false;
       
        }
    
      //✓ ON by time ✓
      if ((hour() == H1 && minute() == M1) ||(hour() == H2 && minute() == M2)  )  
     {
         VALV1_ON();
         timer = true;
     }
       
    
    
    
    //✓ time to send temperature ? ✓
     float temperature =sensors.getTempCByIndex(0); 
     
    unsigned long nowMillisA = millis();
        if (nowMillisA - startMillisA >= readtime)
        {
    //✓ read temperature  sensors ✓
         sensors.requestTemperatures();
         send(msg1.set(temperature,1));  //send  temp to gw
         startMillisA =  nowMillisA ;  //reset time
        }
    }
    

  • Mod

    @tmaster the debug log will probably give good insight to why the node reacts early.

    If you use the auto-formatting feature in the Arduino IDE, it will be much easier to follow the code.



  • @mfalkvidd said in Crazy timer on my sketch:

    the debug log will probably give good insight to why the node reacts early.

    i just discovered the auto-formatting right now 🙂

    About debug (mydebug) just reacts as normal,everything as should, but in shorter time... less 70-80% of the time most the times. Tomorrow i will get the arduino that its watering right know and change the code a bit..try some "while" instead of "if" or hammering it 😉


  • Mod

    @tmaster the sketch has a few

    timer==false;
    

    that probably should be

    timer=false;
    


  • Nice catch! i didn´t see that . thank you



  • @mfalkvidd that was the problem! I correct the code and timers are working perfect now. I will post the skematic and code this night on projects for if someone want to use it . Very handy on summer for water some vegetables like i do. It's a very simple build.


  • Mod

    @tmaster great work!


Log in to reply
 

Suggested Topics

  • 4
  • 15
  • 20
  • 2
  • 17
  • 9

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts