Jump to content

I have to create a memory game for the Arduino. I know there is code online, but it's for school, so I can't just copy paste/rewrite it - nevermind that I don't really understand it either.

 

The way I'm doing it, I have 4 methods:

 

createLevel() creates the level, and repeats 5 times adding difficulty every time it runs.

showLevel() displays the created level.

getUserInput() is supposed to pause the main loop every time a memory is shown, wait for user input and then create an array of what the user inputted, the size of which should correspond with the size of the level array.

compareMemory() is supposed to compare the memoryArray and the userInputArray and continue the game if they're the same, and reset the game if they're different.

 

The last two don't wanna work, and I can't seem to figure out to logic to make it.

 

Can any of you guys help? I've attached the .ino file to this post.

I have 4 buttons which are connected to pins 4-7 and the leds are connected to pins 8-11.

MemoryGame.ino

RuneScape is still the best MMO ever made.

Program on Mac | Game on PlayStation | Homelab on Ubuntu

Link to comment
https://linustechtips.com/topic/709126-arduino-memory-game-problems/
Share on other sites

Link to post
Share on other sites

11 hours ago, ElfenSky said:

I have to create a memory game for the Arduino. I know there is code online, but it's for school, so I can't just copy paste/rewrite it - nevermind that I don't really understand it either.

 

The way I'm doing it, I have 4 methods:

 

createLevel() creates the level, and repeats 5 times adding difficulty every time it runs.

showLevel() displays the created level.

getUserInput() is supposed to pause the main loop every time a memory is shown, wait for user input and then create an array of what the user inputted, the size of which should correspond with the size of the level array.

compareMemory() is supposed to compare the memoryArray and the userInputArray and continue the game if they're the same, and reset the game if they're different.

 

The last two don't wanna work, and I can't seem to figure out to logic to make it.

 

Can any of you guys help? I've attached the .ino file to this post.

I have 4 buttons which are connected to pins 4-7 and the leds are connected to pins 8-11.

MemoryGame.ino

If I understood correctly, it is about the user correctly repeating the light sequence produced by the arduino. Every successful completion means that the next level will have one more light added. Right? (correct me if Im wrong).

The code you provided is wrong on several levels, but mainly in the getUserInfo function. You need debouncing and some way to know if the user has released the button. Remember that the arduino polls millions of times per second so this code:

for(int x=0; x<=3; x++)
      {
        if (digitalRead(buttonPin[x] == LOW))
        {
          userInputArray[i] = x;
        }
      }

Will instantly fill all the array with zeroes. I really don't fell like correcting it all AND explaining it, so pick one :D

Cheers

Quote or tag if you want me to answer! PM me if you are in a real hurry!

Why do Java developers wear glasses? Because they can't C#!

 

My Machines:

The Gaming Rig:

Spoiler

-Processor: i5 6600k @4.6GHz

-Graphics: GTX1060 6GB G1 Gaming

-RAM: 2x8GB HyperX DDR4 2133MHz

-Motherboard: Asus Z170-A

-Cooler: Corsair H100i

-PSU: EVGA 650W 80+bronze

-AOC 1080p ultrawide

My good old laptop:

Spoiler

Lenovo T430

-Processor: i7 3520M

-4GB DDR3 1600MHz

