Skip to content
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. [SOLVED] Timers and Interrupts not being triggered
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store

[SOLVED] Timers and Interrupts not being triggered

Scheduled Pinned Locked Moved Troubleshooting
19 Posts 4 Posters 5.5k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Cayouette
    wrote on last edited by
    #6

    @Yveaux How can I get Serial.println to work? I'm using the new 2.0 library and I have a few Serail.println() command in various places, including in void setup(), but I am not getting any output on the serial monitor except for all the debug data.

    Sketch with serial output:

    /*AC Light Control
     
     Updated by Robert Twomey 
     
     Changed zero-crossing detection to look for RISING edge rather
     than falling.  (originally it was only chopping the negative half
     of the AC wave form). 
     
     Also changed the dim_check() to turn on the Triac, leaving it on 
     until the zero_cross_detect() turn's it off.
     
     Adapted from sketch by Ryan McLaughlin 
     http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230333861/30
     
     */
    #define MY_DEBUG 
    
    #define MY_RADIO_NRF24
    
    #define MY_NODE_ID 300
    #define SN "ACDimmer"
    #define SV "1.0"
    #define AC1_ID 1
    
    #include <MySensors.h>
    #include <SPI.h>
    #include <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
    
    volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
    volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
    int AC_pin = 4;                 // Output to Opto Triac
    int interruptPin = 3;
    int dim = 0;                    // Dimming level (0-128)  0 = on, 128 = 0ff
    int inc=1;                      // counting up or down, 1=up, -1=down
    int freqStep = 65;    // This is the delay-per-brightness step in microseconds.
                          // For 60 Hz it should be 65
    
    MyMessage dimmerMsg(AC1_ID, V_DIMMER);
    
    void setup() {                                        // Begin setup
      Serial.println("Dim Sketch started");
    
      pinMode(AC_pin, OUTPUT);                            // Set the Triac pin as output
      attachInterrupt(digitalPinToInterrupt(interruptPin), zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
      Timer1.initialize(freqStep);                       // Initialize TimerOne library for the freq we need
      Timer1.attachInterrupt(dim_check, freqStep);      
    
    }
    
    void presentation() {
      // Register the LED Dimmable Light with the gateway
      present( AC1_ID, S_DIMMER );
      
      sendSketchInfo(SN, SV);
    }
    
    
    void zero_cross_detect() {    
      Serial.println("Zero Cross");
      
      zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
      i=0;
      digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
    }                                 
    
    // Turn on the TRIAC at the appropriate time
    void dim_check() {                   
      Serial.println("Dim Check");
    
      if(zero_cross == true) {              
        if(i>=dim) {                     
          digitalWrite(AC_pin, HIGH); // turn on light       
          i=0;  // reset time step counter                         
          zero_cross = false; //reset zero cross detection
        } 
        else {
          i++; // increment time step counter                     
        }                                
      }                                  
    }                                   
    
    void loop() {                        
      Serial.print("Dim value: ");
      Serial.println(String(dim));
      
      dim+=inc;
      if((dim>=128) || (dim<=0))
        inc*=-1;
      delay(18);
    }
    
    
    
    YveauxY 1 Reply Last reply
    0
    • M Mike Cayouette

      @Yveaux How can I get Serial.println to work? I'm using the new 2.0 library and I have a few Serail.println() command in various places, including in void setup(), but I am not getting any output on the serial monitor except for all the debug data.

      Sketch with serial output:

      /*AC Light Control
       
       Updated by Robert Twomey 
       
       Changed zero-crossing detection to look for RISING edge rather
       than falling.  (originally it was only chopping the negative half
       of the AC wave form). 
       
       Also changed the dim_check() to turn on the Triac, leaving it on 
       until the zero_cross_detect() turn's it off.
       
       Adapted from sketch by Ryan McLaughlin 
       http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230333861/30
       
       */
      #define MY_DEBUG 
      
      #define MY_RADIO_NRF24
      
      #define MY_NODE_ID 300
      #define SN "ACDimmer"
      #define SV "1.0"
      #define AC1_ID 1
      
      #include <MySensors.h>
      #include <SPI.h>
      #include <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
      
      volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
      volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
      int AC_pin = 4;                 // Output to Opto Triac
      int interruptPin = 3;
      int dim = 0;                    // Dimming level (0-128)  0 = on, 128 = 0ff
      int inc=1;                      // counting up or down, 1=up, -1=down
      int freqStep = 65;    // This is the delay-per-brightness step in microseconds.
                            // For 60 Hz it should be 65
      
      MyMessage dimmerMsg(AC1_ID, V_DIMMER);
      
      void setup() {                                        // Begin setup
        Serial.println("Dim Sketch started");
      
        pinMode(AC_pin, OUTPUT);                            // Set the Triac pin as output
        attachInterrupt(digitalPinToInterrupt(interruptPin), zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
        Timer1.initialize(freqStep);                       // Initialize TimerOne library for the freq we need
        Timer1.attachInterrupt(dim_check, freqStep);      
      
      }
      
      void presentation() {
        // Register the LED Dimmable Light with the gateway
        present( AC1_ID, S_DIMMER );
        
        sendSketchInfo(SN, SV);
      }
      
      
      void zero_cross_detect() {    
        Serial.println("Zero Cross");
        
        zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
        i=0;
        digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
      }                                 
      
      // Turn on the TRIAC at the appropriate time
      void dim_check() {                   
        Serial.println("Dim Check");
      
        if(zero_cross == true) {              
          if(i>=dim) {                     
            digitalWrite(AC_pin, HIGH); // turn on light       
            i=0;  // reset time step counter                         
            zero_cross = false; //reset zero cross detection
          } 
          else {
            i++; // increment time step counter                     
          }                                
        }                                  
      }                                   
      
      void loop() {                        
        Serial.print("Dim value: ");
        Serial.println(String(dim));
        
        dim+=inc;
        if((dim>=128) || (dim<=0))
          inc*=-1;
        delay(18);
      }
      
      
      
      YveauxY Offline
      YveauxY Offline
      Yveaux
      Mod
      wrote on last edited by
      #7

      @Mike-Cayouette if you don't have MY_DEBUG defined you should call Serial.begin() with the Baudelaire yourself.

      http://yveaux.blogspot.nl

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mike Cayouette
        wrote on last edited by Mike Cayouette
        #8

        @Yveaux There is something wrong with the Serial print command. I have the most basic of sketches below but I see none on the Serial.println() output, but I get all the debug output.

        #define MY_DEBUG 
        
        #define MY_RADIO_NRF24
        
        #define SN "ACDimmer"
        #define SV "1.0"
        
        #include <SPI.h>
        #include <MySensors.h>
        
        int AC1_ID = 1;
        
        MyMessage dimmerMsg(AC1_ID, V_DIMMER);
        
        void setup() {                                        // Begin setup
          Serial.println("Dim Sketch started");
        }
        
        void presentation() {
          // Register the LED Dimmable Light with the gateway
          present( AC1_ID, S_DIMMER );
          
          sendSketchInfo(SN, SV);
        }
        
        
        void loop() {                        
          Serial.println("Loop"); 
        }
        
        

        what am I doing wrong?

        Mike

        TheoLT 1 Reply Last reply
        0
        • M Mike Cayouette

          @Yveaux There is something wrong with the Serial print command. I have the most basic of sketches below but I see none on the Serial.println() output, but I get all the debug output.

          #define MY_DEBUG 
          
          #define MY_RADIO_NRF24
          
          #define SN "ACDimmer"
          #define SV "1.0"
          
          #include <SPI.h>
          #include <MySensors.h>
          
          int AC1_ID = 1;
          
          MyMessage dimmerMsg(AC1_ID, V_DIMMER);
          
          void setup() {                                        // Begin setup
            Serial.println("Dim Sketch started");
          }
          
          void presentation() {
            // Register the LED Dimmable Light with the gateway
            present( AC1_ID, S_DIMMER );
            
            sendSketchInfo(SN, SV);
          }
          
          
          void loop() {                        
            Serial.println("Loop"); 
          }
          
          

          what am I doing wrong?

          Mike

          TheoLT Online
          TheoLT Online
          TheoL
          Contest Winner
          wrote on last edited by
          #9

          @Mike-Cayouette Maybe this helps

          void setup() {                                        // Begin setup
            Serial.begin( 115200 );
            Serial.println("Dim Sketch started");
          }
          
          M 1 Reply Last reply
          0
          • TheoLT TheoL

            @Mike-Cayouette Maybe this helps

            void setup() {                                        // Begin setup
              Serial.begin( 115200 );
              Serial.println("Dim Sketch started");
            }
            
            M Offline
            M Offline
            Mike Cayouette
            wrote on last edited by
            #10

            @TheoL I tried that also but I thought that was not needed if MY_DEBUG was defined. In any case, event with Serial.begin( 115200 ) it still does not work.

            I did notice the following 2 errors in the debug output, one after the other.

            TSP:CHKUPL:FAIL (hops=255)
            !TSM:UPL:FAIL
            

            but I see my sensor registered on my MQTT server.

            Thank you,

            Mike

            TheoLT YveauxY 2 Replies Last reply
            0
            • M Mike Cayouette

              @TheoL I tried that also but I thought that was not needed if MY_DEBUG was defined. In any case, event with Serial.begin( 115200 ) it still does not work.

              I did notice the following 2 errors in the debug output, one after the other.

              TSP:CHKUPL:FAIL (hops=255)
              !TSM:UPL:FAIL
              

              but I see my sensor registered on my MQTT server.

              Thank you,

              Mike

              TheoLT Online
              TheoLT Online
              TheoL
              Contest Winner
              wrote on last edited by
              #11

              @Mike-Cayouette I think you won't be getting any serial output from the println's because the node cannot get a handshake with the gateway. But I'm still not familiar with the new debug output.

              1 Reply Last reply
              0
              • M Mike Cayouette

                @TheoL I tried that also but I thought that was not needed if MY_DEBUG was defined. In any case, event with Serial.begin( 115200 ) it still does not work.

                I did notice the following 2 errors in the debug output, one after the other.

                TSP:CHKUPL:FAIL (hops=255)
                !TSM:UPL:FAIL
                

                but I see my sensor registered on my MQTT server.

                Thank you,

                Mike

                YveauxY Offline
                YveauxY Offline
                Yveaux
                Mod
                wrote on last edited by
                #12

                @Mike-Cayouette try putting the code suggested by @theol in the before() method instead of in setup()

                http://yveaux.blogspot.nl

                M 1 Reply Last reply
                1
                • YveauxY Yveaux

                  @Mike-Cayouette try putting the code suggested by @theol in the before() method instead of in setup()

                  M Offline
                  M Offline
                  Mike Cayouette
                  wrote on last edited by
                  #13

                  @Yveaux There is still something odd happening. This is now my basic sketch

                  //#define MY_DEBUG 
                  
                  #define MY_RADIO_NRF24
                  
                  //#define MY_NODE_ID 300
                  #define SN "ACDimmer"
                  #define SV "1.0"
                  
                  #include <SPI.h>
                  #include <MySensors.h>
                  
                  unsigned long previousMillis = 0;      
                  unsigned long currentMillis = 0;
                  int AC1_ID = 1; 
                  int SET_DELAY = 300;
                  
                  MyMessage dimmerMsg(AC1_ID, V_DIMMER);
                  
                  void before() {
                    Serial.begin( 115200 );
                    Serial.println("Dim Sketch started");
                   
                  }
                  
                  void setup() {                                        // Begin setup
                  }
                  
                  void presentation() {
                    // Register the LED Dimmable Light with the gateway
                    present( AC1_ID, S_DIMMER );
                    
                    sendSketchInfo(SN, SV);
                  }
                  
                  
                  void loop() 
                  {                        
                   Serial.println("Loop"); 
                   if (currentMillis - previousMillis > SET_DELAY) {
                      send( dimmerMsg.set("Message From Dimmer") );
                      previousMillis = millis();
                    }
                      currentMillis = millis();
                  
                  
                  }
                  
                  

                  I get the output that is in the before() method, but what I have in the loop is not output at all. Its as if loop is not running at all.

                  Mike

                  YveauxY 1 Reply Last reply
                  0
                  • M Mike Cayouette

                    @Yveaux There is still something odd happening. This is now my basic sketch

                    //#define MY_DEBUG 
                    
                    #define MY_RADIO_NRF24
                    
                    //#define MY_NODE_ID 300
                    #define SN "ACDimmer"
                    #define SV "1.0"
                    
                    #include <SPI.h>
                    #include <MySensors.h>
                    
                    unsigned long previousMillis = 0;      
                    unsigned long currentMillis = 0;
                    int AC1_ID = 1; 
                    int SET_DELAY = 300;
                    
                    MyMessage dimmerMsg(AC1_ID, V_DIMMER);
                    
                    void before() {
                      Serial.begin( 115200 );
                      Serial.println("Dim Sketch started");
                     
                    }
                    
                    void setup() {                                        // Begin setup
                    }
                    
                    void presentation() {
                      // Register the LED Dimmable Light with the gateway
                      present( AC1_ID, S_DIMMER );
                      
                      sendSketchInfo(SN, SV);
                    }
                    
                    
                    void loop() 
                    {                        
                     Serial.println("Loop"); 
                     if (currentMillis - previousMillis > SET_DELAY) {
                        send( dimmerMsg.set("Message From Dimmer") );
                        previousMillis = millis();
                      }
                        currentMillis = millis();
                    
                    
                    }
                    
                    

                    I get the output that is in the before() method, but what I have in the loop is not output at all. Its as if loop is not running at all.

                    Mike

                    YveauxY Offline
                    YveauxY Offline
                    Yveaux
                    Mod
                    wrote on last edited by Yveaux
                    #14

                    @Mike-Cayouette that's because the mysensors stack doesn't start (e.g radio fails to initialize, or parent can not be found)
                    That is probably also causing setup() not to be called and your timer not being registered!

                    http://yveaux.blogspot.nl

                    M 1 Reply Last reply
                    0
                    • YveauxY Yveaux

                      @Mike-Cayouette that's because the mysensors stack doesn't start (e.g radio fails to initialize, or parent can not be found)
                      That is probably also causing setup() not to be called and your timer not being registered!

                      M Offline
                      M Offline
                      Mike Cayouette
                      wrote on last edited by
                      #15

                      @Yveaux I find it hard to believe that the radio is not initialized. My MQTT server receives the following when I have the node running:

                      mymqtt-out/44/255/3/0/24
                      

                      I modified the sketch and add the following receive method

                      void receive(const MyMessage &message) {
                      
                        Serial.print("Message: ");
                        Serial.println(message.type);
                        Serial.print("Message Data: ");
                        Serial.println(message.data);
                          
                      }
                      

                      when I send a payload of 50 to mymqtt-in/44/1/1/0/3, the serial output shows the following:

                      Dim Sketch started
                      Message: 3
                      Message Data: 50
                      

                      I would think if the radio is not initialized this would not be possible.

                      My debug output is below:

                      Dim Sketch started
                      Starting sensor (RNNNA-, 2.0.0)
                      TSM:INIT
                      TSM:RADIO:OK
                      TSP:ASSIGNID:OK (ID=44)
                      TSM:FPAR
                      TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
                      TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0
                      TSP:MSG:FPAR RES (ID=0, dist=0)
                      TSP:MSG:PAR OK (ID=0, dist=1)
                      TSM:FPAR:OK
                      TSM:ID
                      TSM:CHKID:OK (ID=44)
                      TSM:UPL
                      TSP:PING:SEND (dest=0)
                      TSP:MSG:SEND 44-44-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1
                      TSP:CHKUPL:FAIL (hops=255)
                      !TSM:UPL:FAIL
                      TSM:FPAR
                      TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
                      TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0
                      TSP:MSG:FPAR RES (ID=0, dist=0)
                      TSP:MSG:PAR OK (ID=0, dist=1)
                      TSM:FPAR:OK
                      TSM:ID
                      

                      I remember in the past when the radio would not initialize it said radio init failed or something like that.

                      Thank you,

                      Mike

                      YveauxY 1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mike Cayouette
                        wrote on last edited by
                        #16

                        @Yveaux I solved the problem.

                        The issue was not my sketch, but rather my ESP8266 MQTT Gateway. I was using an older development version. After upgrading to version 2.0 everything started working. Thankfully my older nodes, using the older development version, are still working, I will upgrade them over time.

                        This is my final sketch:

                        #define MY_DEBUG 
                        
                        #define MY_RADIO_NRF24
                        
                        #define MY_NODE_ID 300
                        
                        #define SN "ACDimmer"
                        #define SV "1.0"
                        #define AC1_ID 1
                        #define FADE_DELAY 18      // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
                        
                        #include <TimerOne.h>
                        #include <SPI.h>
                        #include <MySensors.h>
                        
                        MyMessage dimmerMsg(AC1_ID, V_DIMMER);
                        
                        volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
                        volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
                        int AC_pin = 4;                // Output to Opto Triac
                        int intPin = 3;
                        int dim = 0;                    // Dimming level (0-128)  0 = on, 128 = 0ff
                        int inc=1;                      // counting up or down, 1=up, -1=down
                        int freqStep = 65;    // This is the delay-per-brightness step in microseconds.
                        unsigned long previousMillis = 0;        // will store last time LED was updated
                        unsigned long currentMillis = 0;
                        int currentLevel =  0;
                        int requestedLevel = 0;
                        
                        void before() {
                          #ifdef MY_DEBUG
                          Serial.println("Dimmer Node Starting");
                          #endif
                        
                          pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
                          attachInterrupt(digitalPinToInterrupt(intPin), zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
                          Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
                          Timer1.attachInterrupt(dim_check, freqStep);      
                        }
                        
                        void setup() {                                      // Begin setup
                        }
                        
                        void presentation() {
                          present( AC1_ID, S_DIMMER );
                          
                          sendSketchInfo(SN, SV);
                        }
                        
                        void zero_cross_detect() {    
                          zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
                          i=0;
                          digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
                        }                                 
                        
                        // Turn on the TRIAC at the appropriate time
                        void dim_check() {                   
                          if(zero_cross == true) {              
                            if(i>=currentLevel) {                     
                              digitalWrite(AC_pin, HIGH); // turn on light       
                              i=0;  // reset time step counter                         
                              zero_cross = false; //reset zero cross detection
                            } 
                            else {
                              i++; // increment time step counter                     
                            }                                
                          }                                  
                        }                                   
                        
                        void loop() {
                                                 
                        }
                        
                        
                        void receive(const MyMessage &message) {
                        
                          #ifdef MY_DEBUG
                          Serial.print("Message: ");
                          Serial.println(message.type);
                          Serial.print("Message Data: ");
                          Serial.println(message.data);
                          #endif
                        
                          if (message.type == 3) {
                            int requestedLevel = map(atoi( message.data ), 0, 100, 128, 0); //128 = off | 0 = ON
                        
                            while (currentLevel != requestedLevel) {
                          
                              if(currentLevel<=requestedLevel) inc=1;
                              if(currentLevel>=requestedLevel) inc=-1;
                        
                              currentLevel+=inc;
                        
                              #ifdef MY_DEBUG
                              Serial.print("Current Level:");
                              Serial.println(String(currentLevel));
                              #endif
                        
                              //delay
                              currentMillis = previousMillis = millis();
                              while (currentMillis - previousMillis < FADE_DELAY) {
                                currentMillis = millis();
                              }
                        
                            }
                        
                          }
                        }
                        

                        There is still one error though. When I try to set my own node id using #define MY_NODE_ID 300, I get the following error:

                        In file included from /home/sketchbook/libraries/MySensors/MySensors.h:253:0,
                                         from /home/sketchbook/ACDimmer_Test/ACDimmer_Test.ino:11:
                        /home/sketchbook/libraries/MySensors/core/MyTransport.cpp: In function 'void stInitTransition()':
                        /home/sketchbook/libraries/MySensors/core/MyTransport.cpp:74:16: warning: large integer implicitly truncated to unsigned type [-Woverflow]
                             _nc.nodeId = MY_NODE_ID;
                                        ^
                        In file included from /home/sketchbook/libraries/MySensors/core/MyHwATMega328.cpp:22:0,
                                         from /home/sketchbook/libraries/MySensors/MySensors.h:69,
                                         from /home/sketchbook/ACDimmer_Test/ACDimmer_Test.ino:11:
                        /home/sketchbook/libraries/MySensors/core/MyHwATMega328.h:69:88: warning: large integer implicitly truncated to unsigned type [-Woverflow]
                          #define hwWriteConfig(__pos, __value) (eeprom_update_byte((uint8_t*)(__pos), (__value)))
                                                                                                                ^
                        /home/sketchbook/libraries/MySensors/core/MyTransport.cpp:76:5: note: in expansion of macro 'hwWriteConfig'
                             hwWriteConfig(EEPROM_NODE_ID_ADDRESS, MY_NODE_ID);
                        

                        I was able to set my own node id's in the older development version but it does not seem to work now.

                        Thank you for all your help.

                        Mike

                        mfalkviddM 1 Reply Last reply
                        1
                        • M Mike Cayouette

                          @Yveaux I solved the problem.

                          The issue was not my sketch, but rather my ESP8266 MQTT Gateway. I was using an older development version. After upgrading to version 2.0 everything started working. Thankfully my older nodes, using the older development version, are still working, I will upgrade them over time.

                          This is my final sketch:

                          #define MY_DEBUG 
                          
                          #define MY_RADIO_NRF24
                          
                          #define MY_NODE_ID 300
                          
                          #define SN "ACDimmer"
                          #define SV "1.0"
                          #define AC1_ID 1
                          #define FADE_DELAY 18      // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
                          
                          #include <TimerOne.h>
                          #include <SPI.h>
                          #include <MySensors.h>
                          
                          MyMessage dimmerMsg(AC1_ID, V_DIMMER);
                          
                          volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
                          volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
                          int AC_pin = 4;                // Output to Opto Triac
                          int intPin = 3;
                          int dim = 0;                    // Dimming level (0-128)  0 = on, 128 = 0ff
                          int inc=1;                      // counting up or down, 1=up, -1=down
                          int freqStep = 65;    // This is the delay-per-brightness step in microseconds.
                          unsigned long previousMillis = 0;        // will store last time LED was updated
                          unsigned long currentMillis = 0;
                          int currentLevel =  0;
                          int requestedLevel = 0;
                          
                          void before() {
                            #ifdef MY_DEBUG
                            Serial.println("Dimmer Node Starting");
                            #endif
                          
                            pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
                            attachInterrupt(digitalPinToInterrupt(intPin), zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
                            Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
                            Timer1.attachInterrupt(dim_check, freqStep);      
                          }
                          
                          void setup() {                                      // Begin setup
                          }
                          
                          void presentation() {
                            present( AC1_ID, S_DIMMER );
                            
                            sendSketchInfo(SN, SV);
                          }
                          
                          void zero_cross_detect() {    
                            zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
                            i=0;
                            digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
                          }                                 
                          
                          // Turn on the TRIAC at the appropriate time
                          void dim_check() {                   
                            if(zero_cross == true) {              
                              if(i>=currentLevel) {                     
                                digitalWrite(AC_pin, HIGH); // turn on light       
                                i=0;  // reset time step counter                         
                                zero_cross = false; //reset zero cross detection
                              } 
                              else {
                                i++; // increment time step counter                     
                              }                                
                            }                                  
                          }                                   
                          
                          void loop() {
                                                   
                          }
                          
                          
                          void receive(const MyMessage &message) {
                          
                            #ifdef MY_DEBUG
                            Serial.print("Message: ");
                            Serial.println(message.type);
                            Serial.print("Message Data: ");
                            Serial.println(message.data);
                            #endif
                          
                            if (message.type == 3) {
                              int requestedLevel = map(atoi( message.data ), 0, 100, 128, 0); //128 = off | 0 = ON
                          
                              while (currentLevel != requestedLevel) {
                            
                                if(currentLevel<=requestedLevel) inc=1;
                                if(currentLevel>=requestedLevel) inc=-1;
                          
                                currentLevel+=inc;
                          
                                #ifdef MY_DEBUG
                                Serial.print("Current Level:");
                                Serial.println(String(currentLevel));
                                #endif
                          
                                //delay
                                currentMillis = previousMillis = millis();
                                while (currentMillis - previousMillis < FADE_DELAY) {
                                  currentMillis = millis();
                                }
                          
                              }
                          
                            }
                          }
                          

                          There is still one error though. When I try to set my own node id using #define MY_NODE_ID 300, I get the following error:

                          In file included from /home/sketchbook/libraries/MySensors/MySensors.h:253:0,
                                           from /home/sketchbook/ACDimmer_Test/ACDimmer_Test.ino:11:
                          /home/sketchbook/libraries/MySensors/core/MyTransport.cpp: In function 'void stInitTransition()':
                          /home/sketchbook/libraries/MySensors/core/MyTransport.cpp:74:16: warning: large integer implicitly truncated to unsigned type [-Woverflow]
                               _nc.nodeId = MY_NODE_ID;
                                          ^
                          In file included from /home/sketchbook/libraries/MySensors/core/MyHwATMega328.cpp:22:0,
                                           from /home/sketchbook/libraries/MySensors/MySensors.h:69,
                                           from /home/sketchbook/ACDimmer_Test/ACDimmer_Test.ino:11:
                          /home/sketchbook/libraries/MySensors/core/MyHwATMega328.h:69:88: warning: large integer implicitly truncated to unsigned type [-Woverflow]
                            #define hwWriteConfig(__pos, __value) (eeprom_update_byte((uint8_t*)(__pos), (__value)))
                                                                                                                  ^
                          /home/sketchbook/libraries/MySensors/core/MyTransport.cpp:76:5: note: in expansion of macro 'hwWriteConfig'
                               hwWriteConfig(EEPROM_NODE_ID_ADDRESS, MY_NODE_ID);
                          

                          I was able to set my own node id's in the older development version but it does not seem to work now.

                          Thank you for all your help.

                          Mike

                          mfalkviddM Offline
                          mfalkviddM Offline
                          mfalkvidd
                          Mod
                          wrote on last edited by
                          #17

                          @Mike-Cayouette node id needs to be in the range of 1..254.

                          1 Reply Last reply
                          0
                          • M Mike Cayouette

                            @Yveaux I find it hard to believe that the radio is not initialized. My MQTT server receives the following when I have the node running:

                            mymqtt-out/44/255/3/0/24
                            

                            I modified the sketch and add the following receive method

                            void receive(const MyMessage &message) {
                            
                              Serial.print("Message: ");
                              Serial.println(message.type);
                              Serial.print("Message Data: ");
                              Serial.println(message.data);
                                
                            }
                            

                            when I send a payload of 50 to mymqtt-in/44/1/1/0/3, the serial output shows the following:

                            Dim Sketch started
                            Message: 3
                            Message Data: 50
                            

                            I would think if the radio is not initialized this would not be possible.

                            My debug output is below:

                            Dim Sketch started
                            Starting sensor (RNNNA-, 2.0.0)
                            TSM:INIT
                            TSM:RADIO:OK
                            TSP:ASSIGNID:OK (ID=44)
                            TSM:FPAR
                            TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
                            TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0
                            TSP:MSG:FPAR RES (ID=0, dist=0)
                            TSP:MSG:PAR OK (ID=0, dist=1)
                            TSM:FPAR:OK
                            TSM:ID
                            TSM:CHKID:OK (ID=44)
                            TSM:UPL
                            TSP:PING:SEND (dest=0)
                            TSP:MSG:SEND 44-44-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1
                            TSP:CHKUPL:FAIL (hops=255)
                            !TSM:UPL:FAIL
                            TSM:FPAR
                            TSP:MSG:SEND 44-44-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
                            TSP:MSG:READ 0-0-44 s=255,c=3,t=8,pt=1,l=1,sg=0:0
                            TSP:MSG:FPAR RES (ID=0, dist=0)
                            TSP:MSG:PAR OK (ID=0, dist=1)
                            TSM:FPAR:OK
                            TSM:ID
                            

                            I remember in the past when the radio would not initialize it said radio init failed or something like that.

                            Thank you,

                            Mike

                            YveauxY Offline
                            YveauxY Offline
                            Yveaux
                            Mod
                            wrote on last edited by
                            #18

                            @Mike-Cayouette well, suggesting the radio fails was just one of the many possible failures as I indicated.
                            Great to hear you finally nailed it!

                            http://yveaux.blogspot.nl

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mike Cayouette
                              wrote on last edited by Mike Cayouette
                              #19

                              @mfalkvidd Node id's work now.

                              Thank you everyone for your help.

                              Mike

                              1 Reply Last reply
                              1
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              16

                              Online

                              11.7k

                              Users

                              11.2k

                              Topics

                              113.0k

                              Posts


                              Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • OpenHardware.io
                              • Categories
                              • Recent
                              • Tags
                              • Popular