Memory game project

Hello

I am trying to make an electronic memory game... when its done it should have 5 or 6 buttons and 5 outputs.
The idea of the game is that the program shows a sequence of lights and then you have to push the buttons in the same sequence to go on to the next level...

I have tried to make it with 4 buttons and 3 outputs at the moment but it doesnt work as i would like it to...

the 4th button is a Done button, for when youre done with pushing in the sequence and then the program should check wether the sequence you pushed in is the same as the one the device showed earlier....

here is my code:

int pin2button = 2;
int pin4button = 4;
int pin6button = 6;
int outputpin3 = 3;
int outputpin5 = 5;
int outputpin7 = 7;
int array1[1] ={0};
int svararray1[1] = {};
int done = 8;
int donecheck = LOW;
int button2check = LOW;
int button4check = LOW;
int button6check = LOW;

void setup()
{

  pinMode(pin2button, INPUT);
  pinMode(pin4button, INPUT);
  pinMode(pin6button, INPUT);
  pinMode(done, INPUT);
  pinMode(outputpin3, OUTPUT);
  pinMode(outputpin5, OUTPUT);
  pinMode(outputpin7, OUTPUT);
}

void loop()
{
start1:
digitalWrite(outputpin3, HIGH);
delay(1000);
digitalWrite(outputpin3, LOW);
delay(1000);

do
{
  button2check = digitalRead(pin2button);
  button4check = digitalRead(pin4button);
  button6check = digitalRead(pin6button);
  donecheck = digitalRead(done);
  if (button2check == HIGH)
  {
    svararray1[1] = svararray1[1] + 0;
  }
  if (button4check == HIGH)
  {
    svararray1[1] = svararray1[1] + 1;
  }
  if (button4check == HIGH)
  {
    svararray1[1] = svararray1[1] + 2;
  }
}while (donecheck == LOW);

if (svararray1[1] == array1[1])
{
  goto congratulations1;
}
if (svararray1[1] != array1[1])
{
  goto wrong1;
}



congratulations1:
digitalWrite(outputpin7, HIGH);
delay(100);
digitalWrite(outputpin7, LOW);
digitalWrite(outputpin5, HIGH);
delay(100);
digitalWrite(outputpin5, LOW);
digitalWrite(outputpin3, HIGH);
delay(100);
digitalWrite(outputpin3, LOW);
digitalWrite(outputpin5, HIGH);
delay(100);
digitalWrite(outputpin5, LOW);
digitalWrite(outputpin7, HIGH);
delay(100);
digitalWrite(outputpin7, LOW);
goto start2;

wrong1:
digitalWrite(outputpin3, HIGH);
delay(500);
digitalWrite(outputpin3, LOW);
delay(500);
digitalWrite(outputpin3, HIGH);
delay(500);
digitalWrite(outputpin3, LOW);
delay(500);
digitalWrite(outputpin3, HIGH);
delay(500);
digitalWrite(outputpin3, LOW);
delay(500);
goto start1;

start2:
do
{
  donecheck == digitalRead(done);
}while(donecheck == LOW);

}

what i would like to know is, is there a simple or maybe another way to solve this problem?

regards

Lungefisk

edit:
What i would like it to do is store the value of the buttons pushed in an array, then when the done button is pushed it should check the array with the right answer array and if they are alike, then it should run a little sequence before movin on to the next sequence, that is a bit longer.

First, please modify your post. Select all of your code, then press the # button on the top row of buttons. This encloses your code in tags that causes the browser to display your code in a scroll window.

Second, this statement needs clarification:

but it doesnt work as i would like it to...

Are we to guess how you would like it to work?

Please explain what you want to have happen, and how what actually happens is different from what you want.

done

This is probably a problem...

  if (button[glow]4[/glow]check == HIGH)
  {
    svararray1[1] = svararray1[1] + 1;
  }
  if (button[glow]4[/glow]check == HIGH)
  {
    svararray1[1] = svararray1[1] + 2;
  }

Also, you have declared svararray1 to be an array of length 1. Why? Arrays of length 1 provide no benefit over single variables of the same type.

In any case, the only valid index into an array of length 1 is 0, not 1.

The svararray[0] (or even 1) position is never initialized. It is only ever incremented.

When the done button is pushed, you compare the sum of the button pushes to array1[1], which is also an invalid position in the array1 array. The array1 array is of length 1, and array1[0] is initialized to 0.

This means that no button pushes is the correct answer.

