【问题标题】:Creating a counter for C++ with a button使用按钮为 C++ 创建计数器
【发布时间】:2020-02-15 20:40:06
【问题描述】:

我正在尝试将按钮按下计数器与我的氩气设备放在一起。我有几个声音我想在它们之间交替,但无法让计数器为我更改歌曲 A 和 B。我想将歌曲 A 设置为计数器偶数,将歌曲 B 设置为计数器赔率。我目前拥有的很少,也不知道从我所在的地方去哪里。不知道如何上传我目前拥有的。

int button = A0;

int buzzer = A2;

// Defining Sound
int sound1[] = {1700,2500,2800,2000,2500,1500,2000,1800};

int sound2[] = {3800,3600,3400,3200,2400,2600,2800,3000};

//Defining Duration Of Notes

int Duration1[] = {4,2,4,2,4,2,4,2};

int Duration2[] = {2,4,2,4,2,2,4,2};

//Setting Button Count
int bcount = 0;
#define MAX 2

//Creates The Setup Of Code
void setup()
{
    //Creates The Input Of Button Being Pressed
    pinMode (button, INPUT);
    //Creates The Output Of The Buzzer 
    pinMode (buzzer, OUTPUT);
}

void loop() {
    //When Button Is Pressed 
    if (digitalRead(button) == 1) {
        bcount = bcount + 1;
        }
        else if 
                (bcount = 0);
        for (int i = 0; i < bcount; i++) {
            digitalWrite(buzzer, 1);

    if (bcount == MAX) {
        bcount = 0;
        break;
    }
        //Reads Notes
        for (int Note = 0; Note < 8; Note++){
            //Note Duration
            int Duration = 1000/Duration1[Note];
            //Sends Notes To Speaker
            tone(buzzer, sound1[Note], Duration);
            //Sets Delay Between Notes
            int pauseBetweenNotes = Duration * 1.50;
            //Delay Itself
            delay(pauseBetweenNotes);
            //Stop To The Sound
            noTone(buzzer);
        }
    }
        //When Button Is Pressed 2nd Time
      if(digitalRead(button) == 2) {
        //Reads Notes
        for (int Note = 0; Note < 8; Note++){
            //Note Duration
            int Duration = 1000/Duration2[Note];
            //Sends Notes To Speaker
            tone(buzzer, sound2[Note], Duration);
            //Sets Delay Between Notes
            int pauseBetweenNotes = Duration * 1.50;
            //Delay Itself
            delay(pauseBetweenNotes);
            //Stop To The Sound
            noTone(buzzer);
}
}
}

【问题讨论】:

  • 什么是氩气?你是这个意思吗? docs.particle.io/argon如果有,你看到了吗? docs.particle.io/quickstart/argon
  • @Ashham93 你需要具体。如果您可以以其他人可以运行的方式提供您的代码,那将是最有帮助的。
  • @DeducibleSteak 我确实浏览了指南,但找不到我需要的东西。
  • @RichardCritten 我终于想出了如何上传我目前所拥有的东西。我有第一个声音播放,但第二个不会播放它只是继续第一个声音

标签: c++ argon


【解决方案1】:

试试这个:

int counter = 0;
void loop() {
    if (digitalRead(button) == HIGH) {
        counter = counter + 1;
        if (counter % 2 == 1) {
            // ... place your code for sound#1 here.
        } else {
            // ... place your code for sound#2 here.
        }
    }
}

这里发生的事情是:每次按下按钮时,我们都会增加一个计数器。如果按钮被按下的次数是奇数,我们播放第一个声音,如果是偶数,我们播放第二个声音。

将计数器存储在循环函数之外很重要,这样它就不会在循环调用之间丢失它的值。

另外,请注意,如果按下按钮,digitalRead 返回 HIGH,否则返回 LOW。除此以外的任何压机计数都必须由您自己完成。

https://docs.particle.io/reference/device-os/firmware/argon/#digitalread-

【讨论】:

    【解决方案2】:

    感谢@DeducibleSteak 的帮助,代码现在可以在两种声音之间切换。

    int button = A0;
    
    int buzzer = A2;
    
    // Defining Sound
    int sound1[] = {1700,2500,2800,2000,2500,1500,2000,1800};
    
    int sound2[] = {3800,3600,3400,3200,2400,2600,2800,3000};
    
    //Defining Duration Of Notes
    
    int Duration1[] = {4,2,4,2,4,2,4,2};
    
    int Duration2[] = {2,4,2,4,2,2,4,2};
    
    //Setting Button Count
    int bcount = 0;
    #define MAX 2
    
    //Creates The Setup Of Code
    void setup()
    {
        //Creates The Input Of Button Being Pressed
        pinMode (button, INPUT);
        //Creates The Output Of The Buzzer 
       pinMode (buzzer, OUTPUT);
    }
    //Counter Setup
    int counter = 0;
    
    void loop() {
        if (digitalRead(button) == HIGH) {
            ++counter;
    
            if (counter % 2 == 1) 
            {
                for (int Note = 0; Note < 8; Note++){
                //Note Duration
                int Duration = 1000/Duration1[Note];
                //Sends Notes To Speaker
                tone(buzzer, sound1[Note], Duration);
                //Sets Delay Between Notes
                int pauseBetweenNotes = Duration * 1.50;
                //Delay Itself
                delay(pauseBetweenNotes);
                //Stop To The Sound
                noTone(buzzer);
            }
                } 
            else {
                    for (int Note = 0; Note < 8; Note++){
                    //Note Duration
                    int Duration = 1000/Duration2[Note];
                    //Sends Notes To Speaker
                    tone(buzzer, sound2[Note], Duration);
                    //Sets Delay Between Notes
                    int pauseBetweenNotes = Duration * 1.50;
                    //Delay Itself
                    delay(pauseBetweenNotes);
                    //Stop To The Sound
                    noTone(buzzer);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-03
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2014-05-21
      • 2010-12-13
      相关资源
      最近更新 更多