Pages: [1]   Go Down
Author Topic: How to use arrays with Arduino?  (Read 195 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello forum.

Before I posted this question (a really basic one, I am absolutely sure), I read this:
http://arduino.cc/de/Reference/Array and http://www.arduino.cc/en/Tutorial/KnightRider

The difference from what I want to do is the initialization of the array. I want to give values to the array inside a cycle, and the example shows an array being read in a cycle. What I tried to do, just to try and put the concept to work in the simplest way possible (and I can't understand why it doesn't work), was this:

Code:
int val[1000];

void setup(){
  Serial.begin(9600);
}

void loop(){
  val[0] = 1;
  val[1] = 5;
  val[2] = 2;
  val[5] = 10;
  Serial.print("val0 = ");
  Serial.println(val[0]);
  Serial.print("val1 = ");
  Serial.println(val[1]);
  Serial.print("val2 = ");
  Serial.println(val[2]);
  Serial.print("val5 = ");
  Serial.println(val[5]);
}

The code compiles but nothing is printed. If I comment all the array related lines then I can see val0 = val1 = val2 = val5 = val0... as expected.
Is there limitation to filling arrays at different zones of the arduino code?

Thanks in advance, I hope the details are enough.
« Last Edit: May 04, 2013, 02:07:10 am by Wonders » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using 2000 of your 2048 bytes of RAM (on a Uno) may not end well for you.
Suggest you trim it down.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Trimmed it down to 500 and all went well. Thanks for the fast answer!
Logged

Pages: [1]   Go Up
Jump to: