Go Down

### Topic: Assigning array to array. Problem (Read 2284 times)previous topic - next topic

#### SpyChar

#15
##### Jan 13, 2013, 01:02 am
I guess that 100 should be absolutely maximum. Actually you know what? I'll post the code here.
Code: [Select]
`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.

#### PaulS

#16
##### Jan 13, 2013, 01:13 am
Code: [Select]
`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.

Code: [Select]
`  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.

#### SpyChar

#17
##### Jan 13, 2013, 01:32 am
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.

#### PaulS

#18
##### Jan 13, 2013, 01:41 am
Quote
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.

Quote
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.

#### SpyChar

#19
##### Jan 13, 2013, 01:57 am
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.

#### PaulS

#20
##### Jan 13, 2013, 02:01 am
Quote
You probably know what's the critical bug, can you share that knowledge with me?

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

Quote
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.

#### SpyChar

#21
##### Jan 13, 2013, 02:18 am
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.

#### PaulS

#22
##### Jan 13, 2013, 02:35 am
Quote
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.