Project not working correctly

I am currently working on an assignment of rock, paper, scissors, and we needed to use one of the Arduino examples which was the LED Dice example found in the book and I wanted to change around the example a little bit so two people would be able to play this game. I have 8 LED's in total 6 of them is the rock paper scissor LED's and the other 2 would light up on either side to so who wins the hand. The conflict that I am currently facing is fixing the code up so that it will work.

If anyone has any idea how to get this to work please help me out thanks.

In regards;
kingtut25

Given that you have one post and this is the second request for the same thing, is this some sort of assignment you are supposed to do yourself?

What is the issue specifically that you are having? Just saying "it doesn't work" does not allow anyone to offer assistance. I'm sure many here have ideas to get it to work, but we need to know what you're stuck with to know how to help you. :wink:

I'm going to try and answer this as best as I can, I am working with an Arduino Uno and I am working on this project and we need to create rock, paper, scissors with the Arduino Uno and breadboard and I have the wiring correct it looks like anyways. But the problem I think is coming from my coding. We needed to change around the LED Dice program (one of the examples here on the site) and so I am not sure what I needed to change and/or add to the program to make it work. I know that I am using 2 red LED's, 2 yellow LED's and 2 green LED's and 330 ohm resistors. Then we are supposed to have 1 LED on both sides and that LED is supposed to light up on either side to show which hand won the rock, paper, scissor game.

So overall, I needed to change the LED Dice example program around so that it fits to work on my assignment of rock, paper, scissors.

Thanks,
kingtut25

So sounds like an assignment to me.

What good is it if we do it for you? You learn nothing.

Show some effort and we can help, throw up your hands and say you are lost and we won't. Show some insight into what you want to do. Plan it out in just words, then attack each sentence one at a time.

I'm not familiar w/ the LED Dice example, but let's start with something simple.

In your current design, are you able to control each of the LEDs? Just turn them on and off? Forget anything to do with dice or rock,paper,scissors for now. Does your code allow you to control the 8 LEDs?

Start with that.

bad_crc:
Start with that.

.... and then, or in parallel with that, write down the steps you want the program to handle.

For example, if each player can choose R, P or S, presumably they have 3 buttons each? Then you need the program to read the pins to see which button was pressed and compare the two to determine the winner. You also might need some delay so that the RPS leds light up together, only after both players press a button, so that they can't cheat 8) .

Only after you design the algorithm (those steps) should you think of coding. Personally I don't see this as a enhancement to the dice program: the only common thing is the 6 leds.

And then add Lizard, Spock....

Can you give us the link to the actual assignment you have been set? Or if it was handed out on paper (how 20th Century!), a photo of it?

It would make the question much clearer.

Ok, so heres the code I am currently working with, I have one LED powered on cant control it and the other 5 LEDs are off.

int rock = 2;
int rock1 = 5;
int paper = 3;
int paper1 = 6;
int scissor = 4;
int scissor1 = 7;

int switchPin = 8;
int blank = 11;
int blank2 = 10;

void setup()
{
pinMode(rock, OUTPUT);
pinMode(rock1, OUTPUT);
pinMode(paper, OUTPUT);
pinMode(paper1, OUTPUT);
pinMode(scissor, OUTPUT);
pinMode(scissor1, OUTPUT);
pinMode(switchPin, INPUT);
}

int x = random(1, 4);

void loop()
{
if (digitalRead(switchPin))
int x = random(1, 4);
if (x = 1)
{
RPS(HIGH, LOW, LOW, LOW, LOW, LOW);
}
else if (x = 2)
{
RPS(LOW, HIGH, LOW, LOW, LOW, LOW);
}
else if (x = 3)
{
RPS(LOW, LOW, HIGH, LOW, LOW, LOW);
}
else if (x = 4)
{
RPS(LOW, LOW, LOW, HIGH, LOW, LOW);
}
else if (x = 5)
{
RPS(LOW, LOW, LOW, LOW, HIGH, LOW);
}
else if (x = 6)
{
RPS(LOW, LOW, LOW, LOW, LOW, HIGH);
}
}

void RPS (int Rock, int Rock1, int Paper, int Paper1, int Scissor, int Scissor1)
{
digitalWrite(rock, Rock);
digitalWrite(rock1, Rock1);
digitalWrite(paper, Paper);
digitalWrite(paper1, Paper1);
digitalWrite(scissor, Scissor);
digitalWrite(scissor1, Scissor1);
}

In an if statement there should be a double equals sign NOT a single one.

 int x = random(1, 4);

How can that generate a number greater than 4? So why test for them?

USE CODE TAGS when posting code.

So I have made the changes to the if statements but things are still not working as expected. I have narrowed the problem down to the random selection. When I change the parameters of the random statement a different light goes off, but it is always the same light. When I do random x (1,7) it always picks number 2. Based on my previous code, is there something that I have done wrong with the randomization?

Because you never initialise the random number generator it will always produce the same sequence of numbers.

Ok, Sorry to be ignorant, but how do you initialize?

You go to google and put in these words:-

arduino initializing the random number generator

and look at the many ways to do it.