And, then, you use that dreaded goto statement that should never have been included in the C language.

Create a function, called congratulations, and move the stuff after the congratulations1 label into it. Call that function instead.

Do the same with wrong1.

Get rid of the start1 label.

I think you may have a fundamental misunderstanding of the loop function. It is called over and over again, not just once.

Create an array of some fixed size - however many lights items you want the user to remember. Create a function to populate that array, presumably with random values. Use that array in a function to flash the lights initially. (I don't see any lights flashing now...).

Create a second array of the same size as the first array. Each time the user presses a button, put the value for that button into the next position in the array. That requires that you keep track of the number of button presses.

A single variable can't be used, because the correct sequence of buttons might be 1, 1, 3, 2, 1, 1, which adds up to 9. So does 3, 3, 3 or 1, 1, 1, 1, 1, 1, 1, 1, 1.

When the correct number of buttons has been pressed, compare the two arrays, using a for loop, and call the appropriate function (congratulations or wrong).

The "done" button should really be a reset button. Pressing it starts the program over again.

Thanks i hope i can make it better now :slight_smile:

Ok now ive tried to remove the loop function, and make a main function instead, but then i went into a problem...
In function 'int main()':
error: redefinition of 'int main()'

how do i solve this one ?

And one more question....
How do i assign values to different spaces in the array instead of adding the one already in space [1] with a number...

do i do this by saying array[0] = 1, array[1] = 2, and so on? or ?

hope im not too much of a pain to you.

In function 'int main()':
error: redefinition of 'int main()'

An Arduino sketch doesn't (can't) have a "main" - it is provided for your sketch by the Arduino framework.
Why did you put one in?

@PaulS:

The svararray[0] (or even 1) position is never initialized.

yes it is, here:

int svararray1[1] = {};

@Groove

No, it isn't!

int svararray1[1] = {};

In a combined declaration and initialization statement, initialization only occurs for the array positions for which values are supplied. There are no values supplied, so no initialization occurs.

In this code, the EXACT same actions occur if the " = {}" portion of that statement are omitted.

In a combined declaration and initialization statement, initialization only occurs for the array positions for which values are supplied

..and those that aren't supplied are initialised to the default of zero.

So, for instance:

int array [6] = {1, 2, 3};

will result in array [3], array [4] and array [5] being set to zero.

[edit]K&R 2nd edition, p 86: "If there are fewer initializers for an array than the number specified, the missing elements will be zero for external, static and automatic variables."[/edit]

For a god, you're not very omniscient :wink:

..and those that aren't supplied are initialised to the default of zero.

That is a compiler-dependent action, and is never to be relied on.

Nonsense.
If I put a initialiser, even if it is empty, I expect the compiler to initialise.

It's still a much better idea to explicitly initialize everything.

It is explicitly initialised!
That's what the {} does.

This int array [12];is not initialised,
although this static int array [12]; is initialised.
It's what the language definition says is the correct behaviour.

It is explicitly initialised!
That's what the {} does.

It only ACTUALLY initializes the n memory locations for the n values supplied between the curly braces.

When there are 0 values, there is 0 initialization.

K&R 2nd edition, p86. See above.

Seeing this thread yesterday is what got me thinking about making a Simon Says game. I just took code someone else made and tweaked it to meet the specs i wanted. This code may be helpful to you.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1264142099

@PaulS:
Groove is correct when s/he says that the array is initialised.

In fact, even without the "= {};", the array in question would be initialised to zeroes, because this is the default for all variables declared outside of a function.
Variables not declared "static" within functions will have whatever junk happened to be on the stack.

Thus spake K&R :sunglasses:

Thus spake K&R

And from this you and Groove assume that all compiler writers do it right. I don't, and so I prefer to explicitly initialize everything.

Why don't you boys simply test this? :slight_smile:
(UNTESTED CODE)

int uninit[5];
int zeroinit[5] = {};
int explicitinit[5] = { 1,2,3,4,5 };
void setup() { 
   //move declarations/definitions here and see what happens
   Serial.begin(9600); 
   for (int i=0; i<5; i++) {
      Serial.print(uninit[i]);
      Serial.print(" ");
      Serial.print(zeroinit[i]);
      Serial.print(" ");
      Serial.println(explicitinit[i]);
   }
} 
void loop() {  }

Round 2: FIGHT! ::slight_smile:

Why don't you boys simply test this

Did you seriously think I hadn't? :o