Arduino Rock paper scissors project

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[i], OUTPUT);
   digitalWrite(ledPins[i], 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[i], dicePatterns[result][i]);
 }
}

Im not surprised your LEDs are acting weird.

Here is the issue.

int dicePatterns[3][3]
int blank = 6;
.
.
.
show(blank);

You are telling the code to go to an area outside the boundaries of your array. ie random data.

Also your show() function doesn't look right, maybe you should put your code in code tags. Click the <> button above the :neutral_face: face.

I would strongly recommend using the switch/case syntax and make each case a specific out come. You could even go so far as to have functions in each case, and give each outcome a little more customization.

Thank you hazard I have changed my int blank to 3 and now the problem where 2 or 0 light up is gone however every once and a while all 3 light up any other suggestions? :slight_smile:

g3t thank you for your suggestion but I am not very knowledgeable in this language since I just started so I do not understand what you mean by

I would strongly recommend using the switch/case syntax and make each case a specific out come. You could even go so far as to have functions in each case, and give each outcome a little more customization.

Cross-post, thread locked.

Cross moderator.