Here's an updated version of the code and we can't get it to work. The random number generator is not working, when we press the button the LED's do nothing. One green LED remains on and nothing else happens.

int rock = 2;
int rock1 = 5;
int paper = 3;
int paper1 = 6;
int scissor = 4;
int scissor1 = 7;

int switchPin = 8;
int blank = 10;
int blank2 = 11;  

void setup()
{
  pinMode(rock, OUTPUT);
  pinMode(rock1, OUTPUT);
  pinMode(paper, OUTPUT);
  pinMode(paper1, OUTPUT);
  pinMode(scissor, OUTPUT);
  pinMode(scissor1, OUTPUT);
  pinMode(switchPin, INPUT);
  randomSeed(analogRead(0));
}


int x = random (1,7);

void loop()
{
  if (digitalRead(switchPin))
int x = random (1,7);
  if (x == 1)
  {
    RPS(HIGH, LOW, LOW, LOW, LOW, LOW);
  }
  else if (x == 2) 
  {
    RPS(LOW, HIGH, LOW, LOW, LOW, LOW);
  }
  else if (x == 3)
  {
    RPS(LOW, LOW, HIGH, LOW, LOW, LOW);
  }
  else if (x == 4)
  {
    RPS(LOW, LOW, LOW, HIGH, LOW, LOW);
  }
  else if (x == 5)
  {
    RPS(LOW, LOW, LOW, LOW, HIGH, LOW);
  }
  else if (x == 6)
  {
    RPS(LOW, LOW, LOW, LOW, LOW, HIGH);
  }
}


void RPS (int Rock, int Rock1, int Paper, int Paper1, int Scissor, int Scissor1)
{
  digitalWrite(rock, Rock);
  digitalWrite(rock1, Rock1);
  digitalWrite(paper, Paper);
  digitalWrite(paper1, Paper1);
  digitalWrite(scissor, Scissor);
  digitalWrite(scissor1, Scissor1);
}
 if (digitalRead(switchPin)) 

// this line is wrong replace it with

 if (digitalRead(switchPin)) {

// and add a } at the end of the sketch

The other thing that is wrong is that as long as the button is held down it will look like all the LEDs are on. When you remove it then it will freeze.

However only one LED is lit up when you do, but you might want it to do this.

I took your word for it, I did the switch and now it continuously states that 'RPS' is not declared in the scope. I removed those brackets afterwards to see if those brackets you told me to place was the problem but it was not the issue. I still do not fully understand what's going on to make this project function the way it's supposed to.

I did the switch and now it continuously states that 'RPS' is not declared in the scope.

So that means you did not do what I said or you had different code to that which you last posted.

I removed those brackets afterwards to see if those brackets you told me to place was the problem but it was not the issue.

Yes it was. If you are not prepared to follow advice then why bother asking the question.

If you tried something and it did not work then you should post the code that did not work.

I took your code and got it working on my machine. Your problem is that you had two variables one local and one global with the same name. You then only accessed the global one because the local one was only declared inside the if statement and then became immediately out of scope, leaving the global one to be used in the code and that one never got updated.

int x = random (1,7);

void loop()
{
  if (digitalRead(switchPin))
int x = random (1,7);
  if (x == 1)
  {

SEE TWO VARIABLES WITH THE SAME NAME x one global and the other local.

So put the brackets back and post the code.

int rock = 2;
int rock1 = 5;
int paper = 3;
int paper1 = 6;
int scissor = 4;
int scissor1 = 7;

int switchPin = 8;
 

void setup()
{
  pinMode(rock, OUTPUT);
  pinMode(rock1, OUTPUT);
  pinMode(paper, OUTPUT);
  pinMode(paper1, OUTPUT);
  pinMode(scissor, OUTPUT);
  pinMode(scissor1, OUTPUT);
  pinMode(switchPin, INPUT);
  randomSeed(analogRead(0));
}


int x = random (1,7);


void loop()
{
  if (digitalRead(switchPin)) {
    
int x = random (1,7);
  if (x == 1)
  {
    RPS(HIGH, LOW, LOW, LOW, LOW, LOW);
  }
  else if (x == 2) 
  {
    RPS(LOW, HIGH, LOW, LOW, LOW, LOW);
  }
  else if (x == 3)
  {
    RPS(LOW, LOW, HIGH, LOW, LOW, LOW);
  }
  else if (x == 4)
  {
    RPS(LOW, LOW, LOW, HIGH, LOW, LOW);
  }
  else if (x == 5)
  {
    RPS(LOW, LOW, LOW, LOW, HIGH, LOW);
  }
  else if (x == 6)
  {
    RPS(LOW, LOW, LOW, LOW, LOW, HIGH);
  }
}
}


void RPS (int Rock = 2, int Rock1 = 5, int Paper = 3, int Paper1= 6, int Scissor= 4, int Scissor1= 7)
{
  digitalWrite(rock, Rock);
  digitalWrite(rock1, Rock1);
  digitalWrite(paper, Paper);
  digitalWrite(paper1, Paper1);
  digitalWrite(scissor, Scissor);
  digitalWrite(scissor1, Scissor1);
}

Sorry to be ignorant, but im not exactly understanding what you want me to do.