带有 if 语句的 switch case 在 true 时不起作用

switch case with an if statement not working when true

我正在使用内部带有 if 语句的 switch case 来帮助触摸屏触摸

以下代码通过串行终端通过确认触摸坐标返回真值页面在代码开头设置为 0 直到主屏幕加载然后它切换到 1(再次由串行终端确认)但即使我已经确认一切都是真的,它不会 运行 真正的事件(目前只是一个串行打印命令。

int page = 0;     // page number active

这是包含

的代码的顶部

然后在主循环中

     switch (page) {

     // --------------check to see if screen 1 is loaded-----------------

  case '2': {  // Screen 1 Active As when screen 1 loads it chages the value of page to 1
    if ((p.x > 137 && p.x < 210)&& (p.y > 280 && p.y < 320)) 
   Serial.print("option1");       

   else if ((p.x > 80 && p.x < 112)&& (p.y > 280 && p.y < 320)) 
   Serial.print("option1.2");

      else if ((p.x > 45 && p.x < 85)&& (p.y > 280 && p.y < 320)) 
   Serial.print("option1.3");

      else if ((p.x > 5 && p.x < 24)&& (p.y > 280 && p.y < 320)) 
   Serial.print("option1.4");
 break;}

主屏幕在设置后加载并翻到值 1

void mainmenu(){
  page = 1;
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
  }
  Serial.println("OK!");
  tft.fillScreen(BLACK);

  Title();
  Grid();
  Buttons();
  {

按下按钮 1 后从串行终端返回的值

p.x is more than 137
p.x is less than 210
p.y is more than 280
p.x is less than 320
 X 196 & Y 310  Page Value= 1

但仍然没有消息Serial.print("option1")

我已将 if 语句分解为单独的值,并且每个 returns 为真 所以我能想到的都是案例行中的内容,但我不确定是什么

您是否尝试更改此行

case '2': { 

用这个吗?

case 2: { 

也尝试在 case 之后放置一个 println 以确保 switch 语句正常工作

case 2: { 
          Serial.println("Entered in case 2");