Start/stop sequence



  • Hi,

    I have an Arduino which controls a relay with 8 channels. The channels are switching on and off one after another. Just like this, very simple:

    digitalWrite(RELAY1,LOW);
    delay(2000);                   
    digitalWrite(RELAY1,HIGH); 
     
    digitalWrite(RELAY2,LOW); 
    delay(2000); 
    digitalWrite(RELAY2,HIGH);
    

    Now I want to control this application over RF/MySensors so that I can send a signal over RF to the Arduino to start or stop the sequence.
    My first idea was just to extend the RelayActuator-code by an if-loop. I used gw.loadState to check whether the sequence should start or not, but this is truly wrong..

    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // 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
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.0");
    
      // Fetch relay status
      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)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
      if (gw.loadState(1)==1) {
        Serial.println("on");
        digitalWrite(RELAY_1,LOW);
      }
      if (gw.loadState(1)==0) {
        Serial.println("off");
        digitalWrite(RELAY_1,HIGH);
      }
      delay(100);
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.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());
       } 
    }
    

    Any idea how I get this working?

    Thanks!



  • I dont have access to test your code now but what I see that could be wrong is using delay in your code. Delay will stop executing of the code for 100 ms. This is a lot.

    Search for non blocking delay examplew that uses timers to implement a delay. I guess also that there is a library for that which i saw in one of the examples of mysensors library.


Log in to reply
 

Suggested Topics

  • 1
  • 6
  • 5
  • 10
  • 1
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts