Order For Declaring Variables?

I ran into a very odd issue while programming today...

The program has quite a few variables, some arrays, and booleans declared before setup().
In a function a boolean kept resetting once the function was exited, even though it had been declared in the first section of the listing!
Oddly enough another boolean declared immediately after the first worked fine.

As an experiment, I moved the boolean declarations up near the top of the declarations, but not right at the top. Suddenly it started working! I have never heard of this before, and didn't know the order of declaration was important. I moved all of the booleans up into the same area.

Has anyone else run into this issue?

Here's the listing, after the fix, up to setup()
boolean RPplayFlag=false; (third clump) was the problem child..

#include <SPI.h>
#include <avr/pgmspace.h>
#include <Wire.h>
#include <EEPROM.h>
//#include <avr/io.h>//??


//********temp buffers********
int tempInt=0;
byte temp1=0;
byte temp2=0;
byte tempArray[96];//used for core ram write(command 0x9c), and possibly other functions
int tempTemp=0;
byte ditherTime;
byte tempDith=0;

//*******timer buffers********
unsigned long milliTimer=0;
unsigned long milliTimerLast=0;
unsigned long microsDither=0;
unsigned long ledTimer=1500;//hold off on LEDs until power stable
unsigned long ledOffTimer=1500;
unsigned long microTimer=0;
unsigned long butTmr=1000;
unsigned long tempTester=3000;
unsigned long oneThird=0;
boolean  microRolledArm=false;
boolean microRolledFlag=false;

//Control Flags *PUTTING ALL BOOLS HERE!!!
boolean RPplayFlag=false;
boolean RPrecFlag=false;
boolean splashOn = true;
boolean but=true;
boolean  recButLast=true;
boolean  recButMask=false;
boolean  playButLast=true;
boolean  playButMask=false;
boolean  sliceButLast=true;
boolean  sliceButMask=false;
boolean  eqButLast=true;
boolean  eqButMask=false;







//********PIN DEFINES**********
//RGB
const int eqR=11;
const int eqG=13;
const int eqB=12;
const int slaveSelectPin=53;
const int analogOutRed = 2;
const int analogOutBlue = 3;
const int analogOutGreen = 4;
const int tdaSwitch = 30;
const int sliceTG=5;//temporary, PWM
const int slicePlayTG=6;//temporary, PWM
const int playTG=7;//temporary, PWM
const int playST=8;//temporary, PWM  //S-Trigger pullup (active High)
const int recTG=9;//temporary, PWM
const int recST=10;//temporary, PWM   //S-Trigger pullup (active High)
//buttons (may change) pins 44-34
const int recBut=43;
const int playBut=48;
const int sliceBut=44;
const int eqBut=9; //A PWM PIN
const int modeBut=42;
const int playSelBut=49;
const int recTgBut=8;
const int playTgBut=47;
const int LoopBut=46;
//const int pLoopBut=43;
const int sliceSelBut=45;
byte  butScan=0;
//LEDs (may change) pins 22-29 (8 rows)=PORTA0-7 for fast access and pins 30-33 (4 commons)=PORTC7-4
const int led0 = 22;//"rows" (ANODES) Each has resistor  
const int led1 = 23;
const int led2 = 24;
const int led3 = 25;
const int led4 = 26;
const int led5 = 27;
const int led6 = 28;
const int led7 = 29;//<<These to here are not necessary, remove if need more RAM
const int ledD = 37; //"commons" (CATHODES)
const int ledC = 36;
const int ledB = 35;
const int ledA = 34; //port c,3 led circle
const int specClk=10;
//LEDs Defined as bits in these 4 bytes
byte ledBuf0=B11111111;
byte ledBuf1=B11111111;  
byte ledBuf2=B11111111;
byte ledBuf3=B11111111; 
byte ledScan=0;
byte dimmer=0;
int splashStep=0;





//****** BUTTON DEBOUNCE ******
byte recButDB=0;
byte playButDB=0;
byte sliceButDB=0;
byte eqButDB=0;
byte modeButDB=0;
byte playSelButDB=0;
byte recTgButDB=0;
byte playTgButDB=0;
byte LoopButDB=0;
byte sliceSelButDB=0;
int  butHold=0;


//********ADC DEFINES**********
const int ratePot=A11;//0-5V, but zeroed at 2.5V: (RateCV-512)+(ratePot-512)
const int rateCV=A2;//zeroed at 2.5V
const int sgpPotCV=A5;//Start/Gmove/Position pot & CV; 0-5V
const int esPotCV=A4;//End/Size pot & CV ; 0-5V
const int eqPotCV=A1;//zeroed at 2.5V,512
const int peakADC=A0;
const int sliceCV=A6;
const int blendPot=A8;
const int feedBackPot=A7;//also offset with Expansion Module
const int specPin=A3;
const int startPot=A10;
const int endPot=A9;
int ratePotVal=0;
int startPotVal=0;
int endPotVal=0;
int fbkPotVal=0;
int blendPotVal=0;
byte potCnt=0;



// ********  TDA MOD FINISHED *******
byte tdaR[] = {//tda RP ini data
  0x20,0x00,0x00,0x00,0xC3,0x00,0xf0,0x40,0x00,0x6f,0x4f,0x0f,0xEE,0x00,0x00,0x2f,0x5f,0x5f,0x5f,0x00,0x00,0x00};
// 0    1    2    3    4    5    6    7    8     9   10   11   12   13   14   15   16   17   18   19   20   21  
//SRC nc     nc   nc SMUTE softS ss2 LOUD VOL TREB MIDR  BASS S/M/B LFo  RFo  LRo  RRo  SWL SWR 
//                                                   4f
//Notes: (4)subWoofer set to input MUX, softmute 1ms,(3)SubW flat **08=Main VOL: set so with EQ flat RR Max FBK-1db is enough
byte tdaL[] = {//tda FX ini data<<<check SOME OF THESE
  0x01,0x00,0x77,0x00,0x10,0x00,0x00,0x07,0x74,0xC7,0x00,0x60,0x03,0x60,0x20,0x60,0xd3,0x00};//was 66..
// 0    1    2    3    4    5    6    7    8     9   10   11   12   13   14   15   16  
//SRC LOUD SMUTE VOL TREB MIDR  BASS SRC2 S/M/B MIX LFo  RFo  LRo  RRo  MLEV  SWo  SPEC(MAY NEED ADJ)
byte   tdaSub=0;
byte  tdaData=0;
byte ditherFX=0x80;
byte ditherFXtoggle=0;

//********* SPI STUFF ***********
byte  rxBuf0=0;
byte  rxBuf1=0;
byte  rxBuf2=0;
byte  rxBuf3=0;
byte  rxBuf4=0;

//******** SLICE STUFF **********
byte  sliceStartAddr2[500];
byte  sliceStartAddr1[500];
byte  sliceStartAddr0[500];
byte  sliceEndAddr2[500]; //may need these, may not
byte  sliceEndAddr1[500];
byte  sliceEndAddr0[500];


//********* CORE STUFF *********
//Buffers to load to core for all
//modes/functions/core routines

byte setFlags= 0x00;
byte retFlags= 0x00;
byte RP_ST_REC_AD0= 0x00;
byte RP_ST_REC_AD1= 0x00;
byte RP_ST_REC_AD2= 0x00;
byte FX_ST_REC_AD0= 0x00;
byte FX_ST_REC_AD1= 0x00;
byte FX_ST_REC_AD2= 0x00;
byte RP_R_REC_AD0= 0x00;
byte RP_R_REC_AD1= 0x00;
byte RP_R_REC_AD2= 0x00;
byte FX_L_REC_AD0= 0x00;
byte FX_L_REC_AD1= 0x00;
byte FX_L_REC_AD2= 0x00;
byte RP_ST_PLAY_AD0= 0x00;
byte RP_ST_PLAY_AD1= 0x00;
byte RP_ST_PLAY_AD2= 0x00;
byte FX_ST_PLAY_AD0= 0x00;
byte FX_ST_PLAY_AD1= 0x00;
byte FX_ST_PLAY_AD2= 0x00;
byte RP_R_PLAY_AD0= 0x00;
byte RP_R_PLAY_AD1= 0x00;
byte RP_R_PLAY_AD2= 0x00;
byte FX_L_PLAY_AD0= 0x00;
byte FX_L_PLAY_AD1= 0x00;
byte FX_L_PLAY_AD2= 0x00;
byte RP_ST_RECSTOP_AD0= 0x00;
byte RP_ST_RECSTOP_AD1= 0x00;
byte RP_ST_RECSTOP_AD2= 0x00;
byte FX_ST_RECSTOP_AD0= 0x00;
byte FX_ST_RECSTOP_AD1= 0x00;
byte FX_ST_RECSTOP_AD2= 0x00;
byte RP_R_RECSTOP_AD0= 0x00;
byte RP_R_RECSTOP_AD1= 0x00;
byte RP_R_RECSTOP_AD2= 0x00;
byte FX_L_RECSTOP_AD0= 0x00;
byte FX_L_RECSTOP_AD1= 0x00;
byte FX_L_RECSTOP_AD2= 0x00;
byte RP_ST_RECMAX_AD0= 0x00;
byte RP_ST_RECMAX_AD1= 0x00;
byte RP_ST_RECMAX_AD2= 0x00;
byte FX_ST_RECMAX_AD0= 0x00;
byte FX_ST_RECMAX_AD1= 0x00;
byte FX_ST_RECMAX_AD2= 0x00;
byte RP_R_RECMAX_AD0= 0x00;
byte RP_R_RECMAX_AD1= 0x00;
byte RP_R_RECMAX_AD2= 0x00;
byte FX_L_RECMAX_AD0= 0x00;
byte FX_L_RECMAX_AD1= 0x00;
byte FX_L_RECMAX_AD2= 0x00;
byte FX_ST_START_AD0= 0x00;
byte FX_ST_START_AD1= 0x00;
byte FX_ST_START_AD2= 0x00;
byte FX_L_START_AD0= 0x00;
byte FX_L_START_AD1= 0x00;
byte FX_L_START_AD2= 0x00;
byte FX_ST_END_AD0= 0x00;
byte FX_ST_END_AD1= 0x00;
byte FX_ST_END_AD2= 0x00;
byte FX_L_END_AD0= 0x00;
byte FX_L_END_AD1= 0x00;
byte FX_L_END_AD2= 0x00;
byte FX_ST_LOOP_AD0= 0x00;
byte FX_ST_LOOP_AD1= 0x00;
byte FX_ST_LOOP_AD2= 0x00;
byte FX_L_LOOP_AD0= 0x00;
byte FX_L_LOOP_AD1= 0x00;
byte FX_L_LOOP_AD2= 0x00;
//end of addr stuff
byte STUT_R_LSB = 0x00;
byte STUT_L_MSB = 0x00;
byte STUT_L_LSB = 0x00;
byte STUT_R_MSB = 0x00;
byte T0CON = 0x00;
byte T1CON = 0x00;
byte T2CON = 0x00;
byte T3CON = 0x00;
byte T4CON = 0x7E;
byte T5CON = 0x00;
byte T6CON = 0x00;
byte TMR0_STEP = 0x01;
byte TMR1_STEP = 0x01;
byte TMR3_STEP = 0x01;
byte TMR5_STEP = 0x01;
byte TMR0_HI = 0xFE;//DEFAULT TO 48KS/S
byte TMR0_LO = 0x0C;
byte TMR1_HI = 0xFE;
byte TMR1_LO = 0x0C;
byte TMR3_HI = 0xFE;
byte TMR3_LO = 0x0C;
byte TMR5_HI = 0xFE;
byte TMR5_LO = 0x0C;
byte PR4 = 0x00;
byte PR6 = 0x00;
byte ADC_CONV_TIME = 0x00;
byte GMOVE_SIZE = 0x00;
byte CTRL_FLAGS = 0x00;
byte MODE_FLAGS = 0x00;
long unsigned playRpSt=0;





//spectrum
byte specStep=0;
int specBand[8]={};
byte specCnt=0;






//*********************************************************SETUP***************************************************************
void setup() {

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

How much memory is your code using?

I'd be surprised if the trouble you were having was really caused by the order of the variable declarations. I think it's more likely memory was being corrupted and moving the variable locations changed how things were being corrupted so the corruption wasn't as apparent.

If you post your full code we might be able to shed some light on the issue.

You'll want to use code blocks to post code rather than using quotes. The "</>" is the icon to insert code tags.

Yes, I suspect that which is why it's a concern.

The code that follow the declarations is

//*********************************************************SETUP***************************************************************
void setup() {
  Serial.begin(57600);  //  Set Com (test rs232)
  Serial1.begin(31250);  //  Set MIDI (if use)
  Serial2.begin(57600);  // Set to Link

  //atempting manual ADC w/interrupt
  // DIDR0 = 0xFF;            // digital inputs disabled
  // DIDR2 = 0x0F;
  // ADMUX = 0x41;            // measuring on ADC9, use the internal 1.1 reference
  //ADCSRA = B10101100;           // AD-converter on, interrupt enabled, prescaler = 16
  bitWrite(ADCSRA, 2, 1);//added to set conv down
  bitWrite(ADCSRA, 1, 0);
  bitWrite(ADCSRA, 0, 1);
  // ADCSRB = 0x48;           // AD channels MUX on, free running mode MUX5 is BIT3 in this!
  // bitWrite(ADCSRA, 6, 1);  // Start the conversion by setting bit 6 (=ADSC) in ADCSRA
  //  sei();                   // set interrupt flag

  //pin modes
  pinMode (slaveSelectPin, OUTPUT);
  DDRA = B11111111;//porta all out for led drivers
  //DDRL = B00000000;
  pinMode (ledA,OUTPUT); //LED Commons.Mostly for illustrative purposes, time doesn't matter here!
  pinMode (ledB,OUTPUT);
  pinMode (ledC,OUTPUT);
  pinMode (ledD,OUTPUT);
  pinMode (recBut,INPUT_PULLUP);
  pinMode (playBut,INPUT_PULLUP);
  pinMode (sliceBut,INPUT_PULLUP);
  pinMode (eqBut,INPUT_PULLUP);
  pinMode (modeBut,INPUT_PULLUP);
  pinMode (playSelBut,INPUT_PULLUP);
  pinMode (recTgBut,INPUT_PULLUP);
  pinMode (playTgBut,INPUT_PULLUP);
  pinMode (sliceSelBut,INPUT_PULLUP);
  pinMode (LoopBut,INPUT_PULLUP);
  pinMode (eqR,OUTPUT);
  pinMode (eqG,OUTPUT);
  pinMode (eqB,OUTPUT);
  pinMode (specClk,OUTPUT);


  SPI.setClockDivider(SPI_CLOCK_DIV8);//16uS  per byte should be ok. *Must have 20uS+space between Sends {ORIG=32}
  SPI.setDataMode(SPI_MODE3);//          Sample Rates no longer affect SPI rate, only core Mainloop (<20uS)
  // initialize SPI:
  SPI.begin();
  pinMode (tdaSwitch,OUTPUT);
  //allow power to stabilize etc.
  delay(200);

  Wire.begin(); // join i2c bus (address optional for master)   
  delay(100);
  // TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;<<this doesnt equate with my results, whatever!
  TWBR = 12;//255;//Two Wire Bit Rate Register:start with slowest (255=71khz) 14uS
  //TDA7718
  for(int i=0;i<19;i++){
    tdaSub=i;
    tdaData=tdaR[i];
    tdaRP(); 
    Serial.print(tdaSub,HEX);
    Serial.print("*");
    Serial.println(256+tdaData,BIN);
  }
  //TDA7419 (HAS SPECTRUM AN)
  for(int i=0;i<18;i++){
    tdaSub=i;
    tdaData=tdaL[i];
    tdaFX(); 
  }
  /*
  T1CON=0x01; //
   retFlags=0x02;
   TMR1_HI=0xfe;
   TMR1_LO=0x0c;
   TMR1_STEP=1;
   COMMAND_SET_TMR1();
   // SamplerSPI(0x8B,retFlags,T3CON,TMR3_STEP,TMR3_LO,TMR3_HI);
   // */
  Serial.print(rxBuf4,HEX);
  Serial.print(rxBuf3,HEX);  
  Serial.print(rxBuf2,HEX); 
  Serial.print(rxBuf1,HEX);     
  Serial.println(rxBuf0,HEX); 

  digitalWrite(specClk,HIGH);


  for(int i=0;i<500;i++){
    sliceStartAddr0[i]=0;
    sliceStartAddr1[i]=0;
    sliceStartAddr2[i]=0;
    sliceEndAddr0[i]=255;
    sliceEndAddr1[i]=255;
    sliceEndAddr2[i]=0x7f;
  }
  Serial.print(freeRam());

  ADC_CONV_TIME=44;
  COMMAND_SET_TMR2();
  // 4=RP_REC_ENABLE, 5=FX_REC_ENABLE
  setFlags=0x01; //0x11 wil make them go
  // SamplerSPI(0x81,retFlags,setFlags,RP_ST_REC_AD0,RP_ST_REC_AD1,RP_ST_REC_AD2);
  RP_ST_REC_AD2=0;
  RP_ST_REC_AD1=0;
  RP_ST_REC_AD0=0;
  RP_ST_PLAY_AD2=0x70;
  RP_ST_PLAY_AD1=0xfc;
  RP_ST_PLAY_AD0=0xff;
  retFlags=0x10;
  COMMAND_SET_REC();
  //


  COMMAND_SET_PLAY();

}

//********************************************END SETUP********************************************************

continued...

//******************************************** MAIN LOOP ******************************************************
//All routines are handled via Perif. events or Timers. Blend and Feedbak pots are scanned less often than other ADCs
//Max ADC resolution may be appx. 1mS (1khz) to limit USART flow to Expansion module & MIDI.
//Rate ADC(1) & EQ(4) zeroes at 512. All of the rest at 0. Rate control as CV ofset "zeroes" at 2.5V. (0->512->1023)
//All T/G inputs must be scanned every 500uS or so. ALL Buttons scanned >10mS.(1ms per)
//Spectrum Analyzer updates "inline" through ADC 9
//LED Scan Rate = >20mS/step. ledBufA-D(PORTC7-4) scan updates 8 Anode drivers on PortA every 20mS
void loop(){

  microTimer=micros();
  milliTimer=millis();


  if(microTimer & 0x80000000UL) microRolledArm=true;//rolls every 71.58278826_6 minutes
  if(microTimer<0x80000000UL && microRolledArm) {
    microRolledFlag=true;  //when rolls, may cause faster update once, but will even out on next!
    microRolledArm=false;  //use as:   "if(microTimer>WhateverTimer || microRolledFlag)" below, with uS stuff only
  }
  //
  //*******Timer routines*****
  //
  if(milliTimer != milliTimerLast){// <<<All MILLI routine calls / counts here
    milliTimerLast=milliTimer; 
    if(splashOn) splash();
    //Serial.println(analogRead(peakADC));
    //analogWrite(eqR,analogRead(peakADC));
    // bitWrite(ADCSRA, 6, 1);
    //  ditherFX++;

  } 

  if(milliTimer>tempTester){
    tempTester=milliTimer+10;
    potScan();
    if(tempDith==0)     ditherTime++;
  }

  if(microTimer>ledOffTimer) PORTC = PORTC | 0x0F; //controls brightness

  if(milliTimer>ledTimer) ledUD();//Upate LEDs every 20 mS

  if(microTimer>butTmr)     buttonScan();

  if(microTimer>oneThird){
    oneThird=microTimer+333; 
    specCnt++;
    if(specCnt>140) spectrum();
  }

  if(microTimer>microsDither){
    microsDither+=ditherTime+1800;
    ditherTime++;
    if((ditherFXtoggle & 0x80) == 0){// check if fbk dither is on(00-7f), if is count for dither 
      tdaSub=12;//dither
      if(ditherFX == ditherFXtoggle){
        tdaData=tdaL[12];// set by pot move
        tdaFX();
      }
      else{
        if(ditherFX==0) {
          tdaData=tdaL[12]+1;  
          tdaFX(); 
        }
      }
      ditherFX++;
      ditherFX=ditherFX & 0x7f;
    }

  }





  microRolledFlag=false;// <<leave at end! all timers will be updated by now.
}



//***********************************************  SPI ********************************************************
void SamplerSPI(int command, unsigned int core0,unsigned int core1,unsigned  int core2,unsigned int core3,unsigned int core4) {
  // if(command==0x83 || command==0x8c || command==0x85 ){//play/playnow
  //  tdaMute();
  //}
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin,LOW);
  //  send in the address and value via SPI:
  SPI.transfer(command);
  delayMicroseconds(20);
  rxBuf0=SPI.transfer(core0);
  delayMicroseconds(20);
  rxBuf1=SPI.transfer(core1);
  delayMicroseconds(20);
  rxBuf2=SPI.transfer(core2);
  delayMicroseconds(20);
  rxBuf3=SPI.transfer(core3);
  delayMicroseconds(20);
  rxBuf4=SPI.transfer(core4);
  delayMicroseconds(20);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin,HIGH); 

}


