Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: January 12, 2013, 02:46:34 pm » |
Okay, so here is the code. I've done it as simply as possible because I'm trying to debug my bigger project. 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 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 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?
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #1 on: January 12, 2013, 02:52:28 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 96
Posts: 6356
|
 |
« Reply #2 on: January 12, 2013, 02:55:25 pm » |
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: 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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #3 on: January 12, 2013, 02:55:53 pm » |
the size of array 2 is unknown.
Zero-length, I would say.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #4 on: January 12, 2013, 02:56:28 pm » |
This would work in Lua where arrays dynamically resize, but not in C++.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #5 on: January 12, 2013, 04:35:56 pm » |
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)
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #6 on: January 12, 2013, 04:51:10 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #7 on: January 12, 2013, 05:26:39 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #8 on: January 12, 2013, 05:30:47 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #9 on: January 12, 2013, 06:23:31 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: January 12, 2013, 06:31:05 pm » |
Ok, I'll tell You but promise you won't laugh. OK, I won't. 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?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 27
Posts: 1550
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #11 on: January 12, 2013, 06:33:47 pm » |
The sizes must match. And I guessing your doing this, 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.
|
|
|
|
« Last Edit: January 12, 2013, 06:44:00 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #12 on: January 12, 2013, 06:34:41 pm » |
And I guessing your doing this, I didn't see anything like that in OPs code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #13 on: January 12, 2013, 06:41:48 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: January 12, 2013, 06:47:50 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
|