0

For a school project I need to randomly turn on LEDs in a color that is randomly chosen. So for example you can choose between red, blue, yellow and green. Then you need to pick one color randomly and randomly situated LEDs of that specific color on. The amount of LEDs that need to be turned on is input from the main document, I am trying to write these functions in a different class.

I need different arrays that contain the different LEDs of that color like:

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);

Then one of these colors needs to be chosen randomly. I thought about putting the different color option in an array like the following:

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

But doing it like this would require me to link the ledG to GrLeds[] etc.

Is there a possibility to choose one of the arrays randomly, or something what would result in the same? I know Java has the option to use a list but that does not seem to be possible within c++.

Seth
  • 11

2 Answers2

0

What you are basically looking for is the random() function, which gives you a random number between an initial and final input numbers.

To integrate it within your code, as you are gonna manage more than one set of LEDs which is integrated by multiple LEDs, I would just create a matrix for that, and then choose a random row from that matrix (each row will represent a color), and turn on all the LEDs from that row.

Some pseudo-code that you can work with:

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);
}
Shunya
  • 2,344
  • 4
  • 16
  • 28
0

Your problem is kinda similar to an application that I developed some time ago which also involved some LEDs and randomness.

I wrote the following code for running some tests before migrating the functionalities to the Arduino ecosystem.

Feel free to reuse and adapt my code to your needs. Keep in mind that I wrote it to be tested on C++17 using Codelite and not for the Arduino platform, therefore you can replace the random function with the one from Arduino.

Hope it helps. If so, just show a bit of appreciation including the link to this answer in your code, for posterity ;)

#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;
    }
}

And this code spits out the following:

enter image description here