Go Down

Topic: Array Quastion (Read 628 times) previous topic - next topic

Fragggy

Hi all,
I have a short question about passing arrays to functions.

Code: [Select]

...
int green[3]={255,0,0};
int blue[3]={0,255,0};
int red[3]={0,0,255};

void loop()  {
setColor(red);
delay(100);
setColor(blue);
}

void setColor(int color[3]) {
  analogWrite(greenPin,color[0]);
  analogWrite(bluePin,color[1]);
  analogWrite(redPin,color[2]);
}
...


This went wrong for the second setColor Call.
I googeld and found that ist had somthing to do with array treated as pointers.
Is there a easy way to pass the Data to the function?

Thanks alot.

Dennis

GoForSmoke

Code: [Select]

void setColor(int *color) {


Try changing just that.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Nick Gammon



This went wrong for the second setColor Call.
I googeld and found that ist had somthing to do with array treated as pointers.
Is there a easy way to pass the Data to the function?



It compiled OK for me. What was the error message?
http://www.gammon.com.au/electronics

Fragggy

GoForSmoke this does not change anything :-(

The Code compiles fine, but the LED only switches to red and than stais red.

I think the array is created in the first call and in the second call the data in color[] is still the same.


GoForSmoke

LOL, sorry, I missed step! Your led is turning blue but instantly turns red.

Code: [Select]

void loop()  {
setColor(red);
delay(100);
setColor(blue);
delay(100);
}


I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

GoForSmoke


LOL, sorry, I missed step! Your led is turning blue but instantly turns red.

tiny fix here
Code: [Select]

void loop()  {
setColor(red);
delay(100);
setColor(blue);
delay(100);
}


BTW Nick, do you happen to know anyone at Dontronics?


I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Nick Gammon


BTW Nick, do you happen to know anyone at Dontronics?


No. I've bought stuff from them, but don't know them.
http://www.gammon.com.au/electronics

Go Up