Go Down

Topic: Assigning array to array. Problem (Read 1 time) previous topic - next topic

SpyChar

Okay, so here is the code. I've done it as simply as possible because I'm trying to debug my bigger project.

Code: [Select]

int button=12;

int position1=0;
int position2=0;
int array1[]={1,2,3,4,5,6,7,8,9};
int array2[]={};
int length=0;

void setup(){
  pinMode(button,INPUT);
  Serial.begin(9600);

}

void loop(){
  if(digitalRead(button)==HIGH){
    delay(100);
    position1++;
    position2++;
    length++;
    if (length>9) length=9;
    if (position1>9) position1=9;
    if (position2>9) position2=9;
    array2[position2]=array1[position1]; //   <---------------- problematic part
    for(int cnt=0;cnt<length;cnt++){
      Serial.print("position "); Serial.print(cnt); Serial.print("= "); Serial.println(array2[cnt]);
    }
    Serial.println("");   
  }
}


What I wanted to achieve is to copy values from one array to another one by one, each one after one press of the button. I expected to get something like this

Code: [Select]

position 0= 1
position 1= 2
position 2= 3
position 3= 4
position 4= 5
position 5= 6
position 6= 7
position 7= 8
position 8= 9


But instead I got this

Code: [Select]

position 0= 9
position 1= 7382
position 2= 3
position 3= 6076
position 4= 5
position 5= 2
position 6= 7
position 7= 8
position 8= 9


Do You know what's wrong?

robtillaart


the size of array 2 is unknown. Give them both fixed size that's large enough. (note C arrays are zero based)

int array1[10]={1,2,3,4,5,6,7,8,9};
int array2[10]={};

give it a try.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

johnwasser

Code: [Select]

int array1[]={1,2,3,4,5,6,7,8,9};  // Array of 9 elements
int array2[]={};  // Array of 0 elements


You can't write into element 0-8 of a zero-length array without corrupting other variables.

To fix it you need to specify how much room to set aside for the array:

Code: [Select]

int array1[]={1,2,3,4,5,6,7,8,9};  // Array of 9 elements
int array2[sizeof array1 / sizeof array1[0]];  // Array the same size as array1


Note: 'sizeof' is an operator that returns the size IN BYTES.  The division is there to convert from BYTES to the same size elements as array1.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Nick Gammon

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

Nick Gammon

This would work in Lua where arrays dynamically resize, but not in C++.
http://www.gammon.com.au/electronics

SpyChar

Darn. I thought this would solve the problem. Actually, what I was really thinking of was to have one fixed array, and then the other one, which would be empty. Then I wanted to be able to choose values from the first array and assign them to the second one in any order and in any amount.
And sorry for any possible language errors :) (and lack of commas)

PaulS

Quote
Actually, what I was really thinking of was to have one fixed array, and then the other one, which would be empty.

A warehouse can be empty. So can a closet. Only one of them is going to be large enough to park 10 trucks in. The size of the storage space and the amount being used are two different things. The size of the storage space you had initially defined was zero. You can make it bigger, and it will still be empty.

SpyChar

Yeah, thanks. Now I'm a little bit wiser. But I still don't know how to do this right. But hey, this is the purpose of forum - to ask questions. And by that I mean, to learn new things.

PaulS

Quote
But I still don't know how to do this right.

Part of asking questions is asking them in a way that includes enough detail. What is it you are trying to do?

SpyChar

Ok, I'll tell You but promise you won't laugh. I'm trying to make a "music synthesizer" as I like to call it. I have one array with notes frequencies assigned to it, and now I want to be able to scroll through them using buttons, and assign them to this second array.

PaulS

Quote
Ok, I'll tell You but promise you won't laugh.

OK, I won't.

Quote
I have one array with notes frequencies assigned to it, and now I want to be able to scroll through them using buttons, and assign them to this second array.

Switches would be more useful, but, whatever works.

The second array needs to have a size associated with it. Now, that size can be a fixed/static size, or it can be a dynamic size, growing as more notes are added to it. There is, of course, a limit as to how large the array can grow to. There are a number of issues with dynamic arrays, mostly related to the fact that the Arduino hasn't got that much memory to be used for dynamic (or static) arrays.

You can minimize the amount of SRAM used by putting the first array in PROGMEM, since that array won't change. Getting that out of SRAM leaves more room for your static (or dynamic) second array.

What do you intend to do with this second array when it is populated?

HazardsMind

#11
Jan 13, 2013, 12:33 am Last Edit: Jan 13, 2013, 12:44 am by HazardsMind Reason: 1
The sizes must match. And I guessing your doing this,
Code: [Select]
Array1[x] = Array2[y] correct?
If your getting garbage in array2 then make it array2[]={0,0,0,0,0,0,0,0,0} to match array1.
@PaulS,
Yea that was weird, [ x ] gave me that black bullet.

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

Quote
And I guessing your doing this,

I didn't see anything like that in OPs code.

SpyChar

Well, as I said that was only test code. In my main code, I'm doing it like this: array1[position1]=array2[position2] after pressing the buttons and raising position1 or position2 as needed.

edit. actually, this part of code is here too.

And what I want to do with the second array is to iterate(good word?) though it using for loop and play those values with buzzer.

PaulS

Quote
And what I want to do with the second array is to iterate(good word?) though it using for loop and play those values with buzzer.

Yes, iterate is a good word. How big are you expecting the array of notes/frequencies to be? 10? 100? 1000?

How will you transition from "record some notes" to "annoy me by endlessly repeating those notes" mode? What will happen when the Arduino is reset?

Go Up