Go Down

Topic: Can somebody help me with code to Lottery machine (only LEDs) (Read 1 time) previous topic - next topic

Mikey999

Its not working there is too much of it idk where to put it.


 
Code: [Select]

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


  }



wvmarle

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().


Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Mikey999

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.





sterretje

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Mikey999

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...

Code: [Select]


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???

Mikey999


sterretje

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
Code: [Select]
// 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
Code: [Select]
// 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().
Code: [Select]

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.
Code: [Select]
// 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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

sterretje

Code: [Select]
// 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();
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Mikey999

Code: [Select]
// 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();
}

Its just what I wanted. But there is a loop whenever I click on any button program starts and never ends

sterretje

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
Code: [Select]
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);
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Mikey999

Code: [Select]
// CHANGED
unsigned int timeShowRandom = 1000;      //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()
{
  // 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();                        // <<<<<<<<<<<<<<<This is bad :( (exit status 1
'randomLeds' was not declared in this scope)[/color]
    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);
}


    // display N random leds
    randomLeds();                      // <<<<<<<<<<<<<<<This is bad :( (exit status 1

So I replaced it but still code is bad. I dont want to mess it up. I tried basic things nothing works for me so can you take a look at that one more time please? Thank you<3

sterretje

Where is the randomLeds() function? It was at the end of the code in reply #37 but it's not in your code in reply #40.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Mikey999

Thank you for cooperation guys. I will analyze it hopefully learn something new so I could help others like you. Can I text you if I find something that doesnt work of I dont understand?

sterretje

Just continue in this thread. It's the easiest as all the information is here.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up