1

I wrote this code to control my Christmas tree. It plays the Holy Night Code perfectly but never transitions to the next song. In addition, it plays random notes after the song is finished. Does anyone know what the error is?

#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

#define TREEONE 3
#define TREETWO 4
#define TREETHREE 5
#define TREEFOUR 10

SdFat sd;
SFEMP3Shield MP3player;

const uint8_t volume = 10; // MP3 Player volume 0=max, 255=lowest (off)
//10 for basement 50 for testing

const uint16_t monoMode = 1;

byte song = 1;
byte totalsongs = 10;
int num = 0;
int i;

//put arrays here
const PROGMEM uint16_t arrayTimesMC[] = {670, 249, 116, 249, 100, 249, 132, 249, 125, 249, 102, 249, 116, 249, 82, 249, 130, 249, 118, 249, 94, 249, 125, 249, 100, 249, 110, 249, 95, 249, 134, 249, 156, 249, 125, 249, 107, 249, 142, 249, 110, 249, 73, 249, 96, 249, 119, 249, 115, 249, 83, 249, 101, 249, 168, 249, 120, 249, 111, 249, 108, 249, 102, 249, 104, 249, 106, 249, 115, 249, 122, 249, 136, 249, 100, 249, 99, 249, 136, 249, 138, 249, 113, 249, 111, 249, 117, 249, 148, 249, 115, 249, 102, 249, 100, 249, 108, 249, 140, 249, 115, 249, 93, 249, 148, 249, 119, 249, 91, 249, 136, 249, 84, 249, 185, 249, 104, 249, 126, 249, 148, 249, 83, 249, 74, 249, 97, 249, 105, 249, 166, 249, 128, 249, 95, 249, 158, 249, 122, 249, 220, 249, 42, 249, 66, 249, 180, 249, 387, 249, 236, 249, 350, 249, 3571, 249, 622, 249, 744, 249, 711, 249, 707, 249, 727, 249, 691, 249, 682, 249, 570, 198, 0, 249, 333, 249, 67, 249, 401, 249, 103, 249, 355, 249, 107, 249, 712, 249, 687, 249, 749, 249, 688, 249, 424, 249, 53, 249, 371, 249, 92, 249, 330, 249, 112, 249, 387, 249, 98, 249, 709, 249, 704, 249, 702, 249, 693, 249, 785, 249, 33, 147, 0, 171, 0, 249, 93, 249, 64, 124, 0, 165, 0, 249, 100, 249, 82, 149, 0, 137, 0, 249, 56, 249, 66, 148, 0, 142, 0, 249, 124, 249, 59, 216, 0, 190, 0, 249, 87, 249, 14, 171, 0, 105, 0, 74, 0, 249, 46, 249, 38, 150, 0, 183, 0, 249, 67, 249, 46, 152, 0, 192, 0, 249, 55, 249, 30, 182, 0, 136, 0, 172, 0, 166, 0, 249, 73, 162, 0, 160, 0, 175, 0, 141, 0, 249, 56, 179, 0, 158, 0, 155, 0, 160, 0, 249, 54, 249, 58, 249, 91, 119, 0, 112, 0, 162, 0, 200, 0, 136, 0, 152, 0, 181, 0, 199, 0, 249, 59, 249, 64, 122, 0, 142, 0, 156, 0, 216, 0, 154, 0, 130, 0, 160, 0, 209, 0, 249, 49, 249, 102, 125, 0, 132, 0, 205, 0, 224, 0, 158, 0, 164, 0, 162, 0, 172, 0, 174, 0, 139, 0, 173, 0, 148, 0, 169, 0, 170, 0, 113, 0, 178, 0, 170, 0, 108, 0, 124, 0, 249, 10, 144, 0, 100, 0, 208, 0, 138, 0, 249, 225, 215, 0, 249, 59, 249, 4, 249, 77, 249, 84, 249, 32, 249, 90, 249, 54, 249, 89, 249, 20, 89, 0, 249, 43, 249, 50, 249, 69, 249, 58, 249, 89, 249, 67, 249, 48, 249, 50, 249, 88, 249, 58, 249, 59, 249, 108, 249, 157, 239, 0, 249, 78, 249, 74, 249, 25, 249, 104, 249, 111, 249, 32, 249, 56, 249, 76, 249, 42, 249, 104, 249, 36, 249, 58, 249, 88, 249, 48, 249, 73, 249, 73, 249, 78, 249, 39, 249, 94, 249, 124, 249, 67, 249, 92, 249, 60, 249, 47, 150, 0, 169, 0, 249, 101, 249, 5, 166, 0, 144, 0, 249, 102, 249, 22, 196, 0, 173, 0, 249, 81, 249, 12, 136, 0, 209, 0, 249, 69, 249, 325, 249, 77, 249, 303, 249, 142, 249, 373, 249, 124, 249, 365, 249, 122, 249, 354, 249, 34, 249, 367, 249, 104, 249, 402, 249, 86, 249, 404, 249, 110, 249, 332, 249, 51, 249, 381, 249, 92, 249, 402, 249, 76, 249, 406, 249, 38, 249, 404, 249, 48, 249, 420, 249, 64, 249, 418, 249, 82, 249, 369, 249, 63, 249, 442, 249, 11, 249, 362, 249, 92, 249, 423, 249, 70, 249, 380, 249, 57, 249, 369, 249, 104, 249, 354, 249, 98, 249, 410, 249, 72, 249, 424, 249, 50, 249, 768, 249, 61, 170, 0, 131, 0, 153, 0, 206, 0, 249, 40, 129, 0, 162, 0, 146, 0, 184, 0, 249, 103, 129, 0, 156, 0, 131, 0, 186, 0, 249, 79, 176, 0, 131, 0, 141, 0, 194, 0, 249, 60, 146, 0, 154, 0, 135, 0, 201, 0, 249, 67, 147, 0, 155, 0, 143, 0, 194, 0, 249, 220, 102, 0, 104, 0, 249, 55, 249, 56, 249, 374, 249, 725, 249, 723, 249, 673, 249, 689, 249, 711, 249, 744, 249, 751, 249, 617, 249, 753, 249, 698, 249, 760, 249, 74, 161, 0, 212, 0, 249, 17, 249, 50, 132, 0, 172, 0, 249, 64, 249, 34, 149, 0, 191, 0, 249, 94, 249, 96, 124, 0, 151, 0, 249, 66, 249, 18, 203, 0, 146, 0, 228, 0, 132, 0, 249, 62, 162, 0, 170, 0, 175, 0, 162, 0, 249, 79, 164, 0, 218, 0, 166, 0, 148, 0, 249, 38, 249, 37, 249, 66, 123, 0, 138, 0, 166, 0, 194, 0, 166, 0, 155, 0, 165, 0, 165, 0, 249, 23, 249, 100, 142, 0, 162, 0, 150, 0, 172, 0, 142, 0, 136, 0, 168, 0, 249, 10, 249, 14, 249, 114, 151, 0, 249, 41, 162, 0, 249, 118, 249, 16, 163, 0, 152, 0, 249, 90, 249, 30, 167, 0, 167, 0, 249, 121, 249, 16, 192, 0, 168, 0, 249, 71, 249, 74, 123, 0, 163, 0, 249, 115, 249, 32, 155, 0, 166, 0, 249, 90, 249, 45, 139, 0, 149, 0, 249, 117, 249, 62, 182, 0, 189, 0, 249, 42, 249, 39, 142, 0, 173, 0, 145, 0, 156, 0, 249, 60, 183, 0, 141, 0, 155, 0, 182, 0, 249, 39, 175, 0, 170, 0, 155, 0, 149, 0, 249, 103, 249, 42, 249, 118, 118, 0, 132, 0, 136, 0, 222, 0, 134, 0, 136, 0, 162, 0, 184, 0, 249, 36, 249, 106, 142, 0, 120, 0, 140, 0, 232, 0, 184, 0, 139, 0, 158, 0, 214, 0, 239, 0, 249, 156, 110, 0, 112, 0, 228, 0, 192, 0, 168, 0, 146, 0, 176, 0, 172, 0, 163, 0, 112, 0, 173, 0, 239, 0, 120, 0, 172, 0, 146, 0, 173, 0, 140, 0, 136, 0, 191, 0, 92, 0, 125, 0, 165, 0, 162, 0, 216, 0, 249, 55, 158, 0, 128, 0, 249, 95, 249, 60, 221, 0, 127, 0, 249, 102, 249, 24, 148, 0, 155, 0, 249, 74, 249, 62, 153, 0, 138, 0, 249, 115, 249, 64, 165, 0, 147, 0, 249, 77, 249, 56, 143, 0, 163, 0, 249, 100, 249, 58, 168, 0, 146, 0, 249, 80, 249, 45, 158, 0, 168, 0, 249, 110, 249, 31, 168, 0, 134, 0, 249, 38, 104, 0, 187, 0, 134, 0, 160, 0, 132, 0, 135, 0, 142, 0, 249, 108, 145, 0, 157, 0, 154, 0, 152, 0, 249, 82, 136, 0, 163, 0, 165, 0, 146, 0, 249, 459, 117, 0, 146, 0, 249, 90, 130, 0, 166, 0, 154, 0, 194, 0, 249, 68, 154, 0, 140, 0, 158, 0, 168, 0, 249, 97, 155, 0, 146, 0, 134, 0, 184, 0, 249, 708, 249, 710, 249, 741, 249, 708, 249, 419, 126, 0, 119, 0, 82, 0, 112, 0, 84, 0, 98, 0, 86, 0, 109, 0, 98, 0, 113, 0, 249, 63, 55, 0, 158, 0, 135, 0, 210, 0, 161, 0, 138, 0, 249, 61, 141, 0, 195, 0, 139, 0, 155, 0, 223, 0, 248};
const PROGMEM byte arrayTreeMC[] = {17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 5, 4, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 5, 4, 9, 8, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 3, 2, 3, 2, 5, 4, 5, 4, 9, 8, 17, 16, 17, 16, 17, 16, 31, 30, 31, 30, 31, 30, 31, 30, 5, 4, 5, 4, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 31, 30, 31, 30, 31, 30, 31, 30, 3, 2, 5, 4, 3, 2, 17, 16, 3, 2, 5, 4, 3, 2, 17, 16, 3, 2, 5, 4, 3, 2, 17, 16, 3, 2, 5, 4, 3, 2, 17, 16, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 5, 4, 11, 10, 17, 16, 15, 14, 3, 2, 5, 4, 17, 16, 3, 2, 5, 4, 3, 2, 9, 8, 3, 2, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 17, 16, 17, 16, 9, 8, 5, 4, 3, 2, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 17, 16, 17, 16, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 5, 4, 3, 2, 5, 4, 5, 4, 5, 4, 9, 8, 17, 16, 5, 4, 9, 8, 9, 8, 9, 8, 17, 16, 13, 12, 3, 2, 3, 2, 5, 4, 9, 8, 9, 8, 17, 16, 9, 8, 3, 2, 5, 4, 9, 8, 9, 8, 9, 8, 17, 16, 3, 2, 5, 4, 5, 4, 5, 4, 9, 8, 9, 8, 9, 8, 17, 16, 17, 16, 17, 16, 5, 4, 3, 2, 3, 2, 5, 4, 5, 4, 5, 4, 9, 8, 17, 16, 3, 2, 5, 4, 5, 4, 9, 8, 17, 16, 9, 8, 3, 2, 5, 4, 3, 2, 9, 8, 3, 2, 5, 4, 3, 2, 17, 16, 3, 2, 5, 4, 3, 2, 17, 16, 3, 2, 5, 4, 3, 2, 17, 16, 17, 16, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 9, 8, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 31, 30, 31, 30, 17, 16, 9, 8, 17, 16, 9, 8, 31, 30, 17, 16, 9, 8, 17, 16, 9, 8, 31, 30, 17, 16, 9, 8, 17, 16, 9, 8, 31, 30, 17, 16, 9, 8, 17, 16, 9, 8, 31, 30, 17, 16, 9, 8, 17, 16, 9, 8, 31, 30, 17, 16, 9, 8, 17, 16, 9, 8, 31, 30, 17, 16, 9, 8, 17, 16, 17, 16, 3, 2, 5, 4, 9, 8, 17, 16, 31, 30, 5, 4, 9, 8, 17, 16, 3, 2, 5, 4, 9, 8, 17, 16, 3, 2, 5, 4, 3, 2, 9, 8, 3, 2, 5, 4, 3, 2, 17, 16, 3, 2, 5, 4, 3, 2, 9, 8, 3, 2, 5, 4, 3, 2, 17, 16, 3, 2, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 17, 16, 17, 16, 9, 8, 5, 4, 3, 2, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 17, 16, 17, 16, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 9, 8, 17, 16, 3, 2, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 17, 16, 3, 2, 5, 4, 3, 2, 5, 4, 11, 10, 21, 20, 11, 10, 21, 20, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 3, 2, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 17, 16, 17, 16, 9, 8, 5, 4, 3, 2, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 17, 16, 17, 16, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 17, 16, 9, 8, 5, 4, 3, 2, 3, 2, 5, 4, 9, 8, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 9, 8, 17, 16, 13, 12, 11, 10, 21, 20, 11, 10, 21, 20, 3, 2, 11, 10, 29, 28, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 21, 20, 11, 10, 21, 20, 11, 10, 31, 30, 11, 10, 21, 20, 11, 10, 31, 30, 11, 10, 21, 20, 11, 10, 31, 30, 11, 10, 21, 20, 11, 10, 31, 30, 11, 10, 21, 20, 11, 10, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 21, 20, 11, 10, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 5, 4, 9, 8, 3, 2, 17, 16, 5, 4, 9, 8, 3, 2, 17, 16, 5, 4, 9, 8, 3, 2, 5, 4, 17, 16, 3, 2, 9, 8, 5, 4, 17, 16, 3, 2, 5, 4, 9, 8, 3, 2, 17, 16, 5, 4, 31, 30};
const PROGMEM uint16_t arrayTimesHN[] = {28, 246, 1542, 246, 646, 246, 390, 246, 38, 246, 1222, 246, 70, 246, 358, 246, 70, 246, 326, 246, 70, 246, 1542, 246, 326, 246, 70, 246, 358, 246, 70, 246, 650, 246, 290, 246, 70, 246, 680, 246, 320, 246, 70, 246, 1550, 246, 1540, 246, 640, 246, 330, 246, 70, 246, 1250, 246, 70, 246, 360, 246, 70, 246, 330, 246, 0, 246, 1550, 246, 390, 246, 0, 246, 360, 246, 70, 246, 650, 246, 320, 246, 100, 246, 620, 246, 360, 246, 70, 246, 1540, 246, 1250, 246, 40, 246, 650, 246, 670, 246, 650, 246, 650, 246, 350, 246, 40, 246, 360, 246, 40, 246, 640, 246, 360, 246, 40, 246, 610, 246, 680, 246, 620, 246, 710, 246, 350, 246, 40, 246, 330, 246, 70, 246, 1540, 246, 680, 246, 640, 246, 650, 246, 360, 246, 40, 246, 630, 246, 660, 246, 640, 246, 360, 246, 70, 246, 650, 246, 640, 246, 330, 246, 70, 246, 330, 246, 70, 246, 640, 246, 650, 246, 640, 246, 360, 246, 70, 246, 620, 246, 640, 246, 650, 246, 360, 246, 70, 246, 610, 246, 680, 246, 640, 246, 330, 246, 100, 246, 650, 246, 610, 246, 360, 246, 70, 246, 360, 246, 40, 246, 640, 246, 650, 246, 640, 246, 650, 246, 680, 246, 610, 246, 650, 246, 680, 246, 610, 246, 650, 246, 670, 246, 650, 246, 680, 246, 640, 246, 650, 246, 360, 246, 40, 246, 640, 246, 650, 246, 250, 246, 250, 246, 246};
const PROGMEM byte arrayTreeHN[] = {31, 30, 17, 16, 17, 16, 17, 16, 9, 8, 9, 8, 5, 4, 5, 4, 9, 8, 5, 4, 3, 2, 5, 4, 5, 4, 9, 8, 17, 16, 5, 4, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 31, 30, 31, 30, 31, 30, 17, 16, 17, 16, 9, 8, 9, 8, 5, 4, 5, 4, 9, 8, 5, 4, 3, 2, 31, 30, 5, 4, 9, 8, 17, 16, 7, 6, 17, 16, 9, 8, 5, 4, 3, 2, 5, 4, 31, 30, 31, 30, 9, 8, 9, 8, 5, 4, 17, 16, 9, 8, 5, 4, 9, 8, 3, 2, 9, 8, 5, 4, 9, 8, 9, 8, 9, 8, 5, 4, 17, 16, 9, 8, 5, 4, 9, 8, 3, 2, 9, 8, 5, 4, 3, 2, 21, 20, 21, 20, 5, 4, 9, 8, 5, 4, 21, 20, 21, 20, 21, 20, 9, 8, 3, 2, 11, 10, 11, 10, 17, 16, 17, 16, 17, 16, 9, 8, 21, 20, 29, 28, 21, 20, 9, 8, 3, 2, 19, 18, 19, 18, 23, 22, 9, 8, 5, 4, 19, 18, 19, 18, 5, 4, 9, 8, 17, 16, 11, 10, 11, 10, 17, 16, 5, 4, 17, 16, 17, 16, 11, 10, 11, 10, 5, 4, 3, 2, 21, 20, 21, 20, 17, 16, 3, 2, 19, 18, 13, 12, 15, 14, 29, 28, 21, 20, 9, 8, 5, 4, 3, 2, 5, 4, 19, 18, 31, 30, 31, 30, 31, 30, 30};

//put arrays here

void setup() {
  initSD();
  initMP3Player();

  pinMode(TREEONE, OUTPUT);
  pinMode(TREETWO, OUTPUT);
  pinMode(TREETHREE, OUTPUT);
  pinMode(TREEFOUR, OUTPUT);

  digitalWrite(TREEONE, HIGH);
  digitalWrite(TREETWO, HIGH);
  digitalWrite(TREETHREE, HIGH);
  digitalWrite(TREEFOUR, HIGH);
}

void loop() {
  MP3player.playTrack(2);
  OHolyNight();
  delay(92000);
  MP3player.stopTrack();

  MP3player.playTrack(3);
  MetallicaChristmas();
  delay(192000);
  MP3player.stopTrack();
}

void initSD() {
  if (!sd.begin(SD_SEL, SPI_HALF_SPEED))
    sd.initErrorHalt();
  if (!sd.chdir("/"))
    sd.errorHalt("sd.chdir");
}

void initMP3Player() {
  uint8_t result = MP3player.begin();
  MP3player.setVolume(volume, volume);
  MP3player.setMonoMode(monoMode);
}

void AllLights() {
  digitalWrite(TREEONE, LOW);
  digitalWrite(TREETWO, LOW);
  digitalWrite(TREETHREE, LOW);
  digitalWrite(TREEFOUR, LOW);
}

void MetallicaChristmas() {
  for (i = 0; i < sizeof(arrayTimesMC); i++) {
    delay(pgm_read_word_near(arrayTimesMC + i));
    num = pgm_read_byte_near(arrayTreeMC + i);

    if (num > 15) {
      if ((num % 2) == 0) {
        digitalWrite(TREEONE, HIGH);
      } else {
        digitalWrite(TREEONE, LOW);
      }
    }
    if ((num % 16) > 7) {
      if ((num % 2) == 0) {
        digitalWrite(TREETWO, HIGH);
      } else {
        digitalWrite(TREETWO, LOW);
      }
    }
    if ((num % 8) > 3) {
      if ((num % 2) == 0) {
        digitalWrite(TREETHREE, HIGH);
      } else {
        digitalWrite(TREETHREE, LOW);
      }
    }
    if ((num % 4) > 1) {
      if ((num % 2) == 0) {
        digitalWrite(TREEFOUR, HIGH);
      } else {
        digitalWrite(TREEFOUR, LOW);
      }
    }
    if (i == sizeof(arrayTimesMC)) {
      break;
    }
  }
}

void OHolyNight() {
  for (i = 0; i < sizeof(arrayTimesHN); i++) {
    delay(pgm_read_word_near(arrayTimesHN + i));
    num = pgm_read_byte_near(arrayTreeHN + i);

    if (num > 15) {
      if ((num % 2) == 0) {
        digitalWrite(TREEONE, HIGH);
      } else {
        digitalWrite(TREEONE, LOW);
      }
    }
    if ((num % 16) > 7) {
      if ((num % 2) == 0) {
        digitalWrite(TREETWO, HIGH);
      } else {
        digitalWrite(TREETWO, LOW);
      }
    }
    if ((num % 8) > 3) {
      if ((num % 2) == 0) {
        digitalWrite(TREETHREE, HIGH);
      } else {
        digitalWrite(TREETHREE, LOW);
      }
    }
    if ((num % 4) > 1) {
      if ((num % 2) == 0) {
        digitalWrite(TREEFOUR, HIGH);
      } else {
        digitalWrite(TREEFOUR, LOW);
      }
    }
    if (i == sizeof(arrayTimesHN)) {
      break;
    }
  }
}
  • 1
    I think this part ` sizeof(arrayTimesHN)` doesn't work as you think. the random notes may be played, because the for loop proceedes further than it should, reading memory, that does not belong to the array. Try to use a constant or a define at this point. Maybe someone else can write an answer, what the sizeof really does here (I'm not sure about it and this is just a guess) – chrisl Dec 28 '18 at 21:07
  • @chrisl The sizeof operator returns size of the variable in bytes. It's used on the array of uint16_t, so the result will be element count times 2. As it is now, it'll result in buffer overflow. – KIIV Dec 28 '18 at 21:21
  • Well, it's not really buffer overflow as it's not overwritten, it's just reading the data beyond the end of array – KIIV Dec 28 '18 at 21:32
0

Since KIIV didn't write an answer to this question, I will do it.

You are using this for loop:

for (i = 0; i < sizeof(arrayTimesHN); i++)

The sizeof() function will return the size of the array in bytes. Since the array is of type uint16_t, each element has a size of 2 bytes (16 bit). Concluding: The sizeof() function will return a number twice as high, as the actual element count of the array. The delays and state number, that you use for the LEDs, will be read from memory, that lies after the array. We don't know, what is saved there (or if there is actually some valid data or only random data), so this will seem random. That random notes are played by the MP3 Player must be a problem with the library (I'm not sure about that). What does the library do, if a song is finished, but you didn't call the stopTrack() method yet?


In the LED functions (for example OHolyNight()) you are using delays, but you also set a big delay after the execution of the function. I guess, the big delay is the duration of the song. So you delay twice. The first comes with the accumulated delays in the LED function (while the LED lights are changing). The second delay will kick in after that (when the LED function is finished). Maybe you thought, that it doesn't proceed to the next song, because you didn't wait for 92 seconds (your first big delay).


Lastly, in your LED functions you have this if statement:

if (i == sizeof(arrayTimesHN)) {
  break;
}

This is unnecessary, you used a for loop with the condition i < sizeof(arrayTimesHN), so that i cannot ever be equal to sizeof(arrayTimesHN) inside of the loop.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.