-Graphics: intel iGPU :(

-Not even 1080p

 

Link to post
Share on other sites

15 hours ago, dany_boy said:

If I understood correctly, it is about the user correctly repeating the light sequence produced by the arduino. Every successful completion means that the next level will have one more light added. Right? (correct me if Im wrong).

The code you provided is wrong on several levels, but mainly in the getUserInfo function. You need debouncing and some way to know if the user has released the button. Remember that the arduino polls millions of times per second so this code:


for(int x=0; x<=3; x++)
      {
        if (digitalRead(buttonPin[x] == LOW))
        {
          userInputArray[i] = x;
        }
      }

Will instantly fill all the array with zeroes. I really don't fell like correcting it all AND explaining it, so pick one :D

Cheers

 

I fixed the game another way, by changing the way getUserInput works and calling it in the compareMemory(), immediatly comparing a button press to the corresponding led.

 

I'm still curious how/if my original idea would work, so I'll pick explain instead of correcting :P

 

RuneScape is still the best MMO ever made.

Program on Mac | Game on PlayStation | Homelab on Ubuntu

Link to post
Share on other sites

Sorry i took so long, but here goes:

In general the program is not wrong per say, it is just has lots of bad practices as well as some actual mistakes that have to do with the fact that you are dealing with a micro:

  • the ShowLevel() function has a return at the end, this is contradictory as "void" should not and will not return anything upon completion. This is actually caught by the compiler and will show you an error.
  • As previously mentioned, getUserInput needs some way of knowing when the user has released the button. Otherwise the arduino will execute the for loop in a matter of microseconds, resulting in a single button press (lasting around 1 sec on average) filling up the array with nonsense.
  • Within your main loop, there should be a logic structure that allows the program track the progress of the user. The way you provided the code, it will just do the functions one after-another spewing out gibberish that don't really go along with the correct flow of the game. You would need to implement a loop that tracks the current level and decides if the game is over, or if the appropriate sequences should be run again.
  • A word on using comments, they should be used to explain the complex parts of the code that you wrote according to your logic. Anyone can perfectly understand what your variables and arrays do based on their names. On the other hand your functions contain no explanation at all, making it difficult to understand the flow of the code.
  • Finally, just a word on variables and functions. I know its trivial and not really relevant but normally functions are written with capital letters. Eg. getUserInput --> GetUserInput, and variables just as you wrote them, starting with a lower case. (At least in C#)
  • Cheers!

Quote or tag if you want me to answer! PM me if you are in a real hurry!

Why do Java developers wear glasses? Because they can't C#!

 

My Machines:

The Gaming Rig:

Spoiler

-Processor: i5 6600k @4.6GHz

-Graphics: GTX1060 6GB G1 Gaming

-RAM: 2x8GB HyperX DDR4 2133MHz

-Motherboard: Asus Z170-A

-Cooler: Corsair H100i

-PSU: EVGA 650W 80+bronze

-AOC 1080p ultrawide

My good old laptop:

Spoiler

Lenovo T430

-Processor: i7 3520M

-4GB DDR3 1600MHz

-Graphics: intel iGPU :(

-Not even 1080p

 

Link to post
Share on other sites

On 12/22/2016 at 2:51 AM, dany_boy said:

Sorry i took so long, but here goes:

In general the program is not wrong per say, it is just has lots of bad practices as well as some actual mistakes that have to do with the fact that you are dealing with a micro:

  • the ShowLevel() function has a return at the end, this is contradictory as "void" should not and will not return anything upon completion. This is actually caught by the compiler and will show you an error.
  • As previously mentioned, getUserInput needs some way of knowing when the user has released the button. Otherwise the arduino will execute the for loop in a matter of microseconds, resulting in a single button press (lasting around 1 sec on average) filling up the array with nonsense.
  • Within your main loop, there should be a logic structure that allows the program track the progress of the user. The way you provided the code, it will just do the functions one after-another spewing out gibberish that don't really go along with the correct flow of the game. You would need to implement a loop that tracks the current level and decides if the game is over, or if the appropriate sequences should be run again.
  • A word on using comments, they should be used to explain the complex parts of the code that you wrote according to your logic. Anyone can perfectly understand what your variables and arrays do based on their names. On the other hand your functions contain no explanation at all, making it difficult to understand the flow of the code.
  • Finally, just a word on variables and functions. I know its trivial and not really relevant but normally functions are written with capital letters. Eg. getUserInput --> GetUserInput, and variables just as you wrote them, starting with a lower case. (At least in C#)
  • Cheers!

 

I see, thank you. I will try to implement it next week and update this post if I get it working :).

The comments are mostly for myself, as arrays confuse me, and I put it there to remember that array[3] does =/= 3, but is a position. Fair point though.

 

As far as both variables and function names go, I thought C++ used lowerCamelCase, and since the Arduino is afaik based on that, that is what I used.  Nevermind, I should read better. Yeah, I'll change the functions to start with a capital.


 

 

 

 

RuneScape is still the best MMO ever made.

Program on Mac | Game on PlayStation | Homelab on Ubuntu

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×