Nikola Travis
Published

Controlling LED light intensity using smartphone light senso

Light sensor and output on fading LED on a PWM pin

BeginnerFull instructions provided5,839
Controlling LED light intensity using smartphone light senso

Things used in this project

Schematics

Fade-LED_bb.png

Code

Untitled file

Arduino
#define CUSTOM_SETTINGS
#define INCLUDE_LIGHT_SENSOR_SHIELD

#include <OneSheeld.h>

long maxLightIntensity=0;
float ratio = 0;
long ledValue = 0;
long lightValue;
int ledPin = 10;

void setup()
{
  OneSheeld.begin();
  pinMode(ledPin,OUTPUT);
}

void loop()
{
  if(LightSensor.getValue() > maxLightIntensity)
  {
    maxLightIntensity = LightSensor.getValue();
    ratio = 255 / (float) maxLightIntensity;
  }
  lightValue = LightSensor.getValue();
  if(lightValue > maxLightIntensity)
  {
    lightValue = maxLightIntensity;
  }
  ledValue= 255 - lightValue * ratio;
  
  if(ledValue < 10)
  {
    analogWrite(ledPin,0);
  }
  else
  {
    analogWrite(ledPin,ledValue);  
  }
  
}

Codebender

Credits

Nikola Travis

Nikola Travis

1 project • 1 follower

Comments