You can not use #define for what you want to do.
You have 5 buttons / switches in total; each of them starts the 'draw' and each of them will give you a different random number?
If so, you will need an array for 5 button pins and you need to remember the last state of all of them in another array. We're however not going to use two arrays for that, so welcome to the world of structs and classes. A struct (or class, I use structs) is like a record in e.g. a phone book; it combines related information There is a name and phone number, maybe even an address etc.
The below struct can be used
// struct with relevant information for a button
struct BUTTON
{
byte pin; // the pin that the button is connected to
int lastButtonState; // the last state
byte numRandom; // the number of random leds.
};
// array of five buttons
BUTTON buttons[] =
{
{A0, LOW, 1},
{A1, LOW, 2},
{A2, LOW, 3},
{A3, LOW, 4},
{A4, LOW, 5},
};
So now we have an array with 5 buttons. We know to which pin a button is connected (I've used A0..A5, change to what suites you), the initial last button state is low and for each button we indicate how many random leds; you can change the latter to e.g. 1, 3, 5, 7, 8 if needed. The value 0 for numRandom is reserved.
Now to access the 'members' in a variable of type BUTTON, we need to use a dot. And as this is an array of buttons, we use a for-loop to access each button (same as with the leds). NUMLEDS will no longer exist as it's no a variable and we have to use a variable for this. I've added a variable numLeds. And we can't implement a safety check in setup() as we don't know (in setup())what the number will be.
So the first part of your code will now look like
// CHANGED
unsigned int timeShowRandom = 10000; //this is how long picking will be <<==== better to make this an unsigned variable
// NEW: struct with relevant information for a button
struct BUTTON
{
byte pin; // the pin that the button is connected to
int lastButtonState; // the last state
byte numRandom; // the number of random leds.
};
// NEW: array of (five) buttons
BUTTON buttons[] =
{
{A0, LOW, 1},
{A1, LOW, 2},
{A2, LOW, 3},
{A3, LOW, 4},
{A4, LOW, 5},
};
// NEW: the number of random leds to be shown
byte numLeds = 0;
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46};
// set of drawers (array) holding the state of each led for a cycle
// the size (number of drawers) is automatically calculated at compile time based on number of drawers in the ledPins drawer set
byte ledStates[sizeof(ledPins)];
void setup()
{
randomSeed( analogRead (A7) );
// adjus baudrate to needs
Serial.begin(57600);
// set all button pins to input
for (byte btnCnt = 0; btnCnt < sizeof(buttons) / sizeof(buttons[0]); btnCnt++)
{
pinMode(buttons[btnCnt].pin, INPUT);
}
// open each drawer in the set of drawers, read the pin number
// ledPins is the drawer set, ledCnt is the number of the drawer
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
// set the pin, found by reading the paper in drawer ledCnt, LOW
digitalWrite(ledPins[ledCnt], LOW);
// set the pin, found by reading the paper in drawer ledCnt, to be an output
pinMode(ledPins[ledCnt], OUTPUT);
}
}
Now we can modify loop to read each of the buttons, check if it was released and if released set the number of leds that needs to be randomly selected.
We implement that in the beginning of loop().
void loop()
{
static int lastButtonState = LOW;
int buttonState;
// Check the buttons
for (byte btnCnt = 0; btnCnt < sizeof(buttons) / sizeof(buttons[0]); btnCnt++)
{
buttonState = digitalRead(buttons[btnCnt].pin);
// if the button changed
if (buttons[btnCnt].lastButtonState != buttonState)
{
// remember the new state for this button
buttons[btnCnt].lastButtonState = buttonState;
// debug
Serial.print(" Poloha tlačítka se změnila z "); Serial.print(lastButtonState);
Serial.print(" na "); Serial.println(buttonState);
// if the button was released
if (buttonState == LOW)
{
numLeds = buttons[btnCnt].numRandom;
}
}
}
// more to follow here
...
...
}
Instead of checking button states to decide if a button was released, we now check the value of numLeds; if zero, no button was released.
// NEW/CHANGED: if a random number of leds was indicated (not zero)
if (numLeds != 0)
{
// display N random leds
randomLeds();
...
...
Next you should make one change in the randomLeds() function as NUMLEDS no longer exists; use numLeds instead.
It compiles, should work.
Full code in next post. Now try to learn from this during the holiday. If there is something that you don't understand, ask (even if it's after your holiday).
You have posted this in the section for 'paid work' and it has significantly grown out-of-hand.