Assigning array to array. Problem

This would work in Lua where arrays dynamically resize, but not in C++.

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 :slight_smile: (and lack of commas)

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.

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.

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?

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.

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?

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.

And I guessing your doing this,

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

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.

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?

I guess that 100 should be absolutely maximum. Actually you know what? I'll post the code here.

int octave3[]={261,293,329,349,391,440,493};

int melody[]={};
int _position=0;
int note=0;
int length=0;

int button1=11;
int button2=12;
int button3=4;
int button4=3;
int button5=2;
int buzzer=6;

void setup(){
  
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button3,INPUT);
  pinMode(button4,INPUT);
  pinMode(button5,INPUT);
  pinMode(buzzer,OUTPUT);
  Serial.begin(9600);
}

void loop(){ 

  if(digitalRead(button2)==HIGH){
    delay(200);
    _position--;
      if(_position<0) _position=0;
    note=melody[_position];
    tone(buzzer,melody[_position],180);
    Serial.print("length ");
    Serial.println(length);
    Serial.print(" ");
    Serial.print("_position ");
    Serial.println(_position);
    Serial.print(" ");
  }

  if(digitalRead(button3)==HIGH){
    delay(200);
    _position++;     
      if(_position==length) length++;
    melody[length];
      if(_position>=length) melody[_position]=octave3[note];    
    tone(buzzer,melody[_position],180);
    Serial.print("length ");
    Serial.println(length);
    Serial.print(" ");
    Serial.print("_position ");
    Serial.println(_position);
    Serial.print(" ");
  }

  if(digitalRead(button4)==HIGH){
    delay(200);
    note--;
      if(note<0) note=0;  
    melody[_position]=octave3[note];           
    tone(buzzer,melody[_position],180);
  }

  if(digitalRead(button5)==HIGH){
    delay(200);   
    note++;
      if(note>6) note=6; 
    melody[_position]=octave3[note];              
    tone(buzzer,melody[_position],180);
  }

  if(digitalRead(button1)==HIGH){
    delay(200);
    //melody[_position]=octave3[note]; 
    playMelody();
  }   
}

void playMelody(){
  for(int cnt=0;cnt<length;cnt++){  
    tone(buzzer,melody[cnt],180);
    delay(200);
    noTone(buzzer);
    if(digitalRead(button1)==HIGH) break;
  }
}

Keep in mind that i'm a beginner, and I know that this is not a masterpiece.

int melody[]={};

When you don't specify a value in the [], the compiler counts the number of initializers provided to determine how big the array needs to be. If you want the array to be non-zero, you must either provide a size or more than 0 initializers.

  if(digitalRead(button2)==HIGH){
    delay(200);
    _position--;
      if(_position<0) _position=0;

position starts at 0. Decrementing it, and constraining the lower limit to be 0 don't seem like useful things to do.

Names like button2 don't convey any information. You don't really have a button connected to the Arduino, do you? If so, you really might get better results with a switch.

If you do have switches, and you are going to build an enclosure for the device, and want to sell millions of them, labeling the switches 1, 2, and 3 isn't really going to cut it.

Anyway, the biggest problem with that code is that you need to tell the compiler how big melody is to be, rather than expecting it to guess.

:smiley: Actually I do have buttons. I named them liek that becaise this is expanded version of my first project. I had one button named button, and now I connected another four buttons, so I thought it would be right to name them that way.

And with this decrementing thing, I want to be able to go back and maby fix notes which I didn't like.

I named them liek that becaise this is expanded version of my first project. I had one button named button, and now I connected another four buttons, so I thought it would be right to name them that way.

"I gave the variable a crappy name to start with, and I want to keep using crappy names." Well, OK. I guess.

Meaningful names are going to help you. You might as well start using them, now.

And with this decrementing thing, I want to be able to go back and maby fix notes which I didn't like.

By working backwards through the array? I can't imagine how that will really work, but, I guess time will tell. Once you fix the critical bug.

You probably know what's the critical bug, can you share that knowledge with me?
So you tell me that I can't change any value I choose in the array? Is there any other way to achieve it.

You probably know what's the critical bug, can you share that knowledge with me?

We already have. It's that zero sized array.

So you tell me that I can't change any value I choose in the array?

I didn't say that. I said that starting from the end, and working back towards the beginning was a strange way to do it.

Hah, it works now (working backwards too). Turns out, that it was all about that array size. It didn't seem so critical. Anyway, thanks man.

Hah, it works now (working backwards too). Turns out, that it was all about that array size. It didn't seem so critical.

Cool. Glad you got it working.