Motion, Lux, RGB



  • Hey guys, I'm putting together a 3 in 1 motion, lux, RGB sketch. The problem I'm having is it compiles, but there seems to be a delay in motion and RGB doesn't work. When I compile Motion and RGB only, everything works fine. Could someone check my script out...```

    
    #define SN "RGB"
    #define SV "1.0"
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Wire.h>
    #include <BH1750.h>
    
    #define RADIO_ID  9
    #define RED       5 // pin for red 
    #define GREEN     6 // pin for green 
    #define BLUE      3 // pin for blue
    #define FADE_DELAY 30 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim) 
    #define Motion_CHILD 11 // Id of the MotionSensor child
    #define Motion_PIN 7 // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT Motion_PIN-2 //#define INTERRUPT Motion_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID_LIGHT 0 // Id of the LuxSensor child
    #define LIGHT_SENSOR_ANALOG_PIN A0 // pin for lux sensor
    
    boolean lastMotion = false;
    BH1750 lightSensor;
    //unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    
    MySensor gw;
    
    
    static int currentLevel = 0;       // Current dim level...
    long previousMillisLED = 0;        // stores last time LED was updated
    long durationLED = 1800000;        // Max 'On'-time for LED's (watchdog)
    MyMessage motionMsg(Motion_CHILD, V_TRIPPED);
    
    MyMessage luxMsg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    uint16_t lastlux;
    
    void setup()
    
    {
        //Serial.begin(BAUD_RATE);  // Used to write debug info
        gw.begin(setDimmerStatus, RADIO_ID); // Send the Sketch Version Information to the Gateway
       
        
        gw.sendSketchInfo("Light Lux Sensor", SN, SV);
        
        lightSensor.begin();
         
        // Register all sensors to gw (they will be created as child devices)
        gw.present(RED, S_DIMMER);
        gw.present(GREEN, S_DIMMER);
        gw.present(BLUE, S_DIMMER);
        gw.present( Motion_CHILD, S_MOTION );
        gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
        
        pinMode(RED, OUTPUT);
        pinMode(GREEN, OUTPUT);
        pinMode(BLUE, OUTPUT);
    	
        analogWrite(RED, 255 * gw.loadState(RED) / 100); //Get value from RAM and write to output
        analogWrite(GREEN, 255 * gw.loadState(GREEN) / 100);
        analogWrite(BLUE, 255 * gw.loadState(BLUE) / 100);
    	
    
        // Request/wait for dimmer status
          /*
         gw.request(RED, V_DIMMER);
         gw.process();
         gw.request(GREEN, V_DIMMER);
         gw.process();
         gw.request(BLUE, V_DIMMER);
         gw.process();
          */
    
    }
    
    void loop()
    {
      gw.process();
    
      // Read digital motion value
      boolean motion = digitalRead(Motion_PIN) == HIGH; 
      if (lastMotion != motion) {
      lastMotion = motion;     
       gw.send(motionMsg.set(motion ? "1" : "0" ));  // Send motion value to gw
      }
    {  
      uint16_t lux = lightSensor.readLightLevel();// Get Lux value
      Serial.println(lux);
      if (lux != lastlux) {
          gw.send(luxMsg.set(lux));
          lastlux = lux;
      }
        //gw.sleep(SLEEP_TIME);
    
    }
    
    }
    
    void setDimmerStatus(const MyMessage &message)
    {
    	if (message.type == V_DIMMER)
    	{
    	uint8_t incomingDimmerStatus = message.getByte();
    		// Change Dimmer level
    
    	analogWrite(message.sensor, 255 * incomingDimmerStatus / 100);
    	gw.saveState(message.sensor, message.getByte()); //Save value to RAM
    	// Write some debug info
    	//Serial.print("Incoming change for dimmer on pin:");
    	//Serial.print(message.header.childId);
    	//Serial.print(", New status: ");
    	//Serial.println(incomingDimmerStatus);
    
      }
    }
    
           // This method provides a graceful fade up/down effect
         void fadeToLevel( int toLevel ) {
         if (toLevel > 0){ previousMillisLED = millis();}
         int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
         while ( currentLevel != toLevel ) {
         currentLevel += delta;
         analogWrite( RED, (int)(currentLevel / 100. * 255) );
         analogWrite( GREEN, (int)(currentLevel / 100. * 255) );
         analogWrite( BLUE, (int)(currentLevel / 100. * 255) );
         delay( FADE_DELAY );
    
      } 
    }
    
    

  • Admin

    Please edit your post and use the "code format" button in composer.

    I don't see any presentations and you're using the wrong sleep function. Look at the motion sensor example for help.



  • @hek good tip, I was wondering how to work the "code format". Anyway, I just made some changes to the script above. And it seems to be working now except Fade dim. I will appreciate if you could check to make sure I'm on the right path.

    Also, I was wondering is it possible to include Motion, RGB, Lux & Co2 sensor in one sketch?

    Thank you.



  • You are missing something

    MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);
    MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
    

    You just process the messages for:

    MyMessage motionMsg(Motion_CHILD, V_TRIPPED); ////Motion
    
    MyMessage luxMsg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);////Lux
    

Log in to reply
 

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