void tdaRP() {
  digitalWrite(tdaSwitch,HIGH);//switch clock to RP tda (0) (clk pulled up)
  Wire.beginTransmission(0x44); // transmit to tda, both are same address (unfortunately)
  Wire.write(tdaSub);            // sends instruction byte  
  Wire.write(tdaData);             // sends potentiometer value byte  
  Wire.endTransmission();     // stop transmitting. Can't return here until WIRE is done
}

void tdaFX() {
  digitalWrite(tdaSwitch,LOW);//switch clock to FX tda (1)
  Wire.beginTransmission(0x44); // transmit to tda
  Wire.write(tdaSub);            // sends instruction byte  
  Wire.write(tdaData);             // sends potentiometer value byte  
  Wire.endTransmission();     // stop transmitting
}



//**************************************LED UPDATE *************************************************
/*LEDs (may change) pins 22-29 (8 rows)=PORTA0-7 for fast access and pins 37-34 (4 commons)=PORTC3-0
 32 (31) LEDs are set/clred via ledBuf0-3. COMMON CATHODE (-) so ledBufx is 1=on/0=off
 bits+   0      1      2      3      4      5      6      7
 comD  circ1   circ2  circ3  circ4  circ5  circ6  circ7  circ8  LEDBUF0
 comC  R/P     peak   echo   lay    circC   ST     GM    POS    LEDBUF1
 comB  HI      MID     LO    REC    SLOOP  R-TR   R-FSW  R-GA   LEDBUF2
 comA  SL.S    SL.P   PLAY   n/c    PLOOP  P-TR   P-FSW  P-GA   LEDBUF3
 */
void ledUD() {
  ledTimer+=3UL;
  ledOffTimer=microTimer+100+(dimmer*8);
  if(ledScan==0){
    ledScan=1;
    PORTA = ledBuf0;
    PORTC = PORTC | 0x0F; //PORT{letter} |= _BV(P{letter}{number});  PORT{letter} &= ~_BV(P{letter}{number})
    PORTC = PORTC & 0xF7; //portc bit 3
    //strobe (common)
    return;
  }

  if(ledScan==1){
    ledScan=2;
    PORTA = ledBuf1;
    PORTC = PORTC | 0x0F;
    PORTC = PORTC & 0xfb;//portC bit 2
    return;
  }
  if(ledScan==2){
    ledScan=3;
    PORTA = ledBuf2;
    PORTC = PORTC | 0x0F;
    PORTC = PORTC & 0xfc; 
    return;
  }
  if(ledScan==3){
    ledScan=0;
    PORTA = ledBuf3;
    PORTC = PORTC | 0x0F;
    PORTC = PORTC & 0xfe; 
    return;
  }

}

void splash() {
  splashStep++;

  switch (splashStep){
  case  1000:
    if (splashStep>999) dimmer-=10;
    ledBuf0 = ledBuf0 << 1;
    break; 
  case  1060:
    ledBuf1 =ledBuf0 << 1;
    break; 
  case  1120:
    ledBuf2 = ledBuf0 << 1;
    break;
  case  1190:
    ledBuf3 = ledBuf0 << 1;
    splashStep=930;
    break;   
  }
  if (ledBuf3==0) {
    splashOn=false;
    ledBuf0=0;
    ledBuf1=B00110001;
    ledBuf2=B00110010;
    ledBuf3=B00111000;
    dimmer=200;
  }


}
//******************BUTTON SCAN***********************************************************

//Here every 1 mS, so takes 11 mS to do full scan
//Can change these to PORT "digital" once order is known
//do immediately. if a "hold" possibility, then only during whatever state will it not execute immediately
//DeBounce: Each button is masked after initial "low" state for set debounce time
void buttonScan(){
  butTmr=microTimer+500;
  switch (butScan){
  case 0x00: //recBut=43;
    but=bitRead(PINL,6);//will use this...
    if(!recButMask) { //if mask is off..
      if(!but && recButLast) { //if BUT is low now, but was high
        recButMask=true;
        recButLast=false;
        recButDB=0;
        ledBuf1=B11111111;
        //do whatever immediate function
        echoStart();
      }
      if(but && !recButLast) { //if BUT is back HI, but was low
        recButMask=true;  
        recButLast=true;
        recButDB=0;
        ledBuf1=B00110001; 
        //do whatever immediate release function 
        if (butHold>250) ledBuf1=0;  //test for >2.5secs
        if (butHold>94) {
          dimmer=random(255);
          //do whatever 1 second hold should do
          RPplayFlag=false;
          RPrecFlag=false; 
          setFlags=0x01;
          COMMAND_SET_PLAY();  
          COMMAND_SET_REC();      
        }            
        butHold=0;       
      }
    }
    if (recButMask) {
      recButDB++; //count for mask time
      if(recButDB>5) recButMask=false;  
    }
    if (!but) {
      butHold++;     
    } 
    break;

  case 0x01: //playBut=48;
    but=bitRead(PINL,1); 
    if(!playButMask) { //if mask is off..
      if(!but && playButLast) { //if BUT is low now, but was high
        playButMask=true;
        playButLast=false;
        playButDB=0;
        ledBuf1=B11111111;
        //do whatever immediate function
        tdaSub=13;
        tdaData=0x1f;
        tdaRP(); 
        tdaSub=10;
        tdaData=0x0f;
        tdaFX(); 
      }

      if(but && !playButLast) { //if BUT is back HI, but was low
        playButMask=true;  
        playButLast=true;
        playButDB=0;
        ledBuf1=B00110001; 
        //do whatever immediate release function 
        if (butHold>250) ledBuf1=0;  //test for >2.5secs
        if (butHold>94) {
          dimmer=random(255);
          //do whatever 1 second hold should do
          tdaSub=13;
          tdaData=0x01;
          tdaRP(); 
          tdaSub=10;
          tdaData=0x01;
          tdaFX(); 
        }            
        butHold=0;       
      }

    }
    if (playButMask) {
      playButDB++; //count for mask time
      if(playButDB>5) playButMask=false;  
    }
    if (!but) {
      butHold++;     
    }  
    break;

  case 0x02: //sliceBut=44;

    but=bitRead(PINL,5);
    if(!sliceButMask) { //if mask is off..
      if(!but && sliceButLast) { //if BUT is low now, but was high
        sliceButMask=true;
        sliceButLast=false;
        sliceButDB=0;
        ledBuf1=B11111111;
        //do whatever immediate function
      }
      if(but && !sliceButLast) { //if BUT is back HI, but was low
        sliceButMask=true;  
        sliceButLast=true;
        sliceButDB=0;
        ledBuf1=B00110001; 
        //do whatever immediate release function  
        if (butHold>94) {
          dimmer=random(255);
          //do whatever 1 second hold should do  
        }   
        butHold=0;     
      }
    }
    if (sliceButMask) {
      sliceButDB++; //count for mask time
      if(sliceButDB>5) sliceButMask=false;  
    }

    if (!but) {
      butHold++;    
    }    
    break;

  case 0x03: //eqBut=9;
    but=bitRead(PINH,6);
    break;

  case 0x04: //modeBut=42;
    but=bitRead(PINL,7);
    break;

  case 0x05: //playSelBut=49;
    but=bitRead(PINL,0);
    break;

  case 0x06: //recTgBut=8;
    but=bitRead(PINH,5);
    //but=bitRead(PINH,5);
    break;

  case 0x07: //playTgBut=47;
    but=bitRead(PINL,2);
    break;

  case 0x08: //LoopBut=46;
    but=bitRead(PINL,3);
    break;

  case 0x09: //sliceSelBut=45
    but=bitRead(PINL,4);
    break;
  }
  butScan++;
  if(butScan>0x09) butScan=0;
  //  if(butHold>254) butHold=254;
}

In a function a boolean kept resetting once the function was exited, even though it had been declared in the first section of the listing!

.

Which function? Please post the code of that function; it can be useful to also have the original sequence.

You're using a few arrays; e.g.

//******** SLICE STUFF **********
byte  sliceStartAddr2[500];
byte  sliceStartAddr1[500];
byte  sliceStartAddr0[500];
byte  sliceEndAddr2[500]; //may need these, may not
byte  sliceEndAddr1[500];
byte  sliceEndAddr0[500];


//********* CORE STUFF *********
//Buffers to load to core for all
//modes/functions/core routines

byte setFlags= 0x00;
byte retFlags= 0x00;

I can not see in your code where they are accessed (except for the initialisation). What happens if some function is writing one of those arrays but it writes location 500 or 501 of any of those? It will overwrite the bytes following.

So the below code will overwrite sliceStartAddr1[0]

sliceStartAddr2[500] = 0xFF;

And this will overwrite retFlags

sliceEndAddr0[501] = 123;

Check all your indices and all your pointers; you might be adding data to an array outside its boundaries.

//****************spectrum analyzer**********
//here every 1 ms after 50 mS high state. Maxes around 45. Low to high??
void spectrum() {
  if (specCnt & 0x01) {
    specBand[specStep] = analogRead(specPin); //Only [1-7] valid, zero is dummy.
    if(specStep<8) {
      digitalWrite(specClk,LOW);
      specStep++;
    }
    else{
      specStep=0;
      specCnt=0;
      analogWrite(eqR,(specBand[2]+specBand[1])*4);
      analogWrite(eqG,(specBand[4]+ specBand[5])*2);
      analogWrite(eqB,(specBand[6]));
      for(int i=1;i<8;i++){
        //   Serial.print (specBand[i]);
        //    Serial.print ("-");
      }
      //   Serial.println();
    }
  }
  else{
    digitalWrite(specClk,HIGH); 
  }
}



//*** Interrupt routine ADC ready ***/
ISR(ADC_vect) {
  //  bitClear(PORTB, 4); // marker low
  int  aval = ADCL;        // store lower byte ADC
  aval += ADCH << 8;  // store higher bytes ADC
  Serial.println(aval);
  //  bitSet(PORTB, 4);   // marker high
}

//This is temp routine for scanning the 5 pots (not attenuvertors)
//AN0=Rate, AN6=Blend, AN7=FBK, AN9=end, AN10=start
void potScan(){
  switch(potCnt){
  case   0x00: //Rate Pot.....
    tempInt=analogRead(ratePot);
    if (tempInt<ratePotVal || tempInt>ratePotVal+1 ) {
      ratePotVal=tempInt; 
    }
    break;
  case   0x01: //Blend
    break;
  case   0x02: //FBK *not finished yet!!!
    tempInt=analogRead(feedBackPot);
    if (tempInt<fbkPotVal || tempInt>fbkPotVal+1 ) {
      fbkPotVal=tempInt; 

      if(fbkPotVal>511){
        ditherFXtoggle=fbkPotVal & 0x7f; //128 dither values between each of 4 dB
        tdaL[12]=((fbkPotVal-512)/128)+1;
        tdaSub=12;
        tdaData=tdaL[12];
        tdaFX();
      }
      else{
        ditherFXtoggle=128; // so dither routine isn't active   
        tdaL[12]=(85-(fbkPotVal/6))+16;//find better way!
        tdaSub=12;
        tdaData=tdaL[12];
        tdaFX();
      }
      Serial.print(ditherFXtoggle);
      Serial.print("-");
      Serial.println(tdaL[12]);
      //  Serial.println(analogRead(peakADC));
    }
    break;  
  }
  potCnt++; 
  if(potCnt>4) potCnt=0;
}


int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}


void echoStart() {
  
       if ( RPplayFlag == false && RPrecFlag==true){  
                   Serial.print(RPplayFlag);
          RPplayFlag = true;         
          setFlags=0x11;
          RP_ST_PLAY_AD2=0x00;
          RP_ST_PLAY_AD1=0x00;
          RP_ST_PLAY_AD0=0x00;
          retFlags=0x10;
          COMMAND_SET_PLAY();
         
        }
       
        if(RPrecFlag==false){
          setFlags=0x11; //0x11 wil make them go
          //SamplerSPI(0x81,retFlags,setFlags,RP_ST_REC_AD0,RP_ST_REC_AD1,RP_ST_REC_AD2);
          RP_ST_REC_AD2=0;
          RP_ST_REC_AD1=0;
          RP_ST_REC_AD0=0;
          COMMAND_SET_REC();
          RPrecFlag=true;
        } 
           Serial.println(RPplayFlag);
}

Thanks for pointing out that code button BTW, my first ever post

I've added the serial.prints (last function in code) from troubleshooting and made the boolean "if's " blatently obvious, trying to solve the thing

void echoStart() is called each time a button is pressed, after having been debounced etc.
There's no other instances using RPplayFlag other than the booleans cleared after a button hold.

sterretje, sorry, I had to wait because of the posting in 5 minutes and > 900 characters thingies.

I understand that sliceStartAddr2[500] = 0xFF; would overwrite a following buffer as 500 is 0-499. I initiated them, but haven't got to that part of the program yet. They're just there to use up the ram they will in the future.

I think you're right though as in the first code I posted
int specBand[8]={}; .. which was originally int specBand[8]; now changed to int specBand[]={0,0,0,0,0,0,0,0,0}; seems to have fixed the problem. Any variables following that weren't global.

I even changed the booleans to a 2 bit counter and same thing happened. Once moved up, it all worked fine.

It makes me wonder how sliceStartAddr2[500] doesn't just erase the next 500 variables in that case, so it's still unexplained. Maybe there's a limit to how many arrays can be declared? Or perhaps it's because that array is an int array?

What Arduino is this for? The Uno only has 2k of SRAM memory and those big byte arrays are asking for 3k.

Must be a Mega; the setup shows Serial1 and Serial2 and a number of pins that the compiler does not know when you use the Uno setup.