Skip to content
  • MySensors
  • 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. Development
  3. Motion, Lux, RGB

Motion, Lux, RGB

Scheduled Pinned Locked Moved Development
4 Posts 3 Posters 2.6k Views 1 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.
  • jeylitesJ Offline
    jeylitesJ Offline
    jeylites
    wrote on last edited by jeylites
    #1

    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 );
    
      } 
    }
    
    
    1 Reply Last reply
    0
    • H Offline
      H Offline
      hek
      Admin
      wrote on last edited by
      #2

      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.

      jeylitesJ 1 Reply Last reply
      0
      • H hek

        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.

        jeylitesJ Offline
        jeylitesJ Offline
        jeylites
        wrote on last edited by jeylites
        #3

        @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.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vladut Grecu
          wrote on last edited by
          #4

          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
          
          1 Reply Last reply
          0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          20

          Online

          12.0k

          Users

          11.2k

          Topics

          113.4k

          Posts


          Copyright 2025 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
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular