HELP ROCK PAPER SCISSORS

Hi guys I am fairly new at arduino and I am currently working on a rock paper scissors project. I am using a arduino uno and a small bread board. I have it programmed so that when I push a button 3 LED lights begin to flicker then after a small amount of time 2 lights are supposed to turn off leaving one randomly chosen light on (this is the end result of the game [rock = red, paper = yellow, scissors = green])

However sometimes when the button is pushed 2 lights stay on as a end result or sometimes all 3 or 0 turn on too :sob:

I am thoroughly confused onto how to fix this and would be very grateful if I were to receive any suggestions on how to fix this problem

THANKYOU :smiley:

The Code:

int ledPins[3] = {2, 3, 4};   //scissoirs = 2      paper = 3     rock = 4
int dicePatterns[3][3] = {
 {0,0,1},    //rock
 {0,1,0},    //paper
 {1,0,0}    // scissors
};

int badPattern[5][3] = {
 {1,1,0},
 {1,0,1},
 {0,1,1},
 {0,0,0},
 {1,1,1} 
};

int switchPin = 9;
int blank = 6;

void setup()

{
 for (int i = 0; i < 4; i++)
 {
   pinMode(ledPins, OUTPUT);
   digitalWrite(ledPins, LOW);
 }
 randomSeed(analogRead(0));
}


void loop()
{
 if (digitalRead(switchPin))
 {
   rollTheDice();
 }
 delay(100);
}

void rollTheDice()
{
 int result = 0;
 int lengthOfRoll = random(15, 25);
 for (int i = 0; i < lengthOfRoll; i++)
 {
   result = random(0, 4);      //result will be 0 - 5 not 1 - 6
   show(result);
   delay(50 + i * 10);
 }
 for (int j = 0; j < 3; j++)
 {
   show(blank);
   delay(500);
   show(result);
   delay(500);
 }
}

void show(int result)
{
 for (int i = 0; i < 7; i++)
 {
   digitalWrite(ledPins, dicePatterns[result]);
 }
}

Have another look at your show(function)...

pinMode(ledPins, OUTPUT);?

for (int i = 0; i < 7; i++)
 {
   digitalWrite(ledPins, dicePatterns[result]);

And again

 result = random(0, 4);      //result will be 0 - 5 not 1 - 6

Oh yeah?

You should probably scrap the old dice game and re-start with code that just picks a random number from 0 to 2 (0=Rock, 1=Paper, 2=Scissors). Then it will be much easier to decide which of the three lights to turn on.

One you get it to turn on a single light you can add the visual effects like blinking all the lights for a while before showing the result.

This program doesnt look quite stable. :stuck_out_tongue: