Hey, guys were wondering if someone could help me get a sensor to change the brightness of my led strip.
here the code I've made so far.
#include <FastLED.h>
define TSL235R 2
#define LED_PIN 5
#define NUM_LEDS 20
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// Constants
int period = 1000; // Miliseconds of each light frecuency measurement
int ScalingFactor = 1; // Scaling factor of this sensor
float area = 0.0092;
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
// Variable to hold sensor value
int sensorValue;
// LED pin (PMW~)
const int ledPin = 5;
// Min value to turn on the LED (beteween 0 and 1023)
const int LedOn_StartMinLightLevel = 150;
unsigned long counter = 0; // Counter of measurements during the test
unsigned long currentTime = millis();
unsigned long startTime = currentTime;
volatile long pulses = 0; // Counter of measurements of the TSL235R
unsigned long frequency; // Read the frequency from the digital pin (pulses/second)
float irradiance; // Calculated irradiance (uW/cm2)
void setup() {
// Make the LED pin an output and turn it off
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Start up serial connection for monitoring
Serial.begin(9600); // baud rate
Serial.flush();
Serial.begin(9600); // Start and configure the serial port
attachInterrupt(0, PulseCount, RISING);
pinMode(TSL235R, INPUT); // Declare the pin such as an input of data
Serial.println("Testing a TSL235R sensor:"); // Splash screen
Serial.println("-------------------------");
Serial.println();
}
void loop() {
//Read the input from A0 and store it in the variable
sensorValue = digitalRead(2);
Serial.println(sensorValue); // print it also to serial port to monitor it
// AnalogRead provides value beteween 0 and 1023
// We must invert that value so LED must be brigther in low lighting readings
int outputValue = 1023 - sensorValue;
// Set output value to 0 when not reached the LedOn_StartMinLightLevel
if(outputValue < LedOn_StartMinLightLevel)
{
outputValue = 0;
}
// Convert Analog Value (beteween 0 and 1023) to Analog Output (beteween 0 and 255)
int resultValue = map(outputValue, 255, 1023, 255, 255);
// Set ResultValue to LED
analogWrite(ledPin, resultValue);
// Some Delay to Serial/Refresh
delay(100);
}
void PulseCount()
{
pulses++;
}
unsigned long getfrequency () {
noInterrupts();
frequency = pulses /(period/1000); // Calculate the frequency (pulses/second)
interrupts();
return (frequency);
}
float getirradiance () {
irradiance = frequency / area; // Calculate Irradiance (uW/cm2)
return (irradiance);
}