Switch case block the code even if not execute

Hi guys,
I’m going crazy with this piece of code (4 digit display with number 0-99 chosen by the rotary encoder and based on the number choosed, pressing the button the system sends some IR commands)
If I disable the whole “switch case” the sketch works flawlessly, but as soon as I enable it, it stops working but without any error in the code (I see that if I rotate the encoder the display doesn’t change numbers).
How is that possible? Even because if there was some problem with the code, that code is executed only if I push the encoder button!!!

#include <AltSoftSerial.h>
#include "TM1637Display.h"
#include <IRremote.h>

IRsend irsend;

/* Define digital pins used for the display */
const int CLK_display = 2; //Set the CLK pin connection to the display
const int DIO = 4; //Set the DIO pin connection to the display

/* Define digital pins used to read the encoder */
#define DT 6
#define CLK_rotary 7
#define SW 8

/* Define digital pin used for the IR LED (to send) */
#define IR_pin 3

TM1637Display display(CLK_display, DIO);  //set up the 4-Digit Display.
//SoftwareSerial mySerial(10, 11); // RX, TX

unsigned int PowerOn[] = {....};
unsigned int PowerOff[] = {....};
unsigned int Menu[] = {....};
unsigned int UP[] = {....};
unsigned int DOWN[] = {....};
unsigned int RIGHT[] = {....};
unsigned int LEFT[] = {....};
unsigned int EXIT[] = {....};
unsigned int OK[] = {....};
unsigned int SOURCE[] = {....};
unsigned int BTN_1[67] = {....};
unsigned int BTN_2[67] = {....};
unsigned int BTN_3[67] = {....};
unsigned int BTN_4[67] = {....};
unsigned int BTN_5[67] = {....};
unsigned int BTN_6[67] = {....};
unsigned int BTN_7[67] = {....};
unsigned int BTN_8[67] = {....};
unsigned int BTN_9[67] = {....};
unsigned int BTN_0[67] = {....};


void setup()
{
  byte rand;
  rand = random(1000);
  Serial.begin(9600);
  Serial.print(rand);
  pinMode(DT, INPUT);   		//define pins for the rotary encoder
  pinMode(CLK_rotary, INPUT); 	//define pins for the rotary encoder
  pinMode(SW, INPUT); 			//define pins for the rotary encoder
  digitalWrite(SW, HIGH);		
  pinMode (IR_pin, OUTPUT);  	//output as used in IR library
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  display.setBrightness(0x0c);  //set the diplay to maximum brightness
}

/* Main program */
void loop()
{
 byte counter;
 byte DialPos;
 byte Last_DialPos;
 int Last_counter;
 //String command="NULL";
 byte checksum=0x00;
 byte khz=38; 					//frequency to transmit on the IR LED; NB Change this default value as neccessary to the correct modulation frequency
 byte i;
 uint8_t ones, tens;

 // Define Rs-232 commands
uint8_t Power_on[6] = {0xAA, 0x11, 0xFE, 0x01, 0x01, 0x11};
uint8_t Power_off[6] = {0xAA, 0x11, 0xFE, 0x01, 0x00, 0x10};
uint8_t Input_PC[6] = {0xAA, 0x14, 0xFE, 0x01, 0x14, 0x27};
uint8_t Input_HDMI[6] = {0xAA, 0x14, 0xFE, 0x01, 0x21, 0x34};
uint8_t Volume_0[6] = {0xAA, 0x12, 0xFE, 0x01, 0x00, 0x11};
uint8_t Volume_20[6] = {0xAA, 0x12, 0xFE, 0x01, 0x14, 0x25};
 
unsigned int BTN_tens[67];
unsigned int BTN_ones[67];
char input=' ';

  /* Reset the counter */
 counter = 0;

  /* Continiouously read the state of the encoder */
  while(1)
  {
    /* Read the status of the dial */
    DialPos = (digitalRead(CLK_rotary) << 1) | digitalRead(DT);
 
    /* Is the dial being turned anti-clockwise? */
    if (DialPos == 3 && Last_DialPos == 1)
    {
      if (counter == 0)
      {
      	counter = 99;
      }
      else
      {
      	counter--;
      }
    }
 
    /* Is the dial being turned clockwise? */
    if (DialPos == 3 && Last_DialPos == 2)
    {
      if (counter == 99)
      {
      	counter = 0;
      }
      else
      {
      	counter++;
      }
    }
    
    if (counter != Last_counter)
    {
      /* Output the counter to the serial port */
      Serial.println(counter);
      display.showNumberDec(counter); //Display the Variable value;
    }

    /* Is the switch pressed? */
    if(!digitalRead(SW))
    {
	  Serial.println("Switch pressed!");
	  if ((counter >0) && (counter <=49)){
	  	tens = counter / 10;
  		ones = counter - tens*10;
	  	switch (tens){
			case 0: 
				memcpy(BTN_0, BTN_tens, 67);
				break;
			case 1: 
				memcpy(BTN_1, BTN_tens, 67);
				break;
			case 2: 
				memcpy(BTN_2, BTN_tens, 67);
				break;
			case 3: 
				memcpy(BTN_3, BTN_tens, 67);
				break;
			case 4: 
				memcpy(BTN_4, BTN_tens, 67);
				break;
			case 5: 
				memcpy(BTN_5, BTN_tens, 67);
				break;
			case 6: 
				memcpy(BTN_6, BTN_tens, 67);
				break;
			case 7: 
				memcpy(BTN_7, BTN_tens, 67);
				break;
			case 8: 
				memcpy(BTN_8, BTN_tens, 67);
				break;
			case 9: 
				memcpy(BTN_9, BTN_tens, 67);
				break;
		}
	  	/*	irsend.sendRaw(Menu, sizeof(Menu)/sizeof(int), khz);
      	    Serial.println(Menu);
      	    delay(200);
      	    irsend.sendRaw(RIGHT, sizeof(RIGHT)/sizeof(int), khz);
      	    Serial.println(RIGHT);
      	    delay(200);
      	    irsend.sendRaw(DOWN, sizeof(DOWN)/sizeof(int), khz);
      	    Serial.println(DOWN);
      	    delay(200);
      	    irsend.sendRaw(DOWN, sizeof(DOWN)/sizeof(int), khz);
      	    Serial.println(DOWN);
      	    delay(200);
      	    irsend.sendRaw(RIGHT, sizeof(RIGHT)/sizeof(int), khz);
      	    Serial.println(RIGHT);
      	    delay(200);*/
	  			/*for (i= 0; i<=34; i++){
					BTN_x[i] = standard_code[i];
				}
				for (i= 35; i<=69; i++){
					BTN_x[i] = BTN[counter][i];
				}*/
	  	irsend.sendRaw(PowerOn, sizeof(PowerOn)/sizeof(int), khz);
      	delay(200);
  		
  		Serial.print("Tens: ");
  		Serial.print(tens);
  		Serial.print("  ones: ");
  		Serial.println(ones);
	  }
      delay(50);
    }
    /* Save the state of the encoder */
    Last_DialPos = DialPos;
    Last_counter = counter;
  }
}

If I disable the whole "switch case" the sketch works flawlessly, but as soon as I enable it, it stops working

The compiler is pretty smart. It throws away useless code and variables. Without the switch statement, all those huge arrays are dumped in the bit bucket. With the switch statement, they go into SRAM, of which you do not have all that much.

Damn it...that's why!!!
So...suggestions on how I can resolve this problem?
I need those arrays because each one is a button of the remote

I need those arrays

But you don’t need them in SRAM. They can stay in flash memory, if you use the PROGMEM qualifier and the appropriate access functions.

Thanks Paul for pointing me there.
I'll work on it!

PaulS:
But you don't need them in SRAM. They can stay in flash memory, if you use the PROGMEM qualifier and the appropriate access functions.

andrpex:
Damn it...that's why!!!
So...suggestions on how I can resolve this problem?
I need those arrays because each one is a button of the remote

Do they really need to be 67*2 bytes long each, though?

PaulMurrayCbr:
Do they really need to be 67*2 bytes long each, though?

Well...I guess do, those are the timing that the remote uses

http://www.gammon.com.au/progmem

unsigned int BTN_1[67] = {....};

unsigned int BTN_2[67] = {…};
unsigned int BTN_3[67] = {…};
unsigned int BTN_4[67] = {…};
unsigned int BTN_5[67] = {…};
unsigned int BTN_6[67] = {…};
unsigned int BTN_7[67] = {…};
unsigned int BTN_8[67] = {…};
unsigned int BTN_9[67] = {…};
unsigned int BTN_0[67] = {…};

Do they need to be ints?

 memcpy(BTN_0, BTN_tens, 67);

memcpy copies bytes. An int is two bytes (on this platform). Thus you are copying only half the data.

Try sizeof:

 memcpy(BTN_0, BTN_tens, sizeof (BTN_0));

They are all numberbetween 0 and 62.000

memcpy copies bytes. An int is two bytes (on this platform). Thus you are copying only half the data.

Try sizeof:

 memcpy(BTN_0, BTN_tens, sizeof (BTN_0));

Thanks!

They are all numberbetween 0 and 62.000

And I'll bet they could all be factored into much smaller numbers.