Pages: 1 [2]   Go Down
Author Topic: Assigning array to array. Problem  (Read 821 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess that 100 should be absolutely maximum. Actually you know what? I'll post the code here.
Code:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
  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.
Logged

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

smiley-grin 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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2]   Go Up
Jump to: