Set a randomSeed for better randomness. An unconnected analog input usually works great.
Ok. Where is the reading of your button gone to? And the button press variable?
So I edited it and it doesnt work
Idk guys but something is wrong there. I know what parts mean wich is great but I dont know how to put them together. Can you provide code or atleast give me super detailed description? I know its hard with me and thank you so much for being this patient with me. Now I see giving me code would be much faster but you are trying to teach me something. Thank you.
int timeShowRandom = 2500; //this is how long picking will be
int timeShowDecision = 5000; //this will set up how ling "winners" light will be ON
int timeBlink = 15;
int buttonPin = 3; //Here I want to add 4 other options wich will set up how many "winners" will be choosed
int buttonPress = false;
int randomNumber;
int previousNo = 1;
int timePassed = 0;
// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12};
// 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()
{
// adjus baudrate to needs
Serial.begin(57600);
// safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
if (NUMLEDS > sizeof(ledStates))
{
Serial.println("Can't have more leds ON than there are leds");
// hang forever
for (;;);
}
if (buttonPin == HIGH) {
}
// 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);
}
}
void loop()
{
// put a pieces of paper with LOW written on it in every drawer of the led states
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
byte onCount = 0;
if (buttonPress == true && timePassed <= timeShowRandom)
{
} else
{
// Reset all output pins
for(byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
// I suppose that this is where you want to show a random led pattern
// select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
while (onCount != NUMLEDS)
{
// open a random drawer in ledStates
int randomNumber = random(0, sizeof(ledStates));
// if the paper in there indicated that the led must be off (not on (HIGH))
if (ledStates[randomNumber] != HIGH)
{
// take the LOW paper out and put HIGH paper in
ledStates[randomNumber] = HIGH;
// increment the number of drawers that have a piece of paper with HIGH on it
onCount++;
}
}
// display the leds
// open each drawer in the ledStates drawer set
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
// to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; print a new line
Serial.println();
// wait a bit
delay(5000);
}
That delay(5000) in loop() makes it very unresponsive, as that's where it will be sitting just about all the time. Just remove that line, it has no use other than blocking the thing doing other things.
The below is not how you check a button. It would also be in the wrong place as welll; you should check in loop().
if (buttonPin == HIGH) {
}
Anyway, knowing that you're a bit in a hurry.
I have created a function to display the random leds and that function will be called from within loop().
The code will display the random leds when you release the button; so press it followed by releasing).
I've kept the 2500 delay and there is a short debounce delay.
int timeShowRandom = 2500; //this is how long picking will be
int timeShowDecision = 5000; //this will set up how ling "winners" light will be ON
int timeBlink = 15;
int buttonPin = 3; //Here I want to add 4 other options wich will set up how many "winners" will be choosed
// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12};
// 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()
{
// adjus baudrate to needs
Serial.begin(57600);
// safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
if (NUMLEDS > sizeof(ledStates))
{
Serial.println("Can't have more leds ON than there are leds");
// hang forever
for (;;);
}
// set the button pin to input
pinMode(buttonPin, 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);
}
}
void loop()
{
static int lastButtonState = LOW;
static int buttonState = LOW;
// Check the button
buttonState = digitalRead(buttonPin);
// if the button changed
if (lastButtonState != buttonState)
{
Serial.print("button changed from "); Serial.print(lastButtonState);
Serial.print(" to "); Serial.println(buttonState);
// remember the new state
lastButtonState = buttonState;
// if the button is released
if (buttonState == LOW)
{
// display N random leds
randomLeds();
Serial.print("waiting "); Serial.print(timeShowRandom);
Serial.println(" ms");
delay(timeShowRandom);
Serial.println("waiting done, clearing leds");
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
else
{
}
}
// debounce delay
delay(100);
}
void randomLeds()
{
// put a pieces of paper with LOW written on it in every drawer of the led states
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
byte onCount = 0;
// select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
while (onCount != NUMLEDS)
{
// open a random drawer in ledStates
int randomNumber = random(0, sizeof(ledStates));
// if the paper in there indicated that the led must be off (not on (HIGH))
if (ledStates[randomNumber] != HIGH)
{
// take the LOW paper out and put HIGH paper in
ledStates[randomNumber] = HIGH;
// increment the number of drawers that have a piece of paper with HIGH on it
onCount++;
}
}
// display the leds
// open each drawer in the ledStates drawer set
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
// to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; print a new line
Serial.println();
}
PS
more or less tested
Whaaaaaaaaaat is working. Jesus and I feel like I understand atleast bit more. Thank you. <3 But sadly I see one more problem
Its loop of random but same numbers (or led or drawers). I would like to every time I reset board different light would light up. Do you understand? I dont know how to describe my problem is a loop.
No my 2nd problem. While you are not here Im working on adding modes. I would like to have switchs (5) with numbers. So if I switch 3rd only 3 random LEDs will light up. If I turn that off and switch on 2nd only 2 random LEDs will light up. If 5th all 5 will light up. Or something like that. So I tried to edit that
#define NUMLEDS 5
add some kind of multi if but it doesnt work like that. Wich makes me sad
Can you look at it aswell?
int timeShowRandom = 2500; //this is how long picking will be
int timeShowDecision = 5000; //this will set up how ling "winners" light will be ON
int timeBlink = 15;
int buttonPin = 3; //Here I want to add 4 other options wich will set up how many "winners" will be choosed
int MODE = 5;
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12};
// 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()
{
// adjus baudrate to needs
Serial.begin(57600);
// safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
if (NUMLEDS > sizeof(ledStates))
{
Serial.println("Can't have more leds ON than there are leds");
// hang forever
for (;;);
}
// set the button pin to input
pinMode(buttonPin, 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);
pinMode(MODE, INPUT);
digitalWrite(MODE, HIGH); //Pull-up
// determine region
if (digitalRead(MODE)) {
MODELEDS = 5;
}
else {
MODELEDS = 1;
}
}
}
void loop()
{
static int lastButtonState = LOW;
static int buttonState = LOW;
// Check the button
buttonState = digitalRead(buttonPin);
// if the button changed
if (lastButtonState != buttonState)
{
Serial.print("button changed from "); Serial.print(lastButtonState);
Serial.print(" to "); Serial.println(buttonState);
// remember the new state
lastButtonState = buttonState;
// if the button is released
if (buttonState == LOW)
{
// display N random leds
randomLeds();
Serial.print("waiting "); Serial.print(timeShowRandom);
Serial.println(" ms");
delay(timeShowRandom);
Serial.println("waiting done, clearing leds");
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
else
{
}
}
// debounce delay
delay(100);
}
void randomLeds()
{
// put a pieces of paper with LOW written on it in every drawer of the led states
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
byte onCount = 0;
// select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
while (onCount != NUMLEDS)
{
// open a random drawer in ledStates
int randomNumber = random(0, sizeof(ledStates));
// if the paper in there indicated that the led must be off (not on (HIGH))
if (ledStates[randomNumber] != HIGH)
{
// take the LOW paper out and put HIGH paper in
ledStates[randomNumber] = HIGH;
// increment the number of drawers that have a piece of paper with HIGH on it
onCount++;
}
}
// display the leds
// open each drawer in the ledStates drawer set
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
// to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; print a new line
Serial.println();
}
Mikey999:
Whaaaaaaaaaat is working. Jesus and I feel like I understand atleast bit more. Thank you. <3 But sadly I see one more problemIts loop of random but same numbers (or led or drawers). I would like to every time I reset board different light would light up. Do you understand? I dont know how to describe my problem is a loop.
See #20 on randomseed.
wvmarle:
See #20 on randomseed.
I dont know how to set randomSeed and where ![]()
Did you follow the link and read the instructions? It explains what it is, why it's used, and even has a sample code that shows how/where it's used.
Its not working there is too much of it idk where to put it.
int timeShowRandom = 2500; //this is how long picking will be
int timeShowDecision = 5000; //this will set up how ling "winners" light will be ON
int timeBlink = 15;
int buttonPin = 3; //Here I want to add 4 other options wich will set up how many "winners" will be choosed
int buttonPress = false;
int randomNumber;
int previousNo = 1;
int timePassed = 0;
// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12};
// 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 loop(){
randomNumber = random(300);
Serial.println(randomNumber);
}
void setup()
{
// adjus baudrate to needs
Serial.begin(57600);
// safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
if (NUMLEDS > sizeof(ledStates))
{
Serial.println("Can't have more leds ON than there are leds");
// hang forever
for (;;);
}
// 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);
}
}
void loop()
{
// put a pieces of paper with LOW written on it in every drawer of the led states
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
byte onCount = 0;
if (buttonPress == true && timePassed <= timeShowRandom)
{
} else
{
// Reset all output pins
for(byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
// I suppose that this is where you want to show a random led pattern
// select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
while (onCount != NUMLEDS)
{
// open a random drawer in ledStates
int randomNumber = random(0, sizeof(ledStates));
// if the paper in there indicated that the led must be off (not on (HIGH))
if (ledStates[randomNumber] != HIGH)
{
// take the LOW paper out and put HIGH paper in
ledStates[randomNumber] = HIGH;
// increment the number of drawers that have a piece of paper with HIGH on it
onCount++;
}
}
// display the leds
// open each drawer in the ledStates drawer set
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
// to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; print a new line
Serial.println();
// wait a bit
delay(5000);
}
Come on, seriously. There's an example of about 6 lines of code given in that randomSeed() reference. The one command that's relevant is quite obviously placed in setup().
I saw it and tried to fit it in code but it doesnt work. There is just so many praces I can try to fit it in but non of them work.



Please keep your pictures, far too much work to read them. Code is text so you can post text.
Referring to the code in reply #30
You can only have one setup() and one loop().
wvmarle has been talking about randomSeed(), not about random(). In the example, that function is only used once. Where? Indeed, in setup(). So add that as the first line in the setup() that I presented to you.
So I used analog pin for generating random numbers out of noise. Here is code and its working. Now I need to add those Mode buttons. I need 5 of them to edit #define NUMLEDS X. So when I switch one of 5 switch his settings will set up. So on 1st X will be 1 on 2nd X=2, 3rd X=3 etc...
int timeShowRandom = 10000; //this is how long picking will be
int buttonPin = 3; //Here I want to add 4 other options wich will set up how many "winners" will be choosed
// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// 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);
// safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
if (NUMLEDS > sizeof(ledStates))
{
Serial.println("Nemůže svítit víc LEDs než tam je!");
// hang forever
for (;;);
}
// set the button pin to input
pinMode(buttonPin, 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);
}
}
void loop()
{
static int lastButtonState = LOW;
static int buttonState = LOW;
// Check the button
buttonState = digitalRead(buttonPin);
// if the button changed
if (lastButtonState != buttonState)
{
Serial.print(" Poloha tlačítka se změnila z "); Serial.print(lastButtonState);
Serial.print(" na "); Serial.println(buttonState);
// remember the new state
lastButtonState = buttonState;
// if the button is released
if (buttonState == LOW)
{
// display N random leds
randomLeds();
Serial.print("načítám "); Serial.print(timeShowRandom);
Serial.println(" ms");
delay(timeShowRandom);
Serial.println("načítání dokončeno, vypínám LEDs ");
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
else
{
}
}
// debounce delay
delay(100);
}
void randomLeds()
{
// put a pieces of paper with LOW written on it in every drawer of the led states
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
byte onCount = 0;
// select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
while (onCount != NUMLEDS)
{
// open a random drawer in ledStates
int randomNumber = random(0, sizeof(ledStates));
// if the paper in there indicated that the led must be off (not on (HIGH))
if (ledStates[randomNumber] != HIGH)
{
// take the LOW paper out and put HIGH paper in
ledStates[randomNumber] = HIGH;
// increment the number of drawers that have a piece of paper with HIGH on it
onCount++;
}
}
// display the leds
// open each drawer in the ledStates drawer set
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
// to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; print a new line
Serial.println();
}
Can you help me with that. Its probaly last task to do.
So I think we will use something like a multi overkill IF for 5 scenarios is that right? So what to do???
Maybe adding just more triggers would be better.
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.
// 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);
}
}
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;
}
}
}
// NEW/CHANGED: if a random number of leds was indicated (not zero)
if (numLeds != 0)
{
// display N random leds
randomLeds();
Serial.print("načítám "); Serial.print(timeShowRandom);
Serial.println(" ms");
delay(timeShowRandom);
Serial.println("načítání dokončeno, vypínám LEDs ");
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
// debounce delay
delay(100);
}
void randomLeds()
{
// put a pieces of paper with LOW written on it in every drawer of the led states
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
byte onCount = 0;
// CHANGED: use numLeds instead of NUMLEDS
// select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
while (onCount != numLeds)
{
// open a random drawer in ledStates
int randomNumber = random(0, sizeof(ledStates));
// if the paper in there indicated that the led must be off (not on (HIGH))
if (ledStates[randomNumber] != HIGH)
{
// take the LOW paper out and put HIGH paper in
ledStates[randomNumber] = HIGH;
// increment the number of drawers that have a piece of paper with HIGH on it
onCount++;
}
}
// display the leds
// open each drawer in the ledStates drawer set
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
// to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; print a new line
Serial.println();
}
sterretje:
// 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);
}
}
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;
}
}
}
// NEW/CHANGED: if a random number of leds was indicated (not zero)
if (numLeds != 0)
{
// display N random leds
randomLeds();
Serial.print("načítám "); Serial.print(timeShowRandom);
Serial.println(" ms");
delay(timeShowRandom);
Serial.println("načítání dokončeno, vypínám LEDs ");
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
// debounce delay
delay(100);
}
void randomLeds()
{
// put a pieces of paper with LOW written on it in every drawer of the led states
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
byte onCount = 0;
// CHANGED: use numLeds instead of NUMLEDS
// select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
while (onCount != numLeds)
{
// open a random drawer in ledStates
int randomNumber = random(0, sizeof(ledStates));
// if the paper in there indicated that the led must be off (not on (HIGH))
if (ledStates[randomNumber] != HIGH)
{
// take the LOW paper out and put HIGH paper in
ledStates[randomNumber] = HIGH;
// increment the number of drawers that have a piece of paper with HIGH on it
onCount++;
}
}
// display the leds
// open each drawer in the ledStates drawer set
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
// to the value on the piece of paper in the corresponding drawer (indicated by ledCnt) in the ledStates drawer set
digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; print a new line
Serial.println();
}
Its just what I wanted. But there is a loop whenever I click on any button program starts and never ends
numLeds was not set back to zero at the beginning of loop(); sorry for that. I also left the lastButtonState in by accident.
Replace your current loop() by
void loop()
{
// reset numLeds
numLeds = 0;
// variable for reading state of button
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(buttons[btnCnt].lastButtonState);
Serial.print(" na "); Serial.println(buttonState);
// if the button was released
if (buttonState == LOW)
{
numLeds = buttons[btnCnt].numRandom;
}
}
}
// NEW/CHANGED: if a random number of leds was indicated (not zero)
if (numLeds != 0)
{
// display N random leds
randomLeds();
Serial.print("načítám "); Serial.print(timeShowRandom);
Serial.println(" ms");
delay(timeShowRandom);
Serial.println("načítání dokončeno, vypínám LEDs ");
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
digitalWrite(ledPins[ledCnt], LOW);
}
}
// debounce delay
delay(100);
}