Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!




Boblight - add ambi...
 
Share:
Notifications
Clear all

Boblight - add ambient light detection

Page 2 / 2

(@castaway)
New Member
Joined: 5 years ago
Posts: 0
 

researching in progress if i set 10000 its immediatly react on light..if i set 40000 or more then its does not react at all even if i wait long time. Now i hide LDR away from light emited by LEDs. I have idea if frequency(blinking) of leds dont mess this and light from LED strip affecting LDR even if i dont see it by eye. If i point LDR directly on light of LED strip it start blinking immediatly so now i do my best to hide LDR from this light and will see how this will do. Thank you my friend for help. Interned need more friendly helpfull guys like you hans :) hope this project never die <3 will report back as i find out something new.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

If you go beyond 32768, then you should probably use a long (range: -2,147,483,648 to 2,147,483,647), unsigned long (range: 0 to 4,294,967,295) or an unsigned int (range: 0 to 65,535) instead of an int (range: -32,768 to 32,767).

Waiting for an int to become 40,000 will never happen, hence no response to light.

Example:

long BrightnessCounter 0;

ReplyQuote
(@castaway)
New Member
Joined: 5 years ago
Posts: 0
 

hello my friend ITS WORKING AWESOME! its working EXACTLY how i want it to work! no problem. I fixed problem by perfectly hiding sensor so no chance for LED strip light touching it. After that modification i tested it in real life and it was just perfect ambilight started automatically when dust come. thank you for instructions i must say you are really good person! Sending photo how i hide sensor. Everyone should have do this its so easy and working so perfect. I think code can be used without any counter (i had no tested that scenario yet) so to be clear in my opinion first code i posted should be working just fine. Now i need measure for potentiometer and hide it in to my DIY arduino nano box :) 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

Awesome!!! That's good news - I'm glad it's working!  

You're most welcome and thank you for the kind words.
I enjoy seeing others being successful with things like that - which makes it so much fun to maintain my website.
If I only could make a living this way 

So, I understand that the initial code worked, without the counter. It's just a matter of a good location for the LDR. Nice! 


ReplyQuote
(@castaway)
New Member
Joined: 5 years ago
Posts: 0
 

quick update:I wanted clean code and bring back initial code but problems apear again. So your code is needed! In my case initial code without counter is not working properly. I set 30000 and it working perfect again.


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

Thanks for the update!! 

Could you post the code you've used and that is working?


ReplyQuote
(@castaway)
New Member
Joined: 5 years ago
Posts: 0
 

this if final working code im using..BTW right resistance for me is 10Kohm 

/* Modified and commented by ai.rs
   t4a_boblight
   (C) 2014 Hans Luijten, www.tweaking4all.com
   t4a_boblight is free software and can be distributed and/or modified
   freely as long as the copyright notice remains in place.
   Nobody is allowed to charge you for this code.
   Use of this code is entirely at your own risk.
*/
#include "Adafruit_NeoPixel.h"
// DEFINITIONS
#define STARTCOLOR 0x333333 // LED colors at start
#define BLACK 0x000000 // LED color BLACK
#define DATAPIN 5 // Datapin
#define LEDCOUNT 202 // Number of LEDs used for boblight
// LEDCOUNT value is local value in Arduino sketch, for hyperion it doesn't matter it sends prefx characters according to hyperion config
#define SHOWDELAY 200 // Delay in micro seconds before showing default 200
#define BAUDRATE 500000// Serial port speed, 460800 tested with Arduino Uno R3 23400 za MEGA, 115200 nano
//#define BRIGHTNESS 70 // Max. brightness in %
#define LDRpin 8 //pin kde je senzor
//Hyperion sends prefix characters based on number of LEDs in config file
// e.g. for 181 LEDs it will send 0xB4 and cheksum 0xE1
// keep in mind if you are using boblight config to calculate prefix that Boblight counts diodes from 1 and Hyperion from 0
// if you have problems try +1 or -1 diodes when generating prefix characters
// values to save some time: 178 B1 E4, 180 B3E6, 181 B4E1, 182 B5E0
//hyperion code
//_ledBuffer[3] = ((ledValues.size() - 1) >> 8) & 0xFF; // LED count high byte
// _ledBuffer[4] = (ledValues.size() - 1) & 0xFF; // LED count low byte
// _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0xC9, 0x9C}; // Start prefix ADA
char buffer; // Temp buffer for receiving prefix data
// Init LED strand, WS2811/WS2912 specific
// These might work for other configurations:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);
int state; // Define current state
#define STATE_WAITING 1 // - Waiting for prefix
#define STATE_DO_PREFIX 2 // - Processing prefix
#define STATE_DO_DATA 3 // - Handling incoming LED colors
int readSerial; // Read Serial data (1)
int currentLED; // Needed for assigning the color to the right LED
int BRIGHTNESS = 70; // make sure this stays within 0...100 TU
int BrightnessCounter = 0;
#define MaxBrightnessCount 30000
void setup()
{
  strip.begin(); // Init LED strand, set all black, then all to startcolor
  strip.setBrightness( (255 / 100) * BRIGHTNESS );
  setAllLEDs(BLACK, 0);
  setAllLEDs(STARTCOLOR, 5);
  Serial.begin(BAUDRATE); // Init serial speed
  state = STATE_WAITING; // Initial state: Waiting for prefix
}

