Pages: [1]   Go Down
Author Topic: Array Quastion  (Read 495 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
...
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
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4002
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setColor(int *color) {

Try changing just that.
Logged

Examples can be found in your IDE.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4002
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Logged

Examples can be found in your IDE.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4002
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

tiny fix here
Code:
void loop()  {
 setColor(red);
 delay(100);
 setColor(blue);
 delay(100);
}

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


Logged

Examples can be found in your IDE.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

No. I've bought stuff from them, but don't know them.
Logged

Pages: [1]   Go Up
Jump to: