Help with for statements and tone functions


hi
i trying make project when type number 1 in serial monitor plays tune, , different tune number 2 , on. used tutorial basics (https://diyhacking.com/arduino-speaker/), i'm trying make play multiple melodies. reason i'm using lcd screen can see number serial.read(). can me make change song when type respective number?
code: [select]
#define note_b0  31
#define note_c1  33
#define note_cs1 35
#define note_d1  37
#define note_ds1 39
#define note_e1  41
#define note_f1  44
#define note_fs1 46
#define note_g1  49
#define note_gs1 52
#define note_a1  55
#define note_as1 58
#define note_b1  62
#define note_c2  65
#define note_cs2 69
#define note_d2  73
#define note_ds2 78
#define note_e2  82
#define note_f2  87
#define note_fs2 93
#define note_g2  98
#define note_gs2 104
#define note_a2  110
#define note_as2 117
#define note_b2  123
#define note_c3  131
#define note_cs3 139
#define note_d3  147
#define note_ds3 156
#define note_e3  165
#define note_f3  175
#define note_fs3 185
#define note_g3  196
#define note_gs3 208
#define note_a3  220
#define note_as3 233
#define note_b3  247
#define note_c4  262
#define note_cs4 277
#define note_d4  294
#define note_ds4 311
#define note_e4  330
#define note_f4  349
#define note_fs4 370
#define note_g4  392
#define note_gs4 415
#define note_a4  440
#define note_as4 466
#define note_b4  494
#define note_c5  523
#define note_cs5 554
#define note_d5  587
#define note_ds5 622
#define note_e5  659
#define note_f5  698
#define note_fs5 740
#define note_g5  784
#define note_gs5 831
#define note_a5  880
#define note_as5 932
#define note_b5  988
#define note_c6  1047
#define note_cs6 1109
#define note_d6  1175
#define note_ds6 1245
#define note_e6  1319
#define note_f6  1397
#define note_fs6 1480
#define note_g6  1568
#define note_gs6 1661
#define note_a6  1760
#define note_as6 1865
#define note_b6  1976
#define note_c7  2093
#define note_cs7 2217
#define note_d7  2349
#define note_ds7 2489
#define note_e7  2637
#define note_f7  2794
#define note_fs7 2960
#define note_g7  3136
#define note_gs7 3322
#define note_a7  3520
#define note_as7 3729
#define note_b7  3951
#define note_c8  4186
#define note_cs8 4435
#define note_d8  4699
#define note_ds8 4978
#define end -1
}
char song1[] = "ode joy";
char song2[] = "honey bun";
char song3[] = "hedwig's theme";
char song4[] = "we number one";
int song = 1;
int songchar = 1;
int speed1 = 50;
int speed2 = 45;
int speed3 = 70;
int speed4 = 50;
int thisnote = 0;
int noteduration = 0;
#include <liquidcrystal.h>
liquidcrystal lcd(12, 11, 5, 4, 3, 2);
int melody1[] = {
  note_e4, note_e4, note_f4, note_g4,//1
  note_g4, note_f4, note_e4, note_d4,//2
  note_c4, note_c4, note_d4, note_e4,//3
  note_e4, note_d4, note_d4,//4
  note_e4, note_e4, note_f4, note_g4,//1
  note_g4, note_f4, note_e4, note_d4,//2
  note_c4, note_c4, note_d4, note_e4,//3
  note_d4, note_c4, note_c4,//5
  note_d4, note_d4, note_e4, note_c4,//6
  note_d4, note_e4, note_f4, note_e4, note_c4,//7
  note_d4, note_e4, note_f4, note_e4, note_d4,//8
  note_c4, note_d4, note_g3,//9
  note_e4, note_e4, note_f4, note_g4,//1
  note_g4, note_f4, note_e4, note_d4,//2
  note_c4, note_c4, note_d4, note_e4,//3
  note_d4, note_c4, note_c4,//5
  end

};
int melody2[] = {
  note_b4, note_a4, note_a4, note_b4, note_g4, note_as4, note_b4, note_d4,
  note_as4, note_b4, note_as4, note_b4, note_as4, note_b4, note_d5,
  0, note_b4, note_g4, note_b4, note_g4, note_b4, note_a4, note_g4, note_a4,
  note_g4, 0,
  end
};
int melody3[] = {
  note_b3, note_e4, note_g4, note_fs4, note_e4, note_b4, note_a4, note_fs4,
  note_e4, note_g4, note_fs4, note_ds4, note_f4, note_b3, note_b3,
  note_e4, note_g4, note_fs4, note_e4, note_b4, note_d5, note_cs5, note_c5, note_gs4,
  note_c5, note_b4, note_as4, note_as3, note_g4, note_e4,
  end
};
int melody4[] = {
  note_f4, 0, note_c5, note_b4, note_c5, note_b4, note_c5, note_b4, note_c5, note_gs4, note_f4,
  0, note_f4, 0, note_gs4, 0, note_c5, 0, note_cs5, note_gs4, note_cs5, note_ds5,
  note_c5, note_cs5, note_c5, note_cs5, note_c5, 0, note_f5,
  end
};
int rhythm1[] = {
  8, 8, 8, 8, //1
  8, 8, 8, 8, //2
  8, 8, 8, 8, //3
  12, 4, 16,//4
  8, 8, 8, 8, //1
  8, 8, 8, 8, //2
  8, 8, 8, 8, //3
  12, 4, 16,//5
  8, 8, 8, 8,//6
  8, 4, 4, 8, 8,//7
  8, 4, 4, 8, 8,//8
  8, 8, 16,//9
  8, 8, 8, 8, //1
  8, 8, 8, 8, //2
  8, 8, 8, 8, //3
  12, 4, 16,//5
};
int rhythm2[] = {
  4, 4, 4, 8, 16, 4, 8, 20,
  8, 8, 8, 8, 4, 8, 20,
  8, 4, 4, 8, 8, 4, 8, 12, 8,
  56, 4,
};
int rhythm3[] = {
  4, 6, 2, 4, 8, 4, 12, 12,
  6,  2, 4, 8, 4, 20,
  4, 6, 2, 4, 8, 4, 8, 4, 8,
  4, 6, 2, 4, 8, 4, 20,
};
int rhythm4[] = {
  8, 4, 4, 2, 2, 2, 2, 4, 4, 8, 8, 4, 2, 2, 2, 2, 2, 2, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 8
};
void setup() {
  serial.begin(9600);
  pinmode(button, input);
  lcd.begin(16, 2);
}

void loop() {
  song = serial.read();
  if (song != -1) {
    songchar = song;
  }
  lcd.print(songchar);
  lcd.setcursor(0, 0);
  (thisnote = 0; songchar == 49; thisnote++) {
    noteduration = speed1 * rhythm1[thisnote];
    tone(8, melody1[thisnote], noteduration * .95);
    delay(noteduration);
    notone(8);
    if (song != -1) {
      songchar = song;
    }
    lcd.print(songchar);
    lcd.setcursor(0, 0);
  }
  (thisnote = 0; songchar == 50; thisnote++) {
    noteduration = speed2 * rhythm2[thisnote];
    tone(8, melody2[thisnote], noteduration * .95);
    delay(noteduration);
    notone(8);
    if (song != -1) {
      songchar = song;
    }
    lcd.print(songchar);
    lcd.setcursor(0, 0);
  }
  (thisnote = 0; songchar == 51; thisnote++) {
    noteduration = speed3 * rhythm3[thisnote];
    tone(3, melody3[thisnote], noteduration * .95);
    delay(noteduration);
    notone(8);
    if (song != -1) {
      songchar = song;
    }
    lcd.print(songchar);
    lcd.setcursor(0, 0);
  }
  (thisnote = 0; songchar == 52; thisnote++) {
    noteduration = speed4 * rhythm4[thisnote];
    tone(8, melody4[thisnote], noteduration * .95);
    delay(noteduration);
    notone(8);
    if (song != -1) {
      songchar = song;
    }
    lcd.print(songchar);
    lcd.setcursor(0, 0);
  };
};

code: [select]
for (thisnote = 0; songchar == 49; thisnote++) {

the middle condition continue condition, not stop condition.  reads, "starting songchar @ 0 long songchar equals 49 , add 1 songchar.  how many times think happen?

and remember, if play song loop , use delay time notes whole song have finish before program read next input.  may want if want react , switch in middle of song you've got rethink this. 


Arduino Forum > Using Arduino > Project Guidance > Help with for statements and tone functions


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor