1

I am working on a Seeburg Jukebox wall unit, I have it playing one track at a time form a SD card no problem. But what I need help with being new to programming is: 1. The code is long because it can play 160 songs. 2. would be nice to have it line up the next selection if possible. Any help with this code would be appreciated.

#include <SD.h>
#include <SPI.h>
#include <Adafruit_VS1053.h>

#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = 
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

int opto = 18;

void setup() {    
  Serial.begin(9600); 
  if (! musicPlayer.begin()) {}
  if (!SD.begin(CARDCS)) {}

  musicPlayer.setVolume(1,1);

  pinMode(opto, INPUT); //initialize the digital pin as an input from the opto-isolator.
  digitalWrite(opto, HIGH);} //turns on the internal pull up resistor.

void loop() {

  int number = 0;  // counts the alphabet pulses
  int letter = 0;  // counts the digit pulses.
  int buffer[4] = {1,1,1,1}; // buffer for reading input. If 4 reads in a row are all the same it is valid input.
  int buffndx = 0; 
  int pausecnt = 0; // counts the number of HIGHs read in.
  int maxpause = 30; // when pausecnt reaches maxpause we are at the pause between alpa and numeric or else we are at the end of transmission.
  int countpause = false;
  buffer[0] = 1;
  buffer[1] = 1;
  buffer[2] = 1;
  buffer[3] = 1;

Serial.println("SEEBURG CONSOLETTE JUKEBOX");

  //Number loop// 
  while( pausecnt < maxpause ) { 
     buffer[buffndx] = digitalRead(opto); // read the input pin    
     if(buffer[buffndx] != 1){}
    delay(8); // space out the reads a little.
     buffndx++; 
     if (buffndx == 4) buffndx = 0; // rotate the buffer index. 
     if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) ){ // we got valid input so proccess it. 
        pausecnt = 0; // we got our pulse so restart the clock for the next one.
        number++; //increment the number counter.          
        while(! ((buffer[0] == 1 ) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1)) ) { //Loop until we get a valid HIGH
        buffer[buffndx] = digitalRead(opto); // read the input pin
        buffndx++; // rotate the buffer index
    if (buffndx == 4) buffndx = 0;} // rotate the buffer index.
        countpause = true; //we are receiving input so start counting pauses instead of looping forever.
        continue;}else{
    if (countpause == true) pausecnt++;}} // else no valid input


  // Letter Loop//
    pausecnt = 0;   // Reset the pausent counter. Ready for the next stream of pulses to arrive.
    countpause = false; // loop forever until a pulse is recieved.
    while( pausecnt < maxpause ) { // Begin the digit loop
    buffer[buffndx] = digitalRead(opto); // read the input pin
    delay(8);
    buffndx++; 
    if (buffndx == 4) buffndx = 0; // rotate the buffer index.
    if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) ){ // we got valid input so proccess it. 
        pausecnt = 0; // we got our pulse so restart the clock for the next one.
        letter++; //increment the aphabet counter.         
        while(! ((buffer[0] == 1) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1)) ) { //Loop until we get a valid HIGH
        buffer[buffndx] = digitalRead(opto); // read the input pin
        buffndx++; // rotate the buffer index
    if (buffndx == 4) buffndx = 0;} // rotate the buffer index.  
        countpause = true; //we are receiving input so start counting pauses instead of looping forever.
        continue;}else{
     if (countpause == true) pausecnt++;} // else no valid input// else no valid input

  }

 //change signal//
    {number = 10 - number;
    letter = 21 - letter;}

  String signal = "";

  if(letter == 1) signal += "A";
  if(letter == 2) signal += "B";
  if(letter == 3) signal += "C";
  if(letter == 4) signal += "D";
  if(letter == 5) signal += "E";
  if(letter == 6) signal += "F";
  if(letter == 7) signal += "G";
  if(letter == 8) signal += "H";
  if(letter == 9) signal += "J";
  if(letter == 10) signal += "K";
  if(letter == 11) signal += ("L");
  if(letter == 12) signal += ("M");
  if(letter == 13) signal += ("N");
  if(letter == 14) signal += ("P");
  if(letter == 15) signal += ("Q");
  if(letter == 16) signal += ("R");
  if(letter == 17) signal += ("S");
  if(letter == 18) signal += ("T");
  if(letter == 19) signal += ("U");
  if(letter == 20) signal += ("V");

  signal += String(number);
  Serial.println(signal);
  delay(1000);

        //////////A////////////
   if(letter == 1 && number == 1);
   {musicPlayer.playFullFile("A1.mp3");}

   if(letter == 1 && number == 2);
   {musicPlayer.playFullFile("A2.mp3");} 

   if(letter == 1 && number == 3);
   {musicPlayer.playFullFile("A3.mp3");} 

   if(letter == 1 && number == 4);
  {musicPlayer.playFullFile("A4.mp3");}

   if(letter == 1 && number == 5);
  {musicPlayer.playFullFile("A5.mp3");} 

   if(letter == 1 && number == 6);
  {musicPlayer.playFullFile("A6.mp3");} 

   if(letter == 1 && number == 7);
  {musicPlayer.playFullFile("A7.mp3");} 

   if(letter == 1 && number == 8);
   {musicPlayer.playFullFile("A8.mp3");}

           //////////B////////////
   if(letter == 2 && number == 1);
   {musicPlayer.playFullFile("B1.mp3");} 

   if(letter == 2 && number == 2);
   {musicPlayer.playFullFile("B2.mp3");} 

   if(letter == 2 && number == 3);
   {musicPlayer.playFullFile("B3.mp3");} 

   if(letter == 2 && number == 4);
   {musicPlayer.playFullFile("B4.mp3");}

   if(letter == 2 && number == 5);
   {musicPlayer.playFullFile("B5.mp3");} 

   if(letter == 2 && number == 6);
  {musicPlayer.playFullFile("B6.mp3");} 

   if(letter == 2 && number == 7);
   {musicPlayer.playFullFile("B7.mp3");} 

   if(letter == 2 && number == 8);
   {musicPlayer.playFullFile("B8.mp3");}

    //////////C////////////
   if(letter == 3 && number == 1);
   {musicPlayer.playFullFile("C1.mp3");} 

   if(letter == 3 && number == 2);
   {musicPlayer.playFullFile("C2.mp3");} 

   if(letter == 3 && number == 3);
   {musicPlayer.playFullFile("C3.mp3");} 

   if(letter == 3 && number == 4);
   {musicPlayer.playFullFile("C4.mp3");}

   if(letter == 3 && number == 5);
   {musicPlayer.playFullFile("C5.mp3");} 

   if(letter == 3 && number == 6);
    {musicPlayer.playFullFile("C6.mp3");}

   if(letter == 3 && number == 7);
    {musicPlayer.playFullFile("C7.mp3");} 

   if(letter == 3 && number == 8);
    {musicPlayer.playFullFile("C8.mp3");}

       //////////D////////////
   if(letter == 4 && number == 1);
   {musicPlayer.playFullFile("D1.mp3");} 

   if(letter == 4 && number == 2);
   {musicPlayer.playFullFile("D2.mp3");} 

   if(letter == 4 && number == 3);
   {musicPlayer.playFullFile("D3.mp3");} 

   if(letter == 4 && number == 4);
   {musicPlayer.playFullFile("D4.mp3");}

   if(letter == 4 && number == 5);
   {musicPlayer.playFullFile("D5.mp3");} 

   if(letter == 4 && number == 6);
   {musicPlayer.playFullFile("D6.mp3");} 

   if(letter == 4 && number == 7);
   {musicPlayer.playFullFile("D7.mp3");} 

   if(letter == 4 && number == 8);
   {musicPlayer.playFullFile("D8.mp3");}


       //////////E////////////
   if(letter == 5 && number == 1);
   {musicPlayer.playFullFile("E1.mp3");} 

   if(letter == 5 && number == 2);
   {musicPlayer.playFullFile("E2.mp3");} 

   if(letter == 5 && number == 3);
   {musicPlayer.playFullFile("E3.mp3");} 

   if(letter == 5 && number == 4);
   {musicPlayer.playFullFile("E4.mp3");}

   if(letter == 5 && number == 5);
   {musicPlayer.playFullFile("E5.mp3");} 

   if(letter == 5 && number == 6);
   {musicPlayer.playFullFile("E6.mp3");} 

   if(letter == 5 && number == 7);
   {musicPlayer.playFullFile("E7.mp3");} 

   if(letter == 5 && number == 8);
   {musicPlayer.playFullFile("E8.mp3");} 

          //////////F////////////
   if(letter == 6 && number == 1);
   {musicPlayer.playFullFile("F1.mp3");} 

   if(letter == 6 && number == 2);
   {musicPlayer.playFullFile("F2.mp3");} 

   if(letter == 6 && number == 3);
   {musicPlayer.playFullFile("F3.mp3");} 

   if(letter == 6 && number == 4);
   {musicPlayer.playFullFile("F4.mp3");}

   if(letter == 6 && number == 5);
   {musicPlayer.playFullFile("F5.mp3");} 

   if(letter == 6 && number == 6);
   {musicPlayer.playFullFile("F6.mp3");} 

   if(letter == 6 && number == 7);
   {musicPlayer.playFullFile("F7.mp3");} 

   if(letter == 6 && number == 8);
   {musicPlayer.playFullFile("F8.mp3");} 


          //////////G////////////
   if(letter == 7 && number == 1);
  {musicPlayer.playFullFile("G1.mp3");} 

  if(letter == 7 && number == 2);
  {musicPlayer.playFullFile("G2.mp3");}

  if(letter == 7 && number == 3);
  {musicPlayer.playFullFile("G3.mp3");}

  if(letter == 7 && number == 4);
  {musicPlayer.playFullFile("G4.mp3");}

  if(letter == 7 && number == 5);
  {musicPlayer.playFullFile("G5.mp3");}

  if(letter == 7 && number == 6);
  {musicPlayer.playFullFile("G6.mp3");}

  if(letter == 7 && number == 7);
  {musicPlayer.playFullFile("G7.mp3");}

  if(letter == 7 && number == 8);
  {musicPlayer.playFullFile("G8.mp3");}

            //////////H//////////;//
   if(letter == 8 && number == 1);
  {musicPlayer.playFullFile("H1.mp3");} 

  if(letter == 8 && number == 2);
  {musicPlayer.playFullFile("H2.mp3");}

  if(letter == 8 && number == 3);
  {musicPlayer.playFullFile("H3.mp3");}

  if(letter == 8 && number == 4);
  {musicPlayer.playFullFile("H4.mp3");}

  if(letter == 8 && number == 5);
  {musicPlayer.playFullFile("H5.mp3");}

  if(letter == 8 && number == 6);
  {musicPlayer.playFullFile("H6.mp3");}

  if(letter == 8 && number == 7);
  {musicPlayer.playFullFile("H7.mp3");}

  if(letter == 8 && number == 8);
  {musicPlayer.playFullFile("H8.mp3");}

            //////////J//////////;//
   if(letter == 9 && number == 1);
  {musicPlayer.playFullFile("J1.mp3");} 

  if(letter == 9 && number == 2);
  {musicPlayer.playFullFile("J2.mp3");}

  if(letter == 9 && number == 3);
  {musicPlayer.playFullFile("J3.mp3");}

  if(letter == 9 && number == 4);
  {musicPlayer.playFullFile("J4.mp3");}

  if(letter == 9 && number == 5);
  {musicPlayer.playFullFile("J5.mp3");}

  if(letter == 9 && number == 6);
  {musicPlayer.playFullFile("J6.mp3");}

  if(letter == 9 && number == 7);
  {musicPlayer.playFullFile("J7.mp3");}

  if(letter == 9 && number == 8);
  {musicPlayer.playFullFile("J8.mp3");}

            //////////K////////////
   if(letter == 10 && number == 1);
  {musicPlayer.playFullFile("K1.mp3");} 

  if(letter == 10 && number == 2);
  {musicPlayer.playFullFile("K2.mp3");}

  if(letter == 10 && number == 3);
  {musicPlayer.playFullFile("K3.mp3");}

  if(letter == 10 && number == 4);
  {musicPlayer.playFullFile("K4.mp3");}

  if(letter == 10 && number == 5);
  {musicPlayer.playFullFile("K5.mp3");}

  if(letter == 10 && number == 6);
  {musicPlayer.playFullFile("K6.mp3");}

  if(letter == 10 && number == 7);
  {musicPlayer.playFullFile("K7.mp3");}

  if(letter == 10 && number == 8);
  {musicPlayer.playFullFile("K8.mp3");}


              //////////L////////////
   if(letter == 11 && number == 1);
  {musicPlayer.playFullFile("L1.mp3");} 

  if(letter == 11 && number == 2);
  {musicPlayer.playFullFile("L2.mp3");}

  if(letter == 11 && number == 3);
  {musicPlayer.playFullFile("L3.mp3");}

  if(letter == 11 && number == 4);
  {musicPlayer.playFullFile("L4.mp3");}

  if(letter == 11 && number == 5);
  {musicPlayer.playFullFile("L5.mp3");}

  if(letter == 11 && number == 6);
  {musicPlayer.playFullFile("L6.mp3");}

  if(letter == 11 && number == 7);
  {musicPlayer.playFullFile("L7.mp3");}

  if(letter == 11 && number == 8);
  {musicPlayer.playFullFile("L8.mp3");}

              //////////M////////////
   if(letter == 12 && number == 1);
  {musicPlayer.playFullFile("M1.mp3");} 

  if(letter == 12 && number == 2);
  {musicPlayer.playFullFile("M2.mp3");}

  if(letter == 12 && number == 3);
  {musicPlayer.playFullFile("M3.mp3");}

  if(letter == 12 && number == 4);
  {musicPlayer.playFullFile("M4.mp3");}

  if(letter == 12 && number == 5);
  {musicPlayer.playFullFile("M5.mp3");}

  if(letter == 12 && number == 6);
  {musicPlayer.playFullFile("M6.mp3");}

  if(letter == 12 && number == 7);
  {musicPlayer.playFullFile("M7.mp3");}

  if(letter == 12 && number == 8);
  {musicPlayer.playFullFile("M8.mp3");}

              //////////N////////////
   if(letter == 13 && number == 1);
  {musicPlayer.playFullFile("N1.mp3");} 

  if(letter == 13 && number == 2);
  {musicPlayer.playFullFile("N2.mp3");}

  if(letter == 13 && number == 3);
  {musicPlayer.playFullFile("N3.mp3");}

  if(letter == 13 && number == 4);
  {musicPlayer.playFullFile("N4.mp3");}

  if(letter == 13 && number == 5);
  {musicPlayer.playFullFile("N5.mp3");}

  if(letter == 13 && number == 6);
  {musicPlayer.playFullFile("N6.mp3");}

  if(letter == 13 && number == 7);
  {musicPlayer.playFullFile("N7.mp3");}

  if(letter == 13 && number == 8);
  {musicPlayer.playFullFile("N8.mp3");}

              //////////P////////////
   if(letter == 14 && number == 1);
  {musicPlayer.playFullFile("P1.mp3");} 

  if(letter == 14 && number == 2);
  {musicPlayer.playFullFile("P2.mp3");}

  if(letter == 14 && number == 3);
  {musicPlayer.playFullFile("P3.mp3");}

  if(letter == 14 && number == 4);
  {musicPlayer.playFullFile("P4.mp3");}

  if(letter == 14 && number == 5);
  {musicPlayer.playFullFile("P5.mp3");}

  if(letter == 14 && number == 6);
  {musicPlayer.playFullFile("P6.mp3");}

  if(letter == 14 && number == 7);
  {musicPlayer.playFullFile("P7.mp3");}

  if(letter == 14 && number == 8);
  {musicPlayer.playFullFile("P8.mp3");}

              //////////Q////////////
   if(letter == 15 && number == 1);
  {musicPlayer.playFullFile("Q1.mp3");} 

  if(letter == 15 && number == 2);
  {musicPlayer.playFullFile("Q2.mp3");}

  if(letter == 15 && number == 3);
  {musicPlayer.playFullFile("Q3.mp3");}

  if(letter == 15 && number == 4);
  {musicPlayer.playFullFile("Q4.mp3");}

  if(letter == 15 && number == 5);
  {musicPlayer.playFullFile("Q5.mp3");}

  if(letter == 15 && number == 6);
  {musicPlayer.playFullFile("Q6.mp3");}

  if(letter == 15 && number == 7);
  {musicPlayer.playFullFile("Q7.mp3");}

  if(letter == 15 && number == 8);
  {musicPlayer.playFullFile("Q8.mp3");}

              //////////R////////////
   if(letter == 16 && number == 1);
  {musicPlayer.playFullFile("R1.mp3");} 

  if(letter == 16 && number == 2);
  {musicPlayer.playFullFile("R2.mp3");}

  if(letter == 16 && number == 3);
  {musicPlayer.playFullFile("R3.mp3");}

  if(letter == 16 && number == 4);
  {musicPlayer.playFullFile("R4.mp3");}

  if(letter == 16 && number == 5);
  {musicPlayer.playFullFile("R5.mp3");}

  if(letter == 16 && number == 6);
  {musicPlayer.playFullFile("R6.mp3");}

  if(letter == 16 && number == 7);
  {musicPlayer.playFullFile("R7.mp3");}

  if(letter == 16 && number == 8);
  {musicPlayer.playFullFile("R8.mp3");}

              //////////S////////////
   if(letter == 17 && number == 1);
  {musicPlayer.playFullFile("S1.mp3");} 

  if(letter == 17 && number == 2);
  {musicPlayer.playFullFile("S2.mp3");}

  if(letter == 17 && number == 3);
  {musicPlayer.playFullFile("S3.mp3");}

  if(letter == 17 && number == 4);
  {musicPlayer.playFullFile("S4.mp3");}

  if(letter == 17 && number == 5);
  {musicPlayer.playFullFile("S5.mp3");}

  if(letter == 17 && number == 6);
  {musicPlayer.playFullFile("S6.mp3");}

  if(letter == 17 && number == 7);
  {musicPlayer.playFullFile("S7.mp3");}

  if(letter == 17 && number == 8);
  {musicPlayer.playFullFile("S8.mp3");}

              //////////T////////////
   if(letter == 18 && number == 1);
  {musicPlayer.playFullFile("T1.mp3");} 

  if(letter == 18 && number == 2);
  {musicPlayer.playFullFile("T2.mp3");}

  if(letter == 18 && number == 3);
  {musicPlayer.playFullFile("T3.mp3");}

  if(letter == 18 && number == 4);
  {musicPlayer.playFullFile("T4.mp3");}

  if(letter == 18 && number == 5);
  {musicPlayer.playFullFile("T5.mp3");}

  if(letter == 18 && number == 6);
  {musicPlayer.playFullFile("T6.mp3");}

  if(letter == 18 && number == 7);
  {musicPlayer.playFullFile("T7.mp3");}

  if(letter == 18 && number == 8);
  {musicPlayer.playFullFile("T8.mp3");}

              //////////U////////////
   if(letter == 19 && number == 1);
  {musicPlayer.playFullFile("U1.mp3");} 

  if(letter == 19 && number == 2);
  {musicPlayer.playFullFile("U2.mp3");}

  if(letter == 19 && number == 3);
  {musicPlayer.playFullFile("U3.mp3");}

  if(letter == 19 && number == 4);
  {musicPlayer.playFullFile("U4.mp3");}

  if(letter == 19 && number == 5);
  {musicPlayer.playFullFile("U5.mp3");}

  if(letter == 19 && number == 6);
  {musicPlayer.playFullFile("U6.mp3");}

  if(letter == 19 && number == 7);
  {musicPlayer.playFullFile("U7.mp3");}

  if(letter == 19 && number == 8);
  {musicPlayer.playFullFile("U8.mp3");}

            //////////V////////////
   if(letter == 20 && number == 1);
  {musicPlayer.playFullFile("V1.mp3");} 

  if(letter == 20 && number == 2);
  {musicPlayer.playFullFile("V2.mp3");}

  if(letter == 20 && number == 3);
  {musicPlayer.playFullFile("V3.mp3");}

  if(letter == 20 && number == 4);
  {musicPlayer.playFullFile("V4.mp3");}

  if(letter == 20 && number == 5);
  {musicPlayer.playFullFile("V5.mp3");}

  if(letter == 20 && number == 6);
  {musicPlayer.playFullFile("V6.mp3");}

  if(letter == 20 && number == 7);
  {musicPlayer.playFullFile("V7.mp3");}

  if(letter == 20 && number == 8);
  {musicPlayer.playFullFile("V8.mp3");}
}
3

This answer only involves help for the first problem:

  1. The code is long because it can play 160 songs

Also, you can align your code by selecting it and press Ctrl-K.

I will go part by part to improve it (leaving the manual work to you):

Align code

#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)

Align this code, making all the values under each other nicely and comments starting at the same column.

Use for loop

  buffer[0] = 1;
  buffer[1] = 1;
  buffer[2] = 1;
  buffer[3] = 1;

You can use a for loop, like:

for (int n = 0; n < 4; n++)
{
    buffer[n] = 1;
}

However, you can remove this code, since the values are already initialized in:

int buffer[4] = {1,1,1,1}; // buffer for reading input. If 4 reads in a row are all the same it is valid input.

Prevent almost identical duplicate code

In the number/letter loop part:

//Number loop// 
  while( pausecnt < maxpause ) { 
     buffer[buffndx] = digitalRead(opto); // read the input pin    
     if(buffer[buffndx] != 1){}
    delay(8); // space out the reads a little.
     buffndx++; 
     if (buffndx == 4) buffndx = 0; // rotate the buffer index. 
     if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) ){ // we got valid input so proccess it. 
        pausecnt = 0; // we got our pulse so restart the clock for the next one.
        number++; //increment the number counter.          
        while(! ((buffer[0] == 1 ) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1)) ) { //Loop until we get a valid HIGH
        buffer[buffndx] = digitalRead(opto); // read the input pin
        buffndx++; // rotate the buffer index
    if (buffndx == 4) buffndx = 0;} // rotate the buffer index.
        countpause = true; //we are receiving input so start counting pauses instead of looping forever.
        continue;}else{
    if (countpause == true) pausecnt++;}} // else no valid input


  // Letter Loop//
    pausecnt = 0;   // Reset the pausent counter. Ready for the next stream of pulses to arrive.
    countpause = false; // loop forever until a pulse is recieved.
    while( pausecnt < maxpause ) { // Begin the digit loop
    buffer[buffndx] = digitalRead(opto); // read the input pin
    delay(8);
    buffndx++; 
    if (buffndx == 4) buffndx = 0; // rotate the buffer index.
    if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) ){ // we got valid input so proccess it. 
        pausecnt = 0; // we got our pulse so restart the clock for the next one.
        letter++; //increment the aphabet counter.         
        while(! ((buffer[0] == 1) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1)) ) { //Loop until we get a valid HIGH
        buffer[buffndx] = digitalRead(opto); // read the input pin
        buffndx++; // rotate the buffer index
    if (buffndx == 4) buffndx = 0;} // rotate the buffer index.  
        countpause = true; //we are receiving input so start counting pauses instead of looping forever.
        continue;}else{
     if (countpause == true) pausecnt++;} // else no valid input// else no valid input

  }

First add brackets and align them (I do it for the Number part):

 while( pausecnt < maxpause ) 
 { 
   buffer[buffndx] = digitalRead(opto); // read the input pin    

   if(buffer[buffndx] != 1)
   {
   }

   delay(8); // space out the reads a little.
   buffndx++; 

   if (buffndx == 4)
   {
     buffndx = 0; // rotate the buffer index. 
   }

   if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) )
   {
     // we got valid input so proccess it. 
     pausecnt = 0; // we got our pulse so restart the clock for the next one.
     number++; //increment the number counter.          
     while(! ((buffer[0] == 1 ) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1)) ) 
     { 
        //Loop until we get a valid HIGH
        buffer[buffndx] = digitalRead(opto); // read the input pin
        buffndx++; // rotate the buffer index

        if (buffndx == 4) 
        {
          buffndx = 0;
        }
      } // rotate the buffer index.

      countpause = true; //we are receiving input so start counting pauses instead of looping forever.
      continue;
    }
    else
    {
      if (countpause == true) 
      {
        pausecnt++;
      }
    } // else no valid input

Note it is longer, but much clearer (clear code is more important than short code).

Now because the code starting from:

buffer[buffndx] = digitalRead(opto); // read the input pin 
...

Until the end of the number loop code is almost equal to the letter loop code, try to find the differences between the two parts: only the number and letter variable. So you make a new function (e.g. processKey), you pass the number or the letter (so for the number call processKey(number) and for the letter call processKey(letter). Note that both are integers. The new function will be:

int processKey(int key) // Item is a number or letter
{
    ...
    return key;
}

You return a key (which is the possibly increased value (because of number++ in the original code. A slightly advanced way is to pass it as pointer:

void processKey(int* key) // Item is a number or letter

and change the key with (*key)++; and call the functions with processKey(&number) and processKey(&letter).

Use define

Note that instead of 4, it's better to use a #define.

     if(buffer[buffndx] != 1){}

Remove dead code

This line does not anything; to make this clear, always align brackets:

     if(buffer[buffndx] != 1)
     {
     }

Always use brackets { }

if (buffndx == 4) buffndx = 0; // rotate the buffer index. 

Use:

if (buffndx == 4) 
{
    buffndx = 0; // rotate the buffer index. 
}

Avoid code duplication

     if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) ){ // we got valid input so proccess it. 
        pausecnt = 0; // we got our pulse so restart the clock for the next one.
        number++; //increment the number counter.          
        while(! ((buffer[0] == 1 ) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1)) ) { //Loop until we get a valid HIGH

In this part you check twice to see if values are 0 or 1, make a function for this (e.g. areAllValues(buffer, 0) for the first call and areAllValues(buffer, 1) for the next. If 0 and 1 are the only values use a boolean (True/False).

    if (buffndx == 4) buffndx = 0;} // rotate the buffer index.

Use here the same #define that I proposed above.

Do not check against true/false

    if (countpause == true) pausecnt++;}} // else no valid input

It is not needed to check against true:

    if (countpause) ...

Use correct casing

Also it's better to use always correct casing, so countPause instead of countpause.

Avoid repetition of if statements (1)

  if(letter == 1) signal += "A";
  if(letter == 2) signal += "B";
  if(letter == 3) signal += "C";
  if(letter == 4) signal += "D";
  if(letter == 5) signal += "E";
  if(letter == 6) signal += "F";
  if(letter == 7) signal += "G";
  if(letter == 8) signal += "H";
  if(letter == 9) signal += "J";
  if(letter == 10) signal += "K";
  if(letter == 11) signal += ("L");
  if(letter == 12) signal += ("M");
  if(letter == 13) signal += ("N");
  if(letter == 14) signal += ("P");
  if(letter == 15) signal += ("Q");
  if(letter == 16) signal += ("R");
  if(letter == 17) signal += ("S");
  if(letter == 18) signal += ("T");
  if(letter == 19) signal += ("U");
  if(letter == 20) signal += ("V");

Instead of this, use the ASCII character value.

 if ((letter >= 1) && (letter <= 20))
 {
     signal += (char) ((int)('A') + letter - 1);
 {

Avoid repetition of if statements (2)

For your largest part:

 if(letter == 1 && number == 1);
   {musicPlayer.playFullFile("A1.mp3");}

   if(letter == 1 && number == 2);
   {musicPlayer.playFullFile("A2.mp3");} 

 ...

Use two if statements, filling in the first and second character of the file name.

char[] fileName = "XX.mp3";

if ((letter >= 1) && (letter <= 20))
{
    if ((number >= 1) && (number <= 8))
    {
       fileName[0] = (char) ((int)('A') + letter - 1);
       fileName[1] = (char) ((int)('0') + number - 1);
       musicPlayer.playFullFile(fileName);
    }
}

Start with 0

Btw, for software engineers, it is more common to start arrays/counters etc. with 0 instead of 1; that saves some - 1 conversions.

| improve this answer | |
  • 1
    My interpretation of would be nice to have it line up the next selection if possible was to have the next song start playing once the current one is finished :-) – Gerben Apr 10 '19 at 15:23
  • I only addressed the first problem; I will add this in the answer. – Michel Keijzers Apr 10 '19 at 15:30

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.