Pages: [1]   Go Down
Author Topic: Array Quastion  (Read 600 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: 98
Posts: 4813
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

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19026
Lua rocks!
View Profile
WWW
 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: 98
Posts: 4813
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

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
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

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19026
Lua rocks!
View Profile
WWW
 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: