Dimming SuperBright LED - Proximity Sensor

Hi there,

I currently have a proximity sensor that works very with the code I got from barepaint. Essentially as proximity increases activity on the LED increases.

I also have a great code that works with a ‘push button’… that will dim and fade in and out an LED…

My question is how to combine the two?

I am looking to make a proximity sensor that holds a ‘dim’ effect on the LED superbright constantly… until proximity increases… and with that proximity increase id like the LED to become brighter… and fade back to a dim setting as proximity decreases…

I will only post the code for proximity sensor now.

#include <CapSense.h>

CapSense cs_4_2 = CapSense(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add Bare Paint

const int LEDPin = 13;
const int LEDThreshold = 15; // The higher the threshold, the closer you have to get to activate the LED

void setup() {
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);

pinMode(LEDPin, OUTPUT);
}

void loop() {
long start = millis();
long total = cs_4_2.capSense(30);

Serial.println(total);

if (total>=LEDThreshold) {
digitalWrite(LEDPin, HIGH);
}
else {
digitalWrite(LEDPin, LOW);
}

delay(10); // arbitrary delay to limit data to serial port
}

Take the value of total read from the sensor, use the map() function to change the range to 0 to 255 and use that as the PWM value written to the analogue pin that the LED is connected to.

Thank you for your help. without asking you to write the code for me... can you give me an example of how that would look? I am a complete beginner at programming

At the moment the code checks to see whether the variable total is greater than a threshold and turns on the LED if it is.

  if (total>=LEDThreshold) { 
    digitalWrite(LEDPin, HIGH); 
  } 
  else {
    digitalWrite(LEDPin, LOW);
  }

If you change LEDPin to use one of the PWM pins, say pin 10, then use analogWrite to output the value of total to it then the brightness will vary according to the value of total. So far, so good, but to get a full range of brightness from the LED the value used in analogWrite should be from 0 to 255. Judging from the value of LEDThreshold in your program (15) the range is currently much less than that, hence the need to use the map function to expand the restricted range available at the moment to 0 to 255.

Suggested changes, one at a time. Make LEDPin use pin 10. Change nothing else and make sure the program still works as now Use analogWrite() instead of digitalWrite() and output total to the LED. You may or may not see any variation in brightness and certainly not the full range. Add some Serial.prints so that you can see the value of total being output. Use the map() function to expand the output range, again using Serial.print to see the value.

great all is working well with analogwrite and PWM pin 10...

still have difficulties with the map function.. not exactly sure how to integrate it.. or rather.. where to integrate?

Anyone about that could answer this question? thanks!

You need to use the map() function after you have read the value from the sensor but before you output the value to the LED

read sensorValue
use map() to expand sensorValue to 0 - 255
analogWrite the expanded sensorValue to the LED