void LineBackAndForth()
{
for (int i = 0; i <= 9; i++){
digitalWrite(ledPins*, HIGH);*
delay(60);*
}*
for (int i = 9; i >= 0; i--){*
_ digitalWrite(ledPins*, LOW);_
_ delay(60);_
_ }_
_}_ Again, this code runs correct on time but the second time it goes all cookoo on me. idk why and need an answer.
_(btw, it is a simple led setting in my led show project)*_
Just a guess but
for (int i = 0; i <= 9; i++)
means it will go from 0 to 9 incrementing by 1, ie it will increment i to 10.
for (int i = 9; i >= 0; i--)
means it will go from 9 to 0, decrementing by 1, ie it will decrement i to -1
You cannot write to pin -1 as it doesnt exist obviously, hence why the micro is most likely not doing what you expect... its just doing exactly what you told it though
I am assuming you want it to go from 0 to 9 and back down again. So if thats the case, just remove the i <= 9 and i >= 0 bits, and replace it with i<9 and i>0. That will mean it will get to 8 and increase it to 9 and then end the for loop, and then go from 9 to 1, and decrement it to 0 and then end the for loop.
May not be what you want but its how i interpreted your question
Assuming you're saying that if you run LineBackAndForth() once then it works perfectly, then Paul is right: you need to tell us exactly what happens the second time.
Also, what happens in your parent code between the first call to LineBackAndForth() and the second call?
/*This is a Led show prototype, it has many modes and they can
be changed with a simple buttonpress */
int ledPins[] = {2,3,4,5,6,7,8,9,10,11,12,};
int ButtonPin = 12; //all the different pins/variables
int ButtonState = 0;
int button_flag = 0;
void setup()
{
for (int i = 0; i < 10; i++){
pinMode(ledPins*, OUTPUT); //LEDs all Output*
}*
pinMode(ButtonPin, INPUT); //Pot and button are input*
{*
_ Serial.begin(9600); //serial for possible debugging_
}*
}* void loop() { //See if pushed and held if(digitalRead(ButtonPin)) {
delay(40);*
while(digitalRead(ButtonPin)) {*
//button is on...*
//set button flag to on*
button_flag = 1;*
while(digitalRead(ButtonPin))*
{}*
}* }
{*
switch (ButtonState)*
{*
case 0: {*
nothing();*
if(button_flag){*
ButtonState = 1;*
button_flag = 0;*
}*
break;*
}*
case 1: {*
LineBackAndForth();*
}*
if(button_flag){*
ButtonState = 2;*
button_flag = 0;*
break;*
}*
case 2: {*
oneafteranother();*
}*
if(button_flag){*
ButtonState = 3;*
button_flag = 0;*
break;*
}*
case 3: {*
strobe();*
}*
if(button_flag){*
ButtonState = 4;*
button_flag = 0;*
break;*
}*
case 4: {*
inandout();*
}*
if(button_flag){*
ButtonState = 0;*
button_flag = 0;*
}*
}*
_ Serial.println(ButtonState);_
}* }
void nothing() { for (int i = 0; i <= 9; i++) digitalWrite(ledPins*, LOW);* } void LineBackAndForth() { * for (int i = 0; i < 10; i++){* _ digitalWrite(ledPins*, HIGH); delay(60); } for (int i = 9; i > 0; i--){ digitalWrite(ledPins, LOW); delay(60); } }*_
void oneafteranother() { * for (int i = 0; i < 10; i++) {* _ digitalWrite(ledPins*, HIGH); }*_
} void strobe() { } void inandout() { *} * [/quote] btw this is my whole code. The Println says each time it finishes it starts mode 1, LineBackAndForth, over again. If it is a problem here it may cause problems all the way through.
Posting your code (incorrectly - use the # button on the editor's toolbar) doesn't define what "work" means to you.
That's the bit you have to explain to us, and how "work" differs from what you're seeing.
Well, the line should go back and forth and it does that once. Than it all lights from 0. And goes back with the normal back for loop. It then repeats this until it is brought to start in mode 1 again. :-/
THANK YOU SOOO MUCH. I've been working on this for a long time and everyone said it was good code. Lessening braces changed EVERYTHING. This already has gone on longer than it should. ;D :o
btw, how to you select multiple pins to turn on at once.
digitalWrite(ledPins[i dont know], HIGH/LOW);
Depends on what you mean by 'at once'. If you mean faster then your eyes can detect then just build a for loop that will turn on the output pins you need if they are sequential pin numbers. If not just do digitalWrite commands in line. If that is not fast enough, you can check out direct port access commands.