A game i've been working on and I'm just stuck

Alright, I have been stuck on this for a while. I'm making a little game that shocks the loser and here is how I want it to work.

1.Arduino is powered
2.switch is flipped to start the game
3.sound buzzes after a random amount of time
4.two players have big buttons in front of the players and the first one to press their button shocks the other person.

This is the first project that I have tried on my own and i'm having issues with a certain part. I cant stop my random function when it buzzes once, I have tried so many things and honestly, my chimp brain cant figure it out.

If this is the wrong place to put it i'm sorry, let me know where I should post this someplace else if that is the case.

int player1 = 2;
int player2 = 3;
int startButton = 4;
int relay1 = 8;
int relay2 = 9;
int buzzer = 10;
bool condition = false;
int led = 13;
bool activeGame = false;
long randomNumber;

void setup() {
  // put your setup code here, to run once:
  pinMode(player1,INPUT);
  pinMode(player2,INPUT);
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode(startButton,INPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(led,OUTPUT);
  randomSeed(analogRead(0));
  Serial.begin(9600);
}

void startGame()
{
  randomNumber = random(10,100);
  
   if(randomNumber = 50)
    {
      delay(2000);
      tone(10,1000,1000);
      activeGame = true;
      
    }
}


void loop() {
  // put your main code here, to run repeatedly:
    if(digitalRead(startButton) == HIGH && activeGame == false)
    {
      Serial.println(randomNumber);
      startGame();
    }
    else
    {
      digitalWrite(startButton,LOW);
    }

    if(digitalRead(player1) == HIGH && activeGame == true)
    {
      digitalWrite(relay2, LOW);
      delay(1000);
      activeGame =!activeGame;
    }
    else
    {
      digitalWrite(relay2, HIGH);
    }

    if(digitalRead(player2) == HIGH && activeGame == true)
    {
      digitalWrite(relay1, LOW);
      delay(1000);
      activeGame =!activeGame;
    }
    else
    {
      digitalWrite(relay1, HIGH);
    }
}

If I get this right, you want to generate random numbers until you get 50, and then buzz? Well to do that you will need to put it in a while loop, breaking from the loop once you get 50.

But let's take a step back. Why do it this way? Is 1 random number not random enough? Why not generate a random delay time?

One last question on the game, have you thought about what would happen if someone were to press early?

the first one to press their button shocks the other person.

Is that "shock" as in apply electricity to the other person?

Hey thank you so much for the response!

The goal of the 50 being there was just a random number for it to hit, i figure it has as much chance of hitting any number so the number it hits doesn't matter. I was more so just adjusting the max of the random to make it a bit longer for it to hit it.

The way I was going about it seemed like a step in the right direction for me, however your suggestion of a random delay time sounds like a better option, and after a literal 5 second google search that looks a lot better then my route, so thank you so much for that! you saved me so much time!

And yeah! Hitting the button before the buzzer is a good question. Giving it some thought I was thinking just have two more if statements where if the digital read of the button is high and the activeGame is false , then their own relays get set to LOW, and I just do that for both players.

I wanna say thanks btw!! I've literally been stuck on this for a couple of days so i'm really thankful! Also the suggestion about pressing the button early is also some hindsight I really should have considered before hand. if you have any comments about the way to fix the early button press I have put above I would love to hear it!

alletta_de_villiers:
Is that "shock" as in apply electricity to the other person?

Kind of yeah, I have a TENS unit wire cut up into the relay. The "shock" level is whatever setting you have the TENS unit on. It sounds scary, I know, but the player gets to set the setting on the tens unit before hand to whatever the player feels comfortable with. The lowest setting feels like light pressure, while the highest setting feels like a hard pinch. I've tried the TENS unit on myself before with and without the game, and every time I have someone try it, I have them go from the lowest level, till they feel comfortable.

mrbubbler:
Hey thank you so much for the response!

The goal of the 50 being there was just a random number for it to hit, i figure it has as much chance of hitting any number so the number it hits doesn't matter. I was more so just adjusting the max of the random to make it a bit longer for it to hit it.

The way I was going about it seemed like a step in the right direction for me, however your suggestion of a random delay time sounds like a better option, and after a literal 5 second google search that looks a lot better then my route, so thank you so much for that! you saved me so much time!

And yeah! Hitting the button before the buzzer is a good question. Giving it some thought I was thinking just have two more if statements where if the digital read of the button is high and the activeGame is false , then their own relays get set to LOW, and I just do that for both players.

I wanna say thanks btw!! I've literally been stuck on this for a couple of days so i'm really thankful! Also the suggestion about pressing the button early is also some hindsight I really should have considered before hand. if you have any comments about the way to fix the early button press I have put above I would love to hear it!

Happy you were able to figure it out!

Indeed I would agree, if a person presses their button early, they shock themselves. (who doesn't love a bit of self-inflicted pain?)

However, in the way you suggested it, this would only happen before the start button is pressed. While, I would expect it to happen after the start button is pressed, but before the buzzer sounds. The problem with using delay() is that is blocks (almost) all other functions of the arduino. The 'several things at the same time' example stickied on this board nicely explains how to avoid this. Once you have your code working without the early-press, this can definitely be a next step to look at!

TimMJN:
(who doesn't love a bit of self-inflicted pain?)

I don't; don't judge everyone by your own standards, thank you.

I'm reluctant to assist in coding a sketch whose purpose is (to my mind) fundamentally wrong, but I'll pretend it's just turning on an led not an electric shock, and go for a state machine:

  • Await start button, capture millis() when it's pressed, head to state....
  • Generate random time, blip own relay led if button pressed here, when random time up sound buzzer, head to state ...
  • Await player button, when a button is pressed turn loser relay led on, capture millis(), head to state...
  • One relay led is on, when it's been on for a certain time, turn off, return to state 1 for new game

I am very scathing about Elf 'n' Safe Tea busybodies generally, but here I think they would rightly be having kittens.

You don't know what the safe level of Voltage is for each individual. Someone with a Pacemaker or other health issues could be put at risk. My advice is to think again about a suitable loser's penalty. Tens machines are not something that you just pick up in the supermarket after all, they are for medically advised and assessed pain relief.