undesrstanding i++
in code leds trigger , dont seem go 1 @ time
go easy im new this.
all want 1 led @ time...once again head hurts
code: [select]
byte red[] = {5, 9, 13, 22};
int ledstate = high;
long previousmillis = 0; // store last time led updated
// follow variables long because time, measured in miliseconds,
// become bigger number can stored in int.
long interval = 250;
void setup() {
// set digital pin output:
for (int i=0; i<sizeof(red); i++) {
pinmode(red[i], output);
}
}
void loop()
{
// check see if it's time blink led; is, if
// difference between current time , last time blinked
// led bigger interval @ want
// blink led.
unsigned long currentmillis = millis();
if(currentmillis - previousmillis > interval) {
// save last time blinked led
previousmillis = currentmillis;
// if led off turn on , 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 this.
all want 1 led @ time...once again head hurts
i++ means take value of i, add 1 it, , save i. or, more simply, increases 1.
sets of relevant pins outputs (which good). similarly,
sets of relevant pins ledstate. they'll same value. when want each 1 change?
random comments:
you shouldn't sizeof(red), since if make red array of ints, return number twice big you're expecting. common way sizeof(red)/sizeof(red[0].
previousmillis , interval should both of type unsigned long, since that's millis() returns.
try indent better. each { should have indent on following line, , each } should have 1 less indent. makes easier see if matches code , such.
code: [select]
for (int i=0; i<sizeof(red); i++) {
pinmode(red[i], output);
}
sets of relevant pins outputs (which good). similarly,
code: [select]
for (int i=0; i<sizeof(red); i++)
digitalwrite(red[i], ledstate);
}sets of relevant pins ledstate. they'll same value. when want each 1 change?
random comments:
you shouldn't sizeof(red), since if make red array of ints, return number twice big you're expecting. common way sizeof(red)/sizeof(red[0].
previousmillis , interval should both of type unsigned long, since that's millis() returns.
try indent better. each { should have indent on following line, , each } should have 1 less indent. makes easier see if matches code , such.
Arduino Forum > Using Arduino > Programming Questions > undesrstanding i++
arduino
Comments
Post a Comment