# help?

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)*_

but the second time it goes all cookoo on me.

Let me check my book of technical jargon. Hmm, can't find an entry there that gives me a clue.

Can you please calm down, and explain what it does on the first call? What does it do on the second call? What do you expect it to do on each call?

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

Cheers

@WanaGo:
Do you want to review the structure of "for" loops?

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.

Yes, when the "for" loop terminates, "i" will have the value 10, but that value will not have been used in the body of the loop.

Loops look fine to me.

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?

I think i got my answer. I am going to test it to make sure.

the line would go
0-on, 1on, 2-on, 3-on... 10-off, 9-off,...ALL ON, back off normaly.

I just tried
{
for (int i = 0; i < 10; i++){
digitalWrite(ledPins*, HIGH);*

• delay(60);*
• }*
• for (int i = 9; i > 0; i--){*
_ digitalWrite(ledPins*, LOW);_
_
delay(60);_
_
}_
_
}*_
and it does not work, does the same thing.

Again, this code runs correct on time but the second time it goes all cookoo on me. idk why and need an answer

it does not work

Did this thread start with some posts missing?
Like an explanation of what you expect to happen, or have some posts been deleted?

Define "work"

Another cross-posting time-waster. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1279644550/15#15[/edit]

/*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. :-/

Maybe try tidying up your braces {} ?

``````        case 1: {
LineBackAndForth();
}
if(button_flag){
ButtonState = 2;
button_flag = 0;
break;
}
``````

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

That's why when we say "post your code", we mean it, not just the bit you've just changed, or the bit you think may be wrong, but the whole lot.

Glad you got it working.

btw, how to you select multiple pins to turn on at once.
digitalWrite(ledPins[i dont know], HIGH/LOW);
:-/

how to you select multiple pins to turn on at once.

You can't with digitalWrite - look at direct port manipulation.
Plenty here on the forum

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.

Lefty

thanks, i need to turn all on, delay, than all off. Will for loops work or not?

Yes, loops will work.