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 no translation for that post!



The map function qu...
 
Share:
Notifications
Clear all

The map function question.

2 Posts
2 Users
0 Likes
237 Views
 Anonymous
Joined: 53 years ago
Posts: 0
Topic starter  

I have this sketch I've been working on...... yeah I know kinda simple but for me it's a win :)

I'm trying to use map function to dim lights as the ambient light increases.  I've tweaked all the settings in the map function but cannot get them to turn all the way off.   They do dim..... 

This is my project https://github.com/cowboysdude/Under_Cabinet    but still struggling to get this to work correctly...  

Any help/suggestions would be greatly appreciated!!!

Thank you!

 

#include <FastLED.h>

//LED strip
#define NUM_LEDS 10  //1 for testing only.  USE your actual numbers and remember you will need to power these from power supply, not the UNO or NANO
#define DATA_PIN 3

//LDR
#define LDR A0 //analog 0

CRGB leds[NUM_LEDS];

int ldr;
int ldrValue;
int sensorValue;

void setup(){
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(100); //set generic brightness at beginning
  pinMode(LDR, INPUT);
}

void loop(){
  ldr = analogRead(LDR);

  /*
  The first 2 numbers are the input low and high [0 Low, 1023 High for A0]. For an analogRead on a 10 bit ADC that is 0 for low and 1023 for high. 
  The other 2 numbers are for the numbers for the output numbers that correspond for the inputs [100 - Low brightness, 255 - to Max Brightness]. 
  So if you want 0 output for 0 input and 255 output for 1023 input the function would be:
   
   IE: outNumber = map(inNumber, 0, 1023, 0, 255);

   You can adjust the numbers to give the output that you want. For instance, what is the analogRead output when the LDR is full dark? What is the analogRead output when the LDR is full light? Use those numbers for the in numbers and 0 and 255 for the out numbers.
  
                  value: the number to map.
                  fromLow: the lower bound of the value’s current range.
                  fromHigh: the upper bound of the value’s current range.
                  toLow: the lower bound of the value’s target range.
                  toHigh: the upper bound of the value’s target range.
  */
  //ldrValue is based on how much light you have hitting your LDR mine is -5, you may have to adjust for your light source to turn off lights!
  
   ldrValue = map(ldr, 0, 1023, 75, 255);
   
   sensorValue = analogRead(LDR); 
 Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(ldrValue);

  FastLED.setBrightness(ldrValue);
  for (int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 255, 255);  //white color
    FastLED.show();
  }
  delay(2);  //repeat every 0.1 second
}

ReplyQuote
Topic Tags
map
 Hans
(@hans)
Famed Member Admin
Joined: 9 years ago
Posts: 2236
 

Hey there! 😊 

Well, my first guess would be that setBrightness only scales, and this may not result in zero (0) for the LED colors.

I do not have hardware laying around at the moment, but I'd try to see what happens if you force ldrvalue to zero.
Or better said: will this last part of the code make the strip dark (off) when forcing ldrvalue to zero

  // FastLED.setBrightness(ldrValue);
  FastLED.setBrightness( 0 );
  for (int i=0;i<NUM_LEDS;i++){
    leds[i].setRGB(255, 255, 255);  //white color
    FastLED.show();
  }

 

My guess: either it will not turn off the LED strip, or "ldrvalue" never reaches zero. I haven't seen your serial output ... so I don't know.

An option would be to use force LEDs OFF when "ldrvalue" goes below a certain threshold ... ?
I took "10" as an arbitrary number ...

For example:

  if(ldrvale<10) {
    for (int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(0, 0, 0);  //LEDs OFF one at a time
      FastLED.show();
    }
  } else {
    FastLED.setBrightness(ldrValue);
    for (int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(255, 255, 255);  //white color
      FastLED.show();
    }
  }

 

Or maybe like this (does the same thing - just looks cleaner):

  CRGB FinalColor;

  ...

  if(ldrvalue<10) {
    FinalColor = CRGB::Black;
  } else {
    FinalColor = CRGB::White;
  }
  
  FastLED.setBrightness(ldrValue);
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = FinalColor; 
    FastLED.show();
  }

 

 Note: if it still doesn't go dark, then try if moving the FastLED.setBrightness call in the if-then loop, something like this:

  CRGB FinalColor;

  ...

  if(ldrvalue<10) {
    FastLED.setBrightness(255);
    FinalColor = CRGB::Black;
  } else {
    FastLED.setBrightness(ldrValue);
    FinalColor = CRGB::White;
  }
  
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = FinalColor; 
    FastLED.show();
  }

 

Hope this helps 😊 


ReplyQuote
Share: