Hello everyone, this is my first post so let me know if I should do things differently for next time. I have an array of 10 LEDs and what I want it to do is light up one at a time, until it gets to the last led (location 9 on my array). I then want it to go back to the first led, lighting each led as it goes backwards. This is where my problem is; i want each led to light but not the last led, i.e. go to array location 8. I want the process to continue, until no LEDs light. I will put the code on my next post because Im pretty sure there is a way to make the code scrollable. Please let me know how to do that also! Also let me know if I made my aims confusing.
Enclose your code with [ code] [ /code ] (but without the spaces)
I will put the code on my next post because Im pretty sure there is a way to make the code scrollable. Please let me know how to do that also!
Up above the window you type text in, there are 2 rows of buttons. On the second row, on the right end, there is a button with a # on it. Select that button, then paste your code.
So, you want to loop through all the LEDs, lighting each one up, then loop through each LED in reverse order, extinguishing them, with a different upper limit on each loop.
for(int ul = 9; ul>0; ul--) // Determine the upper limit - 9, 8, 7...
{
for(int i=0; i<=ul; i++)
{
// Turn on the ith LED
}
for(int i=ul; i>=0; i--)
{
// Turn off the ith LED
}
}
Thanks for the replies!! @paulS, I actually wanted each led to go on and then go off once the next led went on. Its hard to explain, easy to visualise. Wait, let me explain with a little picture:
1 means led is on and 0 means led is off. Im going to show just 4 LEDs.
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0
0 1 0
0 0 1
0 1
1
Anyways, i got it to work thanks to the code you wrote above. My only concern is that I have used a delay to get this desired effect and would mean that I would be unable to change the speed between the LEDs flicking with a potentiometer.
byte ledPin[] = {22,24,26,28,30,49,50,51,52,31};
int ledDelay(100);
unsigned long changeTime;
void setup(){
for (int x=0; x<10;x++)
{
pinMode(ledPin[x], OUTPUT);
}
changeTime = millis();
}
void loop(){
if(millis()-changeTime > ledDelay)
{
changeLED();
changeTime = millis();
}
}
void changeLED(){
for (int x=9; x>=0; x--)
{
for (int i=0; i<x; i++)
{
digitalWrite(ledPin[i],LOW);
}
for (int i=0; i<=x; i++)
{
digitalWrite(ledPin[i],HIGH);
//The delay here is a slight problem!!
delay(400);
digitalWrite(ledPin[i],LOW);
}
}
}
Why not just read the pot every time through the loop?
It will only add 100us or so for each read.
Time to have a look at the blink without delay example.
I looked at the blink without delay and tried mixed it around with what i previously had.
byte ledPin[] = {22,24,26,28,30,49,50,51,52,31};
int potPin = 2;
long interval; // interval at which to blink (milliseconds)
long previousMillis = 0; // will store last time LED was updated
void setup() {
for (int x=0; x<10;x++)
{
pinMode(ledPin[x], OUTPUT);
}
}
void loop()
{
unsigned long currentMillis = millis();
interval = analogRead(potPin);
if((currentMillis - previousMillis) > interval) {
changeLED();
previousMillis = currentMillis;
}
}
void changeLED(){
for (int x=9; x>=0; x--)
{
for (int i=0; i<x; i++)
{
digitalWrite(ledPin[i],LOW);
}
for (int i=0; i<=x; i++)
{
digitalWrite(ledPin[i],HIGH);
interval;
digitalWrite(ledPin[i],LOW);
}
}
}
The problem is that when the potentiometer is at its maximum (10K ohm) all the lights are on and when its at a lower resistance I get extremely rapid lights blinking.
interval;
Why?
I want to delay the speed in which the led turns on and off. If I have nothing there then I think it would execute the code way too fast. I want the delay to come from the potentiometer which was why I put interval there..I know it dont work but that was my thinking behind it