Go Down

Topic: Array Quastion (Read 644 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.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

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?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
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);
}


Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

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?


Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Nick Gammon


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


No. I've bought stuff from them, but don't know them.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up