c - 如何在 switch 语句中连续打印输出?

标签 c user-interface arduino switch-statement

我一直在尝试在 switch 语句条件内连续打印引脚 3 的 PWM 输出,但它只打印一次。我可以在串行监视器中连续打印它直到它满足第二个条件吗?或者使用 while 循环?还是一个 if else?

这是我的代码我也有一个具有类似功能的代码但是它使用if else但仍然只打印一次

void loop() {
    // if there's any serial available, read it:
    while (Serial.available() > 0) {
        int InitVal = Serial.parseInt();
        int red = Serial.parseInt();

        switch(InitVal) {
            case 1:
                if (Serial.read() == '\n') {

                    analogWrite(redPin, red);
                    Serial.println(red);
                    Serial.write(red);

                }
                break;
            case 0:
                analogWrite(redPin, 0);
                Serial.println(0);
                Serial.write(0);
                break;
        }
    }
}

我计划将其与 GUI 相结合。 GUI 将 ascii 发送到 arduino 读取它,然后将输出值发送到 GUI。 例子

1.GUI 发送 [1,123] : 1 = switch 语句的触发点; 123 = PWM 值。

  1. Arduino 接收指令并打印出 pwm 值
  2. GUI接收pwm值并显示

修改后的代码:卡在最后一个 while 循环中,也许我可以在 arduino 中使用线程函数,以便满足/不满足最后一个 while 循环?

void loop() {

  int InitVal = 0;
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
    int InitVal = Serial.parseInt();
    int red = Serial.parseInt();

    switch(InitVal) {
      case 1:
        if (Serial.read() == '\n') {
           InitVal = 1;
          //analogWrite(redPin, red);
          //Serial.println(red);
         // Serial.write(red);
       }
        break;
      case 0:
        InitVal = 0;
        //analogWrite(redPin, 0);
        //Serial.println(0);
        //Serial.write(0);
        break;
       }

      if (InitVal) /* when enabled, blink leds */ {
        delay(20);
        while (InitVal == 1) /* loop forever */{

          Serial.println(red);
          Serial.write(red);
          delay(20);
        }

    }


    }
  } 

最佳答案

我放弃了 Serial.parseInt() 函数,删除了 switch 语句,并在按照本教程 http://forum.arduino.cc/index.php?topic=396450.0 时遵循@Arno Bozo 关于串行监听的建议。 我想出了我想要的,这是代码

const int redPin = 3;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
boolean newData = false;

int InitVal = 0; // change to init value or red
int red = 0;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(redPin, OUTPUT);

}

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        One();
        newData = false;
    }
    else {
      Zero();

    }
}


 ///////////////////// ///////////////////// /////////////////////
void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

 ///////////////////// ///////////////////// /////////////////////

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    InitVal = atoi(strtokIndx); // copy it to messageFromPC

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    red = atoi(strtokIndx);     // convert this part to an integer

}


 ///////////////////// ///////////////////// /////////////////////
void One() {

  if (InitVal == 0){

    delay(20);
    Serial.println(0);
    delay(20);
  }      
 }
 ///////////////////// ///////////////////// /////////////////////
void Zero() {

  if (InitVal == 1){

    delay(20);
    Serial.println(red);
    delay(20);
  }      
 }

总而言之,代码是这样工作的

1.在串行监视器中发送此 <1,123>:1 = switch 语句的触发点; 123 = PWM 值。

  1. Arduino 接收指令并打印出 pwm 值
  2. 如果您发送 <0,123> 它会打印一次零

关于c - 如何在 switch 语句中连续打印输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396304/

相关文章:

c++ - Arduino:将 uint64_t 转换为字符串

c - 我怎样才能把我的文字涂成橙色?

c - 多线程编程 C.互斥量不起作用

对 C 中使用指针的宏感到困惑

c - 设备重启时USB串口垃圾

代码没有显示任何 println arduino? (在每一种语言上)

c - 系统调用在 Linux 上的哪个库中?这个库如何链接到包含系统调用的可执行目标文件?

wpf - 使用 WPF 内嵌文本框标签

java - Swing 图形用户界面 : JList with jtextFields on the rows

java - 是否可以将 JCheckBox 放入不包含 boolean 值的 JTable 单元格中?