not seeing all serial.print, possible I don't understand switch/case

so I have a fairly large program at this point and many of the chunks are now functional. I am directly using timers to create a pulse train for a stepper motor driver. Now to keep my frequency resolution good, I need to use different prescalers for different ranges of speed. (I could use interrupts and not have to play with this, but I don’t like using interrupts if I don’t have to, and I still have pins to spare, plust I want to get a good understanding of this technique).
Now that I have added this code to check my input range and change the prescaler based on some hardcoded crossover values, I am having a very strange issue. when the setPrescaler() is called, I see the serial data from inside the switch/case tree, but none of the prints from outside the switch/case. It also seems to not set the prescaler, which is bizzare since I see the print statement which comes after setting the prescaler bits. I was all excited to realize the break within the default statement was not supposed to be there and commented it out, but still no dice. I am going to paste all relevant functions, let me know if I miss any you need. Also liqscaler and liqPrescaler are two different vars as a dumb legacy thing, nothing more. liqPrescaler is a global, I will clean that up once I get the code working, rather than introduce more issues during a “simple cleanup” operation :lol:

note: while I have commands for controlling a LCD screen, I am doing my debugging using cutecom on my pc
the function liqPumpControl() is called from my kepyad event, which just collects numerical inputs until I hit ‘D’ and passes them to liqPumpControl, it works fine, the value printed of “mL” is always exactly as expected.
ALSO all the “volatile” was thrown in by me as a shotgun approach, I have no reason to think it would fix it or was the real issue, but when you are stabbing in the dark, might as well use every blade you find :~

void setup(){
  motorInitialize();            //sets dir pins
  setTimerMode();               //sets CTC and toggle on match  
  setPrescalers(liqPrescaler, airPrescaler);          //sets prescaler(liq, air), sets toggle & output
  Serial.begin(19200);
  Serial.print('A');
  delay(100);
  LCDOn();
  LCDClear();  
  //initializeSensors();
  kpd.addEventListener(keyEvent); //add an event listener for this keypad
  LCDBLOn();
  LCDClear();
  Serial.print("Setup done");
  delay(300);
  //airPumpControl(0);
  //liqPumpControl(0);
}

void loop(){
  char key = kpd.getKey();
  //LCDClear();
 }


void liqPumpControl(unsigned int mL){
  Serial.print("ml = ");
  Serial.println(mL);
  Serial.print("liqPrescaler = ");
  Serial.println(liqPrescaler);
  
  //check for desired prescaler
  if (mL<120 && liqPrescaler != 1024) {
    setPrescalers (1024, 666);
    //Serial.write(0x94);
    Serial.println("ml<120");
  }
  else if (mL>= 120 && mL<=490 && liqPrescaler != 256) {
    setPrescalers (256, 666);
    //Serial.write(0x94);
    Serial.println("ml<490");
  } 

  //guts version takes ml and less consts
  volatile unsigned long k1=FLUID_K0/(long)liqPrescaler;
  //Serial.write(0x80);
  Serial.print(k1);
  Serial.println(" = k1 ");

  OCR1A = k1/mL;
  Serial.println(OCR1A);

  //delay(5000);
}

void setPrescalers(int liqscaler, int airscaler) {

  switch(liqscaler) {       //sets prescaler or turns off output and timer
    case 0:
      cbi(TCCR1A, COM1A1);
      cbi(TCCR1A, COM1A0);
      cbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      cbi(TCCR1B, CS10);
      Serial.print("0 off liq");
      break;
    case 1:
      cbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      sbi(TCCR1B, CS10);
      //Serial.write(0xA0);
      Serial.println(liqscaler);
      break;
    case 8:
      cbi(TCCR1B, CS12);
      sbi(TCCR1B, CS11);
      cbi(TCCR1B, CS10); 
      //Serial.write(0xA0);
      Serial.println(liqscaler);
      break;
    case 64:
      cbi(TCCR1B, CS12);
      sbi(TCCR1B, CS11);
      sbi(TCCR1B, CS10); 
      //Serial.write(0xA0);
      Serial.println(liqscaler);
      break;
    case 256:
      sbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      cbi(TCCR1B, CS10);
      //Serial.write(0xA0);
      Serial.print("case 256 = ");
      Serial.println(liqscaler);
      break;
    case 1024:
      sbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      sbi(TCCR1B, CS10);
      //Serial.write(0xA0);
      Serial.print("case 1024 = ");
      Serial.println(liqscaler);
      break; 
    default:
      Serial.println("made it default??");
      //break;
  }
  if (liqscaler){           //if a number was set, sets pump pin to output and toggle on match
    sbi(TCCR1A, COM1A0);            //sets oc1A to toggle on match
    pinMode(LIQUIDPUMP, OUTPUT);       //TIMER REquires pin to be output
  }

  switch(airscaler) {       //sets prescaler or turns off timer given valid number
    case 0:                                              //disconnect pin
      cbi(TCCR2A, COM2A1);
      cbi(TCCR2A, COM2A0);
      break;
    case 1: 
      cbi(TCCR2B, CS22);
      cbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 8: 
      cbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      cbi(TCCR2B, CS20); 
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 32: 
      cbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20); 
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 64: 
      sbi(TCCR2B, CS22);
      cbi(TCCR2B, CS21);
      cbi(TCCR2B, CS20); 
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 128:
      sbi(TCCR2B, CS22);
      cbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 256:
      sbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      cbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 1024:
      sbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    default:
      return;
  }
  if (airscaler){           //if a number was set, sets pump pin to output and toggle on match
    sbi(TCCR2A, COM2A0);   //sets to toggle on match
    pinMode(AIRPUMP, OUTPUT);      //TIMER REquires pin to be output
  }
  if (liqscaler != 666) {liqPrescaler = liqscaler; Serial.print("set global var");}
  else Serial.println("don't set global var");
  if (airscaler != 666) airPrescaler = airscaler;
  Serial.print("liqscaler = ");
  Serial.println(liqscaler);
  Serial.print("liqPrescaler = ");
  Serial.println(liqPrescaler);

}

oh forgot thats not a real function, so I have this at the top of my sketch. Do you call cbi() a macro? I am still figuring out the lingo here.

 #ifndef cbi
  #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  #endif
  #ifndef sbi
  #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  #endif

and to help my sanity, i changed the value of the liqPrescaler global varial when I initially declare it, and the code recognizes that fine!, ie the setPrescaler() works when I pass it the starting values in setup, but does not seem to work later, i still blame the swtich/case, but I don't know why

Rather than post extracts of a sketch which you think contain all the code relevant to the problem, it is better to create a minimal sketch which demonstrates the problem with all extraneous code removed.

Nothing is jumping out from that code as a source for the problem, but then I haven't looked at what all those hardware register accesses are doing.

I kinda did that, as in the function for setting prescalers works fine. Somehow it no longer works, and I don't konw if it has something to do with how I am calling it or what. there is a very minimal chance it wasn't working as well as I thought it was previously, before the program got more and more complicated.

However my bigest question is how can this function be called, and not 'make' it to the serial calls at the end?

when called w/ setPrescalers(256, 666) I get the debug message from inside the switch/case, but not hte messages at the end, I added comments to make it clear

void setPrescalers(int liqscaler, int airscaler) {

  switch(liqscaler) {       //sets prescaler or turns off output and timer
    case 0:
      cbi(TCCR1A, COM1A1);
      cbi(TCCR1A, COM1A0);
      cbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      cbi(TCCR1B, CS10);
      Serial.print("0 off liq");
      break;
    case 1:
      cbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      sbi(TCCR1B, CS10);
      //Serial.write(0xA0);
      Serial.println(liqscaler);
      break;
    case 8:
      cbi(TCCR1B, CS12);
      sbi(TCCR1B, CS11);
      cbi(TCCR1B, CS10); 
      //Serial.write(0xA0);
      Serial.println(liqscaler);
      break;
    case 64:
      cbi(TCCR1B, CS12);
      sbi(TCCR1B, CS11);
      sbi(TCCR1B, CS10); 
      //Serial.write(0xA0);
      Serial.println(liqscaler);
      break;
    case 256:
      sbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      cbi(TCCR1B, CS10);
      //Serial.write(0xA0);
      Serial.print("case 256 = ");                                   //THIS DOES PRINT
      Serial.println(liqscaler);
      break;
    case 1024:
      sbi(TCCR1B, CS12);
      cbi(TCCR1B, CS11);
      sbi(TCCR1B, CS10);
      //Serial.write(0xA0);
      Serial.print("case 1024 = ");
      Serial.println(liqscaler);
      break; 
    default:
      Serial.println("made it default??");
      //break;
  }
  if (liqscaler){           //if a number was set, sets pump pin to output and toggle on match
    sbi(TCCR1A, COM1A0);            //sets oc1A to toggle on match
    pinMode(LIQUIDPUMP, OUTPUT);       //TIMER REquires pin to be output
  }

  switch(airscaler) {       //sets prescaler or turns off timer given valid number
    case 0:                                              //disconnect pin
      cbi(TCCR2A, COM2A1);
      cbi(TCCR2A, COM2A0);
      break;
    case 1: 
      cbi(TCCR2B, CS22);
      cbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 8: 
      cbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      cbi(TCCR2B, CS20); 
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 32: 
      cbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20); 
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 64: 
      sbi(TCCR2B, CS22);
      cbi(TCCR2B, CS21);
      cbi(TCCR2B, CS20); 
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 128:
      sbi(TCCR2B, CS22);
      cbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 256:
      sbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      cbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    case 1024:
      sbi(TCCR2B, CS22);
      sbi(TCCR2B, CS21);
      sbi(TCCR2B, CS20);
      Serial.write(0x8C);
      Serial.print(airscaler);
      break;
    default:
      return;
  }
  if (airscaler){           //if a number was set, sets pump pin to output and toggle on match
    sbi(TCCR2A, COM2A0);   //sets to toggle on match
    pinMode(AIRPUMP, OUTPUT);      //TIMER REquires pin to be output
  }
  if (liqscaler != 666) {
    liqPrescaler = liqscaler;
    Serial.print("set global var");
  }
  else Serial.println("don't set global var");
  if (airscaler != 666) airPrescaler = airscaler;
  Serial.print("liqscaler = ");   //THESE DO NOT PRINT
  Serial.println(liqscaler);
  Serial.print("liqPrescaler = ");
  Serial.println(liqPrescaler);

}

When you call the function with parameters of 256, 666 the airscaler variable has a value of 666. You have no case statement for that value of airscaler so the routine falls through to the default case which executes the return statement and the function ends immediately.

break, return, its all the same :roll_eyes: oops. I'll pull out the defaults, originally they were gonna handle non std input, but I decided to just ignore bad data and made them an issue instead of just delete them. lesson learned.

now i have a missing paren or ; somewhere in that dang file, it won't compile.. ARGH

found it, deleted all the contents of the default statement, so it was empty aside from the commented out break. whoops, DELETE