Hi everyone,
I am using the bargraph code from arduino.cc but in response to values from an LDR, not a potentiometer.
I would like to add to the code so that the LEDs turn on and off more slowly when the light levels change dramatically.
For instance, with my current code, the LEDs are off in daylight, but if i cover the LDR so that the values show it is suddenly dark, each of the 12 LEDs plugged in comes on immediately. I would like it so that number one turns on, pause, number two turns on, pause etc. so that it is more of a wave effect down the line of LEDs.
I have tried using the delay function for each instance of the array, with the intention of the code being read as "when the light falls to this level number one turn on. delay. number two turn on. delay. number three turn on." but this has simply made LEDs 1-4 turn off totally. I have read that delay isn't the best function to use, but I have never used millis() or any other time function so I would be very grateful of some help.
Here is the basic unedited code:
//ROTATING THROUGH LEDS
//VARIABLES
#define sensor 1
const int nLEDs=24;
int ledPins[nLEDs]={
2,3,4,5,6,7,8,9,10,11,12,13};
int val=0;
int level=0;
//SETUP
void setup()
{
for(int i=0; i<nLEDs;i++)
{
pinMode(ledPins[i],OUTPUT);
}
Serial.begin(9600);
}
//LOOP
void loop()
{
val=analogRead(sensor);
Serial.println(val);
//Value, fromLow, fromHigh, toLow, toHigh
level=map(val,800,10,0,(nLEDs-1));
for (int i=0; i<level; i++)
{
digitalWrite(ledPins[i],HIGH);
}
delay(10);
for (int i=0;i<nLEDs; i++)
{
digitalWrite(ledPins[i],LOW);
}
}
And here the same code but showing where I have tried to implement a delay between each LED which is not working:
//ROTATING THROUGH LEDS
//VARIABLES
#define sensor 1
const int nLEDs=24;
int ledPins[nLEDs]={
2,3,4,5,6,7,8,9,10,11,12,13};
int val=0;
int level=0;
unsigned long timerA
//SETUP
void setup()
{
for(int i=0; i<nLEDs;i++)
{
pinMode(ledPins[i],OUTPUT);
}
Serial.begin(9600);
}
//LOOP
void loop()
{
val=analogRead(sensor);
Serial.println(val);
//Value, fromLow, fromHigh, toLow, toHigh
level=map(val,800,10,0,(nLEDs-1));
for (int i=0; i<level; i++)
{
digitalWrite(ledPins[i],HIGH);
}
delay(10);
for (int i=0;i<nLEDs; i++)
{
digitalWrite(ledPins[1],LOW);
delay(300);
digitalWrite(ledPins[2],LOW);
delay(300);
digitalWrite(ledPins[3],LOW);
delay(300);
digitalWrite(ledPins[4],LOW);
delay(300);
}
}
/*digitalWrite(ledPins[1],HIGH);
{delay(1000);
}digitalWrite(ledPins[2],HIGH);
{delay(1000);
}digitalWrite(ledPins[3],HIGH);
{delay(1000);}
digitalWrite(ledPins[4],HIGH);
{delay(1000);}
digitalWrite(ledPins[5],HIGH);
*/
/*digitalWrite(ledPins[1],LOW);
{delay(1000);
}digitalWrite(ledPins[2],LOW);
{delay(1000);
}digitalWrite(ledPins[3],LOW);
{delay(1000);}
digitalWrite(ledPins[4],LOW);
{delay(1000);}
digitalWrite(ledPins[5],LOW);
{delay(1000);}*/