As You can see , My program is way too long . Can't even post it here .I'll put the .ino in here. Is there a way to shrink it ? Hope you can help me out .
LED_END.ino (11.5 KB)
As You can see , My program is way too long . Can't even post it here .I'll put the .ino in here. Is there a way to shrink it ? Hope you can help me out .
LED_END.ino (11.5 KB)
Yes, use arrays, loops...
MarkT:
Yes, use arrays, loops...
It only has to run once.
yes you can and its realy simple.
as you can see your doing the same thing everytime. only other variable.
look at this example
byte ledPins[] = {2,3,4,5,6,7,8,9};
void setup() {
for(byte pinNum = 0; pinNum < 8; pinNum++){
pinMode(ledPins[pinNum], OUTPUT);
}
}
void loop() {
for(byte pinNum = 0; pinNum < 8; pinNum++){
digitalWrite(ledPins[pinNum], HIGH);
delay(300);
digitalWrite(ledPins[pinNum], LOW);
}
}
the for loop now just turns led 0 to led 8 on and off.
my example will only count up.
now its up to you to also count down.
PROTRADER500:
It only has to run once.
Irrelevant. Using arrays and loops means fewer chances for bugs / typos to creep in,
means the code is readable and easily extensible.
Disclamer 1 : I am new to arduino, this may not be correct, but I think it is
Disclamer 2 : non english native
I think there are 2 issues with your code.
To solve issue 1, you need - as some people said you - use arrays, for() loops, ...
To solve issue 2, you can use an if condition at the begining of loop()
This is what you could try, but it haven't be tested.
It can not work or not work as intended.
Take few minutes to read it.
#define LED_NUMBER 8
int ledPins[LED_NUMBER] = {2, 3, 4, 5, 6, 7, 8, 9};
boolean hadRun = false;
// Initialisation
void setup () {
for ( int iLed = 0 ; iLed < LED_NUMBER ; ++iLed ) {
pinMode(ledPins[iLed] , OUTPUT);
}
}
void blink(int pin, int time) {
digitalWrite(pin, HIGH);
delay(time);
digitalWrite(pin, LOW);
}
// Main program
void loop () {
int delayTime = 300; // 300 ms
// to be run only once we check this boolean
if ( !hadRun ) {
// for each led, we turn on the first then blink the next ones
for ( int i = 0 ; i < LED_NUMBER ; ++i ) {
// if it is not the first time then light the current led
if (i != 0) {
digitalWrite(ledPins[i-1], HIGH);
}
// blink led from 1 to 8
for (int iLed = i ; iLed < LED_NUMBER ; ++iLed ) {
blink(ledPins[iLed], delayTime);
}
// blink led from 8 to 1
for (int iLed = LED_NUMBER-1 ; iLed >= i ; --iLed ) {
blink(ledPins[iLed], delayTime);
}
}
// set this bolean to true, to not execute this code again
hadRun = true;
}
delay(5000); // to not overheat the hardware (??)
}