void loop()
{
  
    switch (state)
    {
      case STATE_WAITING: // Waiting for prefix
        if ( Serial.available() > 0 )
        {
          readSerial = Serial.read(); // Read one character
          if ( readSerial == prefix[0] ) // if this character is 1st prefix char
          {
            state = STATE_DO_PREFIX; // then set state to handle prefix
          }
        }
        break;

      case STATE_DO_PREFIX: // Processing Prefix
        if ( Serial.available() > sizeof(prefix) - 2 )
        {
          Serial.readBytes(buffer, sizeof(prefix) - 1);
          for ( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++)
          {
            if ( buffer[Counter] == prefix[Counter + 1] )
            {
              state = STATE_DO_DATA; // Received character is in prefix, continue
              currentLED = 0; // Set current LED to the first one
            }
            else
            {
              state = STATE_WAITING; // Crap, one of the received chars is NOT in the prefix
              break; // Exit, to go back to waiting for the prefix
            } // end if buffer
          } // end for Counter
        } // end if Serial
        break;

      case STATE_DO_DATA: // Process incoming color data
        if ( Serial.available() > 2 ) // if we receive more than 2 chars
        {
          Serial.readBytes( buffer, 3 ); // Abuse buffer to temp store 3 charaters
          strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]); // and assing to LEDs
        }
        if ( currentLED > LEDCOUNT ) // Reached the last LED? Display it!
        {
          strip.show(); // Make colors visible
          delayMicroseconds(SHOWDELAY); // Wait a few micro seconds
          state = STATE_WAITING; // Reset to waiting ...
          currentLED = 0; // and go to LED one
          break; // and exit ... and do it all over again
        }
        break;
    } // switch(state)
    strip.setBrightness( (255 / 100) * BRIGHTNESS );
 AdjustBrightness();
  } // loop

  // Sets the color of all LEDs in the strand to 'color'
  // If 'wait'>0 then it will show a swipe from start to end
  void setAllLEDs(uint32_t color, int wait)
  {
    for ( int Counter = 0; Counter < LEDCOUNT; Counter++ ) // For each LED
    {
      strip.setPixelColor( Counter, color ); // .. set the color
      if ( wait > 0 ) // if a wait time was set then
      {
        strip.show(); // Show the LED color
        delay(wait); // and wait before we do the next LED
      } // if wait
    } // for Counter
    strip.show(); // Show all LEDs
  } // setAllLEDs
  void AdjustBrightness() {
  int LDRValue = 0; // result of reading the digital pin
  LDRValue = digitalRead(LDRpin);// read the value from the LDR
// if we detect the same brightness as before, reset counter
    if( ((LDRValue==1) && (BRIGHTNESS==0)) || ((LDRValue!=1) && (BRIGHTNESS==70)) ) {
      BrightnessCounter = 0; 
    }
    // However, if we exceeded the counter max, then set new brightness
    else if(BrightnessCounter>=MaxBrightnessCount) {
  if(LDRValue==1) { 
    BRIGHTNESS = 0;
    // Light, so set LED brightness to 0%
  }
  else { 
    BRIGHTNESS = 70;
    // Dark, so set LED brightness to 70%
  }
   }
  
   BrightnessCounter++; // increase counter
  } 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1832
Topic starter  

Thanks for posting the full code 


ReplyQuote
Page 2 / 2

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: