random winner

Hi i need to build the following...
everytime a visitor passes through a gate he press a button and the arduino will count. Then radomly will create a winner.
So, i want to give a bunch of numbers, lets say 4,9,15,22,37,56,112... and when the fouth, the nineth, the fifteenth... visitor passes a relay will arm and some lights will turn on. No need to show the numbers or the counting to a screen, just the light and maybe sound effects anytime the number meets the given numbers.
Thanks

See:

https://www.arduino.cc/en/Reference/Random

https://www.arduino.cc/en/Reference/RandomSeed

.

...but with random i cannot have the numbers that wins given prior. Can i?

Do you want it to be random or when it matches one of the pre-defined numbers?

If you want it to match against a predefined number, just keep a count of the number of button presses, and check it against the magic numbers every time the button is pressed...

i need it to match the predefined numbers.
i think i can keep the count of presses but i dont know how to check it with the pre defined numbers.
can you please help with this?

Put your numbers into an array.
Then randomly pick an element within that array.

You should think a bit more about how people are actually picked.

.

i want to have the winning "slots" prior the event and given to a lawyer so i will be legally OK and also i can control the number of the winners

Depending on the number of winning slots, either put them in an array and loop over the elements of the array (be careful not to try reading past the end of the array - remember that arrays in C are 0 indexed), or just do it the brute force way (eg, if (pressCount==15 || pressCount==33 /*and so on */) { blinkLights(); }

You'll also need to debounce the button so you only get one count per press, if you haven't already done that.

Thanks, I'll try write it in the morning. Till then any other idea, help...is appreciated!!!

Misleading. There is nothing random here.
You have a count variable that increments with a button event.

stmous:
i want to have the winning "slots" prior the event and given to a lawyer so i will be legally OK and also i can control the number of the winners

Why is it legally not OK to generate a random winner?

Grumpy_Mike:
Why is it legally not OK to generate a random winner?

Well random is legal as well but randomness is something that you can't prove easily if someone doubts the procedure. If the winners are pre defined it's more clear.

If the winners are pre defined it's more clear.

To my mind that opens up more scope for fixing the results than a "random" number that now one knows.

Hi again,
i came up with this code (took pieces of different projects)

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int relayPin = 13;       // the pin that the relay is attached to
const int myarray[] = {1,3,5,7,9,11,13}; //this array will hold integers

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(relayPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
 
}



void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
  // if the state has changed, increment the counter
  if (buttonState == HIGH) {
    // if the current state is HIGH then the button
    // wend from off to on:
    buttonPushCounter++;
    Serial.println("on");
    Serial.print("number of button pushes:  ");
    Serial.println(buttonPushCounter);
  } else {
    // if the current state is LOW then the button
    // wend from on to off:
    Serial.println("off");
  }
  // Delay a little bit to avoid bouncing
  delay(800);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

if (buttonPushCounter == myarray[0]) {
  digitalWrite(relayPin, HIGH);
} else {
  digitalWrite(relayPin, LOW);
}
 if (buttonPushCounter == myarray[1]) {
  digitalWrite(relayPin, HIGH);
} else {
  digitalWrite(relayPin, LOW);
}
if (buttonPushCounter == myarray[2]) {
  digitalWrite(relayPin, HIGH);
} else {
  digitalWrite(relayPin, LOW);
}
 if (buttonPushCounter == myarray[3]) {
  digitalWrite(relayPin, HIGH);
} else {
  digitalWrite(relayPin, LOW);
}
if (buttonPushCounter == myarray[4]) {
  digitalWrite(relayPin, HIGH);
} else {
  digitalWrite(relayPin, LOW);
}
 if (buttonPushCounter == myarray[5]) {
  digitalWrite(relayPin, HIGH);
} else {
  digitalWrite(relayPin, LOW);
}
if (buttonPushCounter == myarray[6]) {
  digitalWrite(relayPin, HIGH);
} else {
  digitalWrite(relayPin, LOW);
}

}

If i use only the first if statement relay works fine, when i add the other if statements its not working (no errors but the relay is not arming)

This is because you have one if statement that turns on the relay and all the other statements turn it off because of the else part.

Very silly code anyway what is wrong with a for loop and one if statement?

Read the how to use this forum sticky to learn about posting code and then go and edit that last post so the code is correctly posted at least.

Grumpy_Mike:
This is because you have one if statement that turns on the relay and all the other statements turn it off because of the else part.

Very silly code anyway what is wrong with a for loop and one if statement?

Read the how to use this forum sticky to learn about posting code and then go and edit that last post so the code is correctly posted at least.

Well the code maybe silly but that's why I'm asking help from this forum. Im just learning so if you can help with the for loop I would appreciate it.

// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
boolean relayState = LOW;
for(int i;i<7;i++){
if (buttonPushCounter == myarray[i]) {
  relayState = HIGH;
  } 
}
digitalWrite(relayPin, relayState);

Is what your code did without turning the relay off every time it saw an else. So if any of the array elements 0 to 7 match the buttonPushCounter variable then the relay pin is sent high otherwise it is set low.

Grumpy_Mike:

// save the current state as the last state,

//for next time through the loop
lastButtonState = buttonState;
boolean relayState = LOW;
for(int i;i<7;i++){
if (buttonPushCounter == myarray[i]) {
  relayState = HIGH;
  }
}
digitalWrite(relayPin, relayState);



Is what your code did without turning the relay off every time it saw an else. So if any of the array elements 0 to 7 match the buttonPushCounter variable then the relay pin is sent high otherwise it is set low.

Thank you very much. That indeed solve my problem.
Is there a way to keep the relay on for lets say 5 seconds and not till the next button press?

Yes but it is a big step up in complexity, you have to write your code as a state machine, see the doing several things at once sticky post in the programming section.

Grumpy_Mike:
Yes but it is a big step up in complexity, you have to write your code as a state machine, see the doing several things at once sticky post in the programming section.

Thank you very much. I'll better done step at a time. I'll read the post you mentioned.

Merry Christmas