Go Down

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

wvmarle

I just dont understand wich part of that code of wvwarle should I add in? I tried to replace all of my 7 pinoutputs to ˘˘

Which part of my code you don't understand?
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.

sterretje

I just dont understand wich part of that code of wvwarle should I add in? I tried to replace all of my 7 pinoutputs to ˘˘
I did not say to copy wvmarle's code in; I asked to compare what you created in your last attempt with what wvmarle posted in reply #2.

So let's try to start from the beginning. An array is like a set of drawers.

1)
You have 40 drawers for led pins. Put a sticker on the holding enclosure of the set of drawers with the name ledPins.
2)
Put a piece of paper with the number of the led pin in each of the drawers.

3)
In code (for 9 drawers)
Code: [Select]
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};


4)
Next you want to set those led pins to output, so you open the first drawer (computers start counting from 0 so the first drawer is number 0). Read the pin number from the piece of paper and set that pin to output.

In code
Code: [Select]
pinMode(ledPins[0], OUTPUT);


5)
Close the drawer (no code) and open the second drawer (drawer numbered 1), read the pin number from the piece of paper and set that pin to output.
In code
Code: [Select]
pinMode(ledPins[1], OUTPUT);


6)
You can do that for all 40 drawers manually. You can also use a for-loop telling the Arduino to open all drawers in sequence; that for-loop will start at 0 and end at 39. The for-loop uses a variable to keep track of which drawer to open to read the pin number.

In code (the variable is called ledCnt in my code, i in wvmarle's code)
Code: [Select]
// drawer set with led pin numbers; add more pin numbers separated by commas if needed
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};

void setup()
{
  // open each drawer of the 40, read the pin number and set the pin with that pin number to output
  // remember that the Arduino counts from 0 so 0 is the first drawerr and 39 is the last drawer.
  // ledPins is the set of drawers, ledCnt is the number of the drawer to be opened
  for (byte ledCnt = 0; ledCnt < 40; ledCnt++)
  {
    // set the pin, found by reading the paper in drawer ledCnt, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}


7)
Now there is a slight problem; we only have 9 out of 40 pins defined for the leds, not all 40. You can use the sizeof operator to determine the size (in bytes) of the array. Because the array contains bytes, the number will be the number of elements in the array.

To demonstrate, the below code
Code: [Select]
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};

void setup()
{
  // serial setup for debugging
  Serial.begin(57600);

  // debug: print the number of drawers in the set of drawers
  Serial.print("Number of led pins = "); Serial.println(sizeof(ledPins));

  // open each drawer in the set of drawers, read the pin number and set the pin with that pin number to output
  // 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, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}


8)
You also want to guarantee that all leds will be off when the program starts. For that you use digitalWrite. The code that I presented does this before you set the pin to output to prevent possible accidental flashes.

In code
Code: [Select]
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};

void setup()
{
  // serial setup for debugging
  Serial.begin(57600);

  // debug: print the number of drawers in the set of drawers
  Serial.print("Number of led pins = "); Serial.println(sizeof(ledPins));

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

Do you now understand how that part works? And what you're supposed to do?

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

1)
Now you create a second set of drawers that will hold the state of the 40 leds. Because the above example only has 9 and we don't want to modify the code in tens (or hundreds) of places every time that we add a led, we again use the sizeof operator
Code: [Select]
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};
// 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)];


2)
In setup, we will build a safety check so the code does not crash if the number of random leds that needs to be on. First we add the number of leds that can be on at the top of the code.
Code: [Select]
// number of leds that need to be ON in every cycle
#define NUMLEDS 5


3)
And we add the check (complete code)
Code: [Select]
// 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, 41, 42};
// drawer set (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()
{
  // serial setup for debugging
  Serial.begin(57600);

  // debug: print the number of drawers in the set of drawers
  Serial.print("Number of led pins = "); Serial.println(sizeof(ledPins));

  // 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()
{
}


4)
Next we can build loop(). We will put papers on every drawer indicating that all leds are off (LOW)so we're sure each drawer is empty at the beginning of loop(). We also add a counter (onCount)that counts the number of leds that is set to on (HIGH).
In code
Code: [Select]
  // 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;


5)
Next we remove from 5 (NUMLEDS) randomly selected drawers in the drawer set ledStates the paper and replace it with one of the pieces of paper with HIGH written on it. We only do this if the randomly selected drawer contains a piece of paper that has LOW written on it, else we will retry with another randomly selected drawer. Every time that we replace a piece of paper, we increment the counter. We use a while-loop() to check if the number of drawers that has a piece of paper with HIGH on it has reached the number of drawers that must have a piece of paper with HIGH on it.

In code
Code: [Select]
  // 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++;
    }
  }


Once we have 5 (NUMLEDS) drawers filled with a piece of paper with HIGH on it, we can use a for-loop to loop through all drawers in the ledStates drawer set and set the pin number in each drawer in the ledPins drawer set to the state that was found in the corresponding drawer (same number, drawer 0 <-> drawer 0, drawer 1 <-> drawer 1 etc) in the ledStates drawer set.

In code
Code: [Select]
  // 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; new line
  Serial.println();


Below the complete code
Code: [Select]
// 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, 41, 42};
// 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 (;;);
  }

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

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

Understand how it works?
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

So now we can run this only when a button is pressed; I have copied parts of your original code (in the opening post).

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, 41, 42};
// 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()
{
  ...
  ...
}


void loop()
{
  ...
  ...
}

And in setup, add setting the pin to input
Code: [Select]
void setup()
{
    // Set button pin
    pinMode(buttonPin, INPUT);

    // other code from setup() in reply #17 is here
    ...
    ...
}


And in loop() we use your button code
Code: [Select]
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;


  // Check if button is pressed
  if (digitalRead(buttonPin) == HIGH && buttonPress == false)
  {
    buttonPress = true;
  }

  if (buttonPress == true && timePassed <= timeShowRandom)
  {
    // I suppose that this is where you want to show a random led pattern
    ...
    ...
  }
  else if (buttonPress == true)
  {
    // no idea what needs to be done here
    ...
    ...
  }
  else
  {
    // Reset all output pins
    ...
    ...
  }
}

You need to give a proper description of the 3 parts; I understand the 'reset all pins' but not what the other parts exactly have to do. Let's start with the else
Code: [Select]
  else
  {
    // Reset all output pins
    for(byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
    {
      digitalWrite(ledPins[ledCnt], LOW);
    }
  }


And next where the loop() code from reply #17 fits in (if I'm correct).
Code: [Select]
  if (buttonPress == true && timePassed <= timeShowRandom)
  {
    // 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);


  }

Not tested, I hope it helps. I will not more than likely have time till tomorrow to help you further.
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

WOW lot of things so I tried to edit code as you said. Here is it. It can light up can pick "random" LEDs but button doesnt work. :/ I tried what you said but I dont rly know what are you doing so I traing. And I must say thx for help Soon it will work. Or atleast I hope. Bad part of that is: Its not random. Every time I restart Arduino its same pattern if you understand. Can you fix 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 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

Set a randomSeed for better randomness. An unconnected analog input usually works great.
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.

sterretje

Ok. Where is the reading of your button gone to? And the button press variable?

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


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


  }

 

wvmarle

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

sterretje

The below is not how you check a button. It would also be in the wrong place as welll; you should check in loop().
Code: [Select]
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.
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


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

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.

Mikey999

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
Code: [Select]
#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?

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

wvmarle

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.
See #20 on randomseed.
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


wvmarle

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

Go Up