Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 阿杜伊诺。计算按下按钮的次数,然后播放顺序_Loops_Button_Arduino_Counter - Fatal编程技术网

Loops 阿杜伊诺。计算按下按钮的次数,然后播放顺序

Loops 阿杜伊诺。计算按下按钮的次数,然后播放顺序,loops,button,arduino,counter,Loops,Button,Arduino,Counter,帮我编写这个程序 当我按下按钮一次时,序列s1()将播放 当我按两次按钮时,序列s2()将播放 当我三次按下按钮时,序列s3()将播放 当我按下按钮4次时,序列s4()将播放 //代码 常量int buttonPin=10; int buttonState=0; 无效设置(){ Serial.begin(9600); //将引脚2-9初始化为输出 对于(int i=2;i你是指类似的东西吗?我使用了内部上拉和按钮作为有源低电平开关,所以我使用了与低电平比较: const int btn

帮我编写这个程序

  • 当我按下按钮一次时,序列s1()将播放
  • 当我按两次按钮时,序列s2()将播放
  • 当我三次按下按钮时,序列s3()将播放
  • 当我按下按钮4次时,序列s4()将播放

  • //代码
    常量int buttonPin=10;
    int buttonState=0;
    无效设置(){
    Serial.begin(9600);
    //将引脚2-9初始化为输出
    
    对于(int i=2;i你是指类似的东西吗?我使用了内部上拉和按钮作为有源低电平开关,所以我使用了与低电平比较:

    const int    btn = 4;
    byte  btn_tim_on = 0;
    byte btn_tim_off = 0;
    byte     btn_val = 0;
    
    void setup() {
      pinMode(btn, INPUT_PULLUP);
      Serial.begin(57600); // debug purposes
    }
    
    void loop() {
    
      if (digitalRead(btn) == LOW) {
        if (++btn_tim_on == 5) {    // 5 checks before it's considered as key press and every 256*4ms repeat
          ++btn_val;
        }
        btn_tim_off = 80;           // about 0.32 second to confirm
      } else {
        btn_tim_on = 0;             // reset button pressed timer
        if (btn_tim_off > 0) {
          if (--btn_tim_off == 1) { // call action
            Serial.println(btn_val); // print in serial monitor
            switch (btn_val) {
              case 1: s1(); break;
              case 2: s2(); break;
              case 3: s3(); break;
              case 4: s4(); break;
              default: Serial.println(F("no action")); break;
            }
          }
        } else {
          btn_val = 0;              // reset button press counter
        }
      }
      delay(4);
    }
    
    // where to place s1, s2, s3 and s4 ...
    // playing these actions is blocking, so no keys are detected during "play"
    

    你能更好地解释你的问题吗?只是一个提示,你可能会在那里得到更好/更快的帮助…这部分论坛主要用于编程相关问题。因此,请在提问之前阅读,因为这些将帮助你从社区获得更多更好的答案。谢谢!当循环结束时,我按下按钮有没有办法打破循环她在跑步吗?
    const int    btn = 4;
    byte  btn_tim_on = 0;
    byte btn_tim_off = 0;
    byte     btn_val = 0;
    
    void setup() {
      pinMode(btn, INPUT_PULLUP);
      Serial.begin(57600); // debug purposes
    }
    
    void loop() {
    
      if (digitalRead(btn) == LOW) {
        if (++btn_tim_on == 5) {    // 5 checks before it's considered as key press and every 256*4ms repeat
          ++btn_val;
        }
        btn_tim_off = 80;           // about 0.32 second to confirm
      } else {
        btn_tim_on = 0;             // reset button pressed timer
        if (btn_tim_off > 0) {
          if (--btn_tim_off == 1) { // call action
            Serial.println(btn_val); // print in serial monitor
            switch (btn_val) {
              case 1: s1(); break;
              case 2: s2(); break;
              case 3: s3(); break;
              case 4: s4(); break;
              default: Serial.println(F("no action")); break;
            }
          }
        } else {
          btn_val = 0;              // reset button press counter
        }
      }
      delay(4);
    }
    
    // where to place s1, s2, s3 and s4 ...
    // playing these actions is blocking, so no keys are detected during "play"