【问题标题】:Turning on random LEDs from a class打开类中的随机 LED
【发布时间】:2021-08-22 13:53:36
【问题描述】:

对于一个学校项目,我需要以随机选择的颜色随机打开 LED。例如,您可以在红色、蓝色、黄色和绿色之间进行选择。然后,您需要随机选择一种颜色随机位于该特定颜色的 LED。需要打开的 LED 数量是从主文档中输入的,我正在尝试在不同的类中编写这些函数。

我需要包含该颜色的不同 LED 的不同阵列,例如:

int GrLeds[] = {LED_1, LED_5}; //the amount of LEDs can be changed
int ReLeds[] = {LED_2, LED_6};
int BlLeds[] = {LED_3, LED_7};
int GrLeds[] = {LED_4, LED_8);

然后需要随机选择其中一种颜色。我想过将不同的颜色选项放在一个数组中,如下所示:

int randomClr[] = {ledG, ledR, ledB, ledY};

但这样做需要我将 ledG 链接到 GrLeds[] 等。

是否有可能随机选择一个数组,或者会导致相同的结果?我知道 Java 可以选择使用列表,但这在 c++ 中似乎是不可能的。

【问题讨论】:

  • 那么是用户输入的颜色,还是程序随机选择的?
  • @topoly 很抱歉,如果不清楚,颜色需要程序随机选择。

标签: c++ random arduino arduino-c++


【解决方案1】:

您基本上要寻找的是random() 函数,它为您提供了一个介于初始输入数字和最终输入数字之间的随机数。

要将它集成到您​​的代码中,因为您要管理一组以上由多个 LED 集成的 LED,我只需为此创建一个矩阵,然后从该矩阵中选择一个随机行(每行将代表一种颜色),然后打开该行的所有 LED。

您可以使用的一些伪代码:

int randomClr[4][2] = {
                        {LED_1, LED_5},
                        {LED_2, LED_6},
                        {LED_3, LED_7},
                        {LED_4, LED_8}
                       };
 // some code...

// Get a random number from 0 to 3
int randNumber = random(4);
for (int i = 0; i < 2; i++) {
   // Your code to turn on the LEDs, for example:
   digitalWrite(randomClr[randNumber][i], HIGH);
   delay(100);
   digitalWrite(randomClr[randNumber][i], LOW);
}

【讨论】:

    【解决方案2】:

    您的问题有点类似于我前段时间开发的一个应用程序,它也涉及一些 LED 和随机性。

    在将功能迁移到 Arduino 生态系统之前,我编写了以下代码来运行一些测试。

    随意重用我的代码并根据您的需要调整代码。请记住,我编写它是为了使用 Codelite 在 C++17 上进行测试,而不是用于 Arduino 平台,因此您可以将 random 函数替换为 Arduino 中的函数。

    希望对您有所帮助。如果是这样,请表示赞赏,包括代码中此答案的链接,以供后代使用;)

    #include <iostream>
    #include <random>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <time.h>
    
    using namespace std;
    
    #define MAX_COLORS 4
    
    char textOut[100];
    int cycles;
    
    string colorNames[4] = { "RED", "BLUE", "GREEN", "PURPLE" };
    
    typedef enum { RED, BLUE, GREEN, PURPLE } ColorList;
    
    struct ColorsGroup {
        ColorList colorCode;
        string name;
    };
    
    ColorsGroup colorLED[4];
    
    // Methods
    int random(int, int);
    ColorList retrieveColor(int);
    void fillColors(void);
    void printColors(int);
    
    void setup()
    {
    
        fillColors();
    
        cycles = 0;
    }
    
    int main()
    {
        cout << "********** Color picker *********" << endl;
    
        setup();
    
        while(cycles < 10) {
    
        fillColors();
    
        printColors(cycles);
    
        cycles++;
        }
    
        return 0;
    }
    
    // From: https://stackoverflow.com/questions/7560114/random-number-c-in-some-range
    int random(int min, int max)
    {
        static bool first = true;
    
        if(first) {
        srand(time(NULL));
        first = false;
        }
    
        return min + rand() % ((max + 1) - min);
    }
    
    void fillColors(void)
    {
        for(int idx = 0; idx < MAX_COLORS; idx++) {
    
        ColorList newColor = retrieveColor(random(0, MAX_COLORS - 1));
    
        colorLED[idx].colorCode = newColor;
        colorLED[idx].name = colorNames[newColor];
        }
    }
    
    void printColors(int i)
    {
        sprintf(textOut, "%d. colorLED >> ", i);
    
        cout << textOut;
    
        for(int idx = 0; idx < MAX_COLORS; idx++) {
    
        const char* nameStr = colorLED[idx].name.c_str(); // or &colorLED[idx].name[0];
    
        sprintf(textOut, "%s[%d]", nameStr, colorLED[idx].colorCode);
        cout << textOut;
    
        if(idx <= MAX_COLORS - 2) {
            sprintf(textOut, ", ");
            cout << textOut;
        }
        else {
            cout << ";" << endl;
        }
        }
    }
    
    ColorList retrieveColor(int col)
    {
    
        switch(col) {
    
        case 0:
        return RED;
        break;
    
        case 1:
        return BLUE;
        break;
    
        case 2:
        return GREEN;
        break;
    
        case 3:
        return PURPLE;
        break;
    
        default:
        return RED; // for the sake of completeness
        break;
        }
    }
    

    这段代码会吐出以下内容:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 2011-04-15
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      相关资源
      最近更新 更多