in my code all LEDs trigger and dont seem to go one at a time
byte Red[] = {5, 9, 13, 22};
int ledState = HIGH;
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 250;
void setup() {
// set the digital pin as output:
for (int i=0; i<sizeof(Red); i++) {
pinMode(Red[i], OUTPUT);
}
}
void loop()
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) (ledState = HIGH);
else (ledState = LOW);
for (int i=0; i<sizeof(Red); i++)
digitalWrite(Red[i], ledState);
}
}
go easy im new to this.
all i want is one LED at a time...once again my head hurts
i++ means take the value of i, add one to it, and save it back to i. Or, more simply, it increases i by 1.
for (int i=0; i<sizeof(Red); i++) {
pinMode(Red[i], OUTPUT);
}
sets all of the relevant pins to outputs (which is good). Similarly,
for (int i=0; i<sizeof(Red); i++)
digitalWrite(Red[i], ledState);
}
Sets all of the relevant pins to ledState. So they'll always be the same value. When do you want each one to change?
Random comments:
You shouldn't say sizeof(Red), since if you make Red an array of ints, it will return a number twice as big as you're expecting. The common way to do it is to say sizeof(Red)/sizeof(Red[0].
previousMillis and interval should both be of type unsigned long, since that's what millis() returns.
Try to indent better. Each { should have an extra indent on the following line, and each } should have one less indent. This makes it easier to see which if matches with what code and such.
x++ has nothing to do with a delay, it just increments a variable by one
so if you have a for loop
for (int i=0; i<sizeof(Red); i++) {
pinMode(Red[i], OUTPUT);
}
its going to loop its contents from 0 to (sizeof(red) - 1) as fast as it can (and who wrote that geez)
if you want a delay between changes you will have to add one after
pinMode(blablahblah);
either by using the delay function (which stops everything and will bite you in the ass if you have some form of input) or other timing methods (blink without delay which is already implemented in your original code)
Osgeld:
x++ has nothing to do with a delay, it just increments a variable by one
so if you have a for loop
for (int i=0; i<sizeof(Red); i++) {
pinMode(Red[i], OUTPUT);
}
its going to loop its contents from 0 to (sizeof(red) - 1) as fast as it can (and who wrote that geez)
if you want a delay between changes you will have to add one after
pinMode(blablahblah);
either by using the delay function (which stops everything and will bite you in the ass if you have some form of input) or other timing methods (blink without delay which is already implemented in your original code)
I think he'd be looking at modifying the loop containing digitalWrite rather than pinMode.
i thought i++ the array moved the array forward one output.
It does, on each iteration of the for loop. Since you have no delays or conditional execution in the for loop, it will race through turning all the pins in the array on.