【问题标题】:printf("%s",stringName) prints the wrong text but only onceprintf("%s",stringName) 打印错误的文本但只打印一次
【发布时间】:2021-05-17 17:26:24
【问题描述】:

我有一个菜单功能,我在其中输入一个问题和两个选项,然后用户选择一个。它每次都可以正常工作,但只有一个;我打电话

 if (menu("ou est le corps?","interieur ","exterieur")==1)
    {

但不是打印“interieur”而是显示“p?” 没有空间它工作得很好,但我需要腾出一个空间,\n 做同样的事情。 我有另一个调用这个函数, \n 工作正常,所以我不知道为什么这不起作用。有人有想法吗?

PS : choix1 的值然后通过蓝牙发送,并保持不变。

PPS : 如果有不清楚的地方告诉我,我不是天生的英语

PPPS(sorry) : 尝试再次运行相同的代码,似乎打印了一个随机字符,后跟“?”,我有两次“p?”,一次“?”和一次“'?”

[更新]一次“#?”

int menu (String texte, String choix1, String choix2)
{
  envoye = 0;

  rxValue = "0";
  while (digitalRead(M5_BUTTON_HOME) != LOW && rxValue == "0")
  {


    heure();
    M5.Lcd.setTextSize(2);
    M5.Lcd.print(texte);
    M5.Lcd.printf("\n");

    if (selec == 0)
    {
      M5.Lcd.printf("->%s   %s", choix1, choix2);
    }
    else
    {
      M5.Lcd.printf("  %s ->%s", choix1, choix2);
    }
    if (M5.BtnB.read() != 0)
    {
      if (selec == 0)
      {
        selec = 1;
      }
      else
      {
        selec = 0;
      }
      while (M5.BtnB.read() != 0) 
      {
      if(digitalRead(M5_BUTTON_HOME) == LOW)
      {
        M5.Lcd.fillScreen(BLACK);
        delay(1000);
        if(digitalRead(M5_BUTTON_HOME) == LOW)
      {
        choix=50;
        heure();
        delay(1000);
        return 1;
      }
      }
        }

    }

    if (deviceConnected && envoye == 0)
    {

      sendchoix(texte, choix1, choix2);
      envoye++;

    }
  }
  if (rxValue != "0")
  {

    recuble = &rxValue[0];
    selec = atoi(recuble) - 1;


    rxValue = "0";
  }
  M5.Lcd.fillScreen(BLACK);
  delay(300);
  return selec;
}

【问题讨论】:

    标签: arduino


    【解决方案1】:
    int menu (String texte, String choix1, String choix2) {
    [...]
          M5.Lcd.printf("->%s   %s", choix1, choix2);
    

    您不能将String 对象视为const char*,这是格式说明符%s 所期望的。 String 是一个 Arduino 类,用于存储..字符串/字符数据,但该类的对象并不等同于指向数据的原始指针。

    为此,您需要在String 对象上调用c_str() 方法来获取指向数据的C-String 指针,如文档[1] 中所示。

    [..]
          M5.Lcd.printf("->%s   %s", choix1.c_str(), choix2.c_str());
    [..]
    

    [1]https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/c_str/

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2021-01-30
      • 1970-01-01
      • 2020-02-10
      • 2019-11-30
      • 2018-03-27
      相关资源
      最近更新 更多