Arduino resetting after playing a chess algo for a while. Memory issue?

First off this is my first time posting here so I hope im doing this right.

About half a year ago I bought an arduino. Things got out of hand quickly and now I am building a chess board where you can play against the arduino that also moves the pieces using an electromagnet. Throughout the whole project i have been struggling with resets. Mind you I knew nothing about electronics so I did everything wrong at first. I added a big flyback diode across the magnets coil. That resolved a lot of resets that happened after switching it off. Using a transistor now to switch the relay, which also has an octocoupler which is supposed to isolate the circuits I think.

With resets still rampant and me getting frustrated i found that removing all Serial.print statements might help. And it did!!! But then today trying my first actual game after about 20/30 turns the arduino decided to reset. What could possibly have caused it this time? It happened during the bit of code I borrowed from H.G Muller that decides what move the arduino plays. The steppers and the electromagnet are both disabled during that.

Ill add a few photos of my wiring after this paragraph. Its a big mess I know. The code is 600+ lines so I dont think I could just add it here. Whats a good way to share it?

Edit:
i found a nice way to share my code:
http://pastie.org/p/2ItNh0Y93uOI9IEOxiEmzP/raw

using an arduino NANO by the way

Thanks in advance for anyone that would like to help!

1 Like

Very nice project. Congrats on your progress.

Resets may be software or may be power-surges from external devices connected to the local home grid. My neighbor has an arc-welder so I really have to be careful :face_with_peeking_eye:

If you can borrow a AC-DC-DC UPS then you may wish to try running the project on that.
Or, a (big) battery.

Ultimately you probably need to output the runtime pool.

Measuring Memory Usage | Memories of an Arduino | Adafruit Learning System
note:
freeMemory() does use some memory and may be device model specific.

No problem, if you use code tags ("</>" post editor button) and you are MUCH more likely to get useful help.

I add: with such code size, I really hope OP won't make use of "String" variables... :wink:
But we'll have a look to that as soon as he'll post the code.

PS: We also need to know of which Arduino we're talking about.

http://pastie.org/p/2ItNh0Y93uOI9IEOxiEmzP/raw
here is a link to the code!
In another post i made the following explaination, feel free to read it if you care:
If youre talkign about the part near the end with all the one letter variables. I have absolutely no clue. I know its derived from this: Micro-Max (hccnet.nl) . Supposedly the smalles possible chess engine for arduino.

Most other things I did write myself. Most important of which are:

void reedLezen() ;

This bit of code is meant to read the 64 reed switches under the tiles that are supposed to sense the pieces through magnetism. S0-3 are the inputs for 4 daisy chained multiplexers. Sig1-4 are the outputs. These outputs are saved in the 8x8 "lezing"(meaning reading in Dutch) boolean matrix. A true/1 means that the sensor was active and thus a piece is present on that square.

After that the matrix is rotated 90 degrees because im dumb and installed the multiplexers wrong.

any time a move is confirmed the reedlezen matrix will be copied to board[8][8]. This is to save the board.

void playerMove() Is the bit of code that is supposed to determine what piece the player has moved. first reedlezen() is called to save the current situation in the lezing matrix. Then with two for loops its determined if any part of the lezing matrix is different from the bord matrix. If so that means a piece has moved since the last time the board was saved. If the reading from that specific sensor is 0 that means that there used to be a piece and no longer is. Meaning thats the origin of the piece that the player has moved. The mess of strings that happens after that is there to save the move into. In chess moves are written like "a2a3" for example.

The next bit is a bit of a mess but works surprysingly well. I wont explain the whole thing but the first if statement checks if there is more than one piece missing. If that is the case the game switches to "attack mode" where it now waits for the player to return a piece to one of newly empty squares. That square is then the destination of the piece. The piece that was previously there is captured.

If there is more than 2 pieces missing the screen will display an error. The code loops intill Ystop (one of the buttons next to the display) is pressed to confirm the players move.

The reading will be copied to the c[4] matrix to be input to the chess algo.

Then through some magic i dont understand the code will either output that the move you made was illegal, or the move it made itself.

void translateAImove(byte xo, byte yo, byte xd, byte yd)

After the chess algo has produced a move translateAImove is called to convert the caracter values to integers that can be used by the moving mechanism.

Movepiece() Is called with a x and y origin of the piece and x and y destination of the piece. It moves to the origin, enables the magnet and then moves to the destination after which the magnet is disabled again.

Moving itself is done with MoveTo() Which i think is pretty self explanatory.

The other void statements are all for running the OLED screen and are also self explanatory.

Id love to learn about more ways for me to improve this code. Thank you so much

Ummmm i did use a few String variables. Am i not supposed to? am i not getting something?
link to code:

And im using an arduino Nano

There's many warnings in your code. Try fix it to prevent unexpected behaviour.

Post the code in line, in your post, using code tags. Few forum members will go off site looking for your stuff.

A few? You have some, and many String function parameters.

  String inputString = "";
...
  String inputx = "abcdefgh";
  String inputy = "12345678";
  String origin1;
  String origin2;
  String destination;
...
void gameOver(String lastmove) {
  u8g.firstPage();
...
void OLED_board(String action, String textA, String textB, bool button2 ){
  int offset = 5; //offset from left, 36 is the middle
...

Yes. String was not designed for microcontrollers, due to limited memory device with no garbage collector (i.e. no high level achitectures and no underlying OS).
If you search Google for "arduino string memory leak" you'll have many results saying to avoid "String", and why. Start from HERE, a good explanation of why and how.

I understand changing a large code like that could be tedious, but I highly suspect that 90% chance your system is hanging due to memory leak caused by the infamous "String" class.
Sorry, If I were you I would try to remove all "String" converting them nto "C strings" (or "char arrays"). A bit more hard to handle, especially when one comes from high level languages, but under Arduino is highly recommended.

if (++Z > 30) {

This seems to control the recursion depth of the search algorithm, what happens when you set this to a lower value (e.g., 20 or 10)?

Incorrect use of String (capital S) can cause a lot of problems. One of them (taken from @docdoc)

void gameOver(String lastmove) {

When that function is called, a copy of the String object is placed on the stack. You're better off using

void gameOver(String &lastmove) {

which uses a reference; it might improve the situation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.