Controlling LEDs with the Input from AnalogPins

Hi everyone,
so basically i have been measuring Light with a Photodiode through an TIA. Now i want to control Leds depending on how much light is being detected. At the moment i have a solutin with 4 LEDS, which activate depending on the Light. But honestly im not satisfied with the solution because i would like to be able to dim the light. So the LED wouldnt just start glowing, at a certain point, but would be able to glow more or less depending on the measured Light. I hope its understandble what im talking about.[

tt]const int RunningAverageCount = 16; // Anzahl der in den Laufenden-Mettelwert aufgenommenen Messungen
float RunningAverageBuffer[RunningAverageCount];
int NextRunningAverage;
int LEDPin1 = 9;
int LEDPin2 = 10;
int LEDPin3 = 11;
int LEDPin4 = 12;

void setup() {

Serial.begin(9600); //Kommunikationsrate

}

void loop() {

int Light = analogRead(A0);

RunningAverageBuffer[NextRunningAverage++] = Light;
if (NextRunningAverage >= RunningAverageCount)
{
NextRunningAverage = 0;
}
float RunningAverageLight = 0;
for(int i=0; i< RunningAverageCount; ++i)
{
RunningAverageLight += RunningAverageBuffer*;*

  • }*
  • RunningAverageLight /= RunningAverageCount;*
  • Serial.println(RunningAverageLight);*
  • delay(10);*
  • int helligkeit;*

if(RunningAverageLight <=125){

  • analogWrite(LEDPin1, 255);*
  • analogWrite(LEDPin2, 255);*
  • analogWrite(LEDPin3, 255);*
  • analogWrite(LEDPin4, 255);*
    }
    else if(RunningAverageLight >125 && RunningAverageLight <=250){
  • analogWrite(LEDPin1, 0);*
  • analogWrite(LEDPin2, 255);*
  • analogWrite(LEDPin3, 255);*
  • analogWrite(LEDPin4, 255);*
    }
    else if(RunningAverageLight >250 && RunningAverageLight <=375){
  • analogWrite(LEDPin1, 0);*
  • analogWrite(LEDPin2, 0);*
  • analogWrite(LEDPin3, 255);*
  • analogWrite(LEDPin4, 255);*
    }
    else if(RunningAverageLight >375 && RunningAverageLight <=500){
  • analogWrite(LEDPin1, 0);*
  • analogWrite(LEDPin2, 0);*
  • analogWrite(LEDPin3, 0);*
  • analogWrite(LEDPin4, 255);*
    }
    else{
  • analogWrite(LEDPin1, 0);*
  • analogWrite(LEDPin2, 0);*
  • analogWrite(LEDPin3, 0);*
  • analogWrite(LEDPin4, 0);*
    }

}
[/tt]
this is my code so far. Thanks for the help in advance.

Please replace the tt tags by code tags ( [code] and [/code] ); that will prevent the lovely italics in your post.

If you need to fade, look at analogWrite examples. Note that not all pins support it (see the reference).

This should work, once you select PWM pins for all of your LEDs.

const int RunningAverageCount = 16;               // Anzahl der in den Laufenden-Mettelwert aufgenommenen Messungen
int RunningAverageBuffer[RunningAverageCount];  // Initialized to 0
int NextRunningAverage;  // Initialized to 0
const int LEDPin1 = 9;
const int LEDPin2 = 10;
const int LEDPin3 = 11;  // Not a PWM pin.  Use 3, 5, 6, or 13
const int LEDPin4 = 12;  // Not a PWM pin.  Use 3, 5, 6, or 13


void setup()
{
  Serial.begin(9600); //Kommunikationsrate
}


void loop()
{
  static unsigned long runningTotal = 0;


  // Remove the oldest sample from the total
  runningTotal -= RunningAverageBuffer[NextRunningAverage];


  // Add the new sample to the buffer and the total
  RunningAverageBuffer[NextRunningAverage] = analogRead(A0);
  runningTotal += RunningAverageBuffer[NextRunningAverage];


  // Point to the oldest sample
  NextRunningAverage = (NextRunningAverage + 1) % RunningAverageCount;


  int RunningAverageLight = runningTotal / RunningAverageCount;


  Serial.println(RunningAverageLight);
  delay(10);


  // int helligkeit;


  if (RunningAverageLight <= 125)
  {


    analogWrite(LEDPin1, 255 - map(RunningAverageLight, 0, 125, 0, 255));
    analogWrite(LEDPin2, 255);
    analogWrite(LEDPin3, 255);
    analogWrite(LEDPin4, 255);
  }
  else if (RunningAverageLight <= 250)
  {


    analogWrite(LEDPin1, 0);
    analogWrite(LEDPin2, 255 - map(RunningAverageLight, 126, 250, 0, 255));
    analogWrite(LEDPin3, 255);
    analogWrite(LEDPin4, 255);
  }
  else if (RunningAverageLight <= 375)
  {


    analogWrite(LEDPin1, 0);
    analogWrite(LEDPin2, 0);
    analogWrite(LEDPin3, 255 - map(RunningAverageLight, 251, 375, 0, 255));
    analogWrite(LEDPin4, 255);
  }
  else if (RunningAverageLight <= 500)
  {
    analogWrite(LEDPin1, 0);
    analogWrite(LEDPin2, 0);
    analogWrite(LEDPin3, 0);
    analogWrite(LEDPin4, 255 - map(RunningAverageLight, 376, 500, 0, 255));
  }
  else
  {
    analogWrite(LEDPin1, 0);
    analogWrite(LEDPin2, 0);
    analogWrite(LEDPin3, 0);
    analogWrite(LEDPin4, 0);
  }
}