Memory overload?

Hello!

Im building a toy with arduino and an Rfid-reader. Everything is working just fine with the reader, detecting the different Rfid’s and giving me the correct feedback. But after a while it seems like some kind of internal storing memory gets overloaded, and it stops functioning. This happens faster if I feed the reader with different Rfid-chips. If I feed it the same chip I can go on forever… Must I reset some storing fascilities? I have read about Sram, it might be this component that gets overloaded? The code itself is not more than 5500 byte, so I dont think this is the problem. The exact same thing happens on the Arduino NG Im using for prototyping and testing and on the Arduino Mini that Im using in my model. Does nybody know what I should try? I’m planning to have at least 16 different id’s in the code at last and so far I’ve just included seven…

Thanks in advance if anybody experienced the same…

Sara

If I’m reading your email correctly your code is generating a memory overrun. This is a very common problem with C/C++ and is usually caused by accessing an array outside of its defined bounds. If you post your code it might help identify the specific problem.

Hi, thanks.

This sounds like a possible explanation. I’m using arrays to detect the Rfid’s, and like I said there seems to be a much faster overload when reading many different (only 6-7 id’s) than when reading the same over and over again. But I’m not sure if the problem is in the detection iof the actual Rfid’s or in the stringcompare I use later to give the accurate feedback. Anyway, here’s my code…

// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOutPin = 9;
int smallVibePin = 3;
int bigVibePin = 10;
int soundBoxPin = 11;

// RFID READER ======================================
int inByte;
char tagArray[12]; // the array for the tags
int byteCount = 0; // counter for filling the tag array

//sun = 0103F8B0B7F
//flower = 0103F895523
//star = 0103F8AACF9

char sun[12] = {
‘0’, ‘1’, ‘0’, ‘3’, ‘F’, ‘8’, ‘B’, ‘0’, ‘B’, ‘7’, ‘F’, ‘\0’};
char flower[12] = {
‘0’, ‘1’, ‘0’, ‘3’, ‘F’, ‘8’, ‘9’, ‘5’, ‘5’, ‘2’, ‘3’, ‘\0’};
char star[12] = {
‘0’, ‘1’, ‘0’, ‘3’, ‘F’, ‘8’, ‘A’, ‘A’, ‘C’, ‘F’, ‘9’, ‘\0’};
char speaker[12] = {
‘0’, ‘1’, ‘0’, ‘4’, ‘C’, ‘3’, ‘8’, ‘3’, ‘7’, ‘3’, ‘3’, ‘\0’};
char smallvibe[12] = {
‘0’, ‘1’, ‘0’, ‘4’, ‘C’, ‘3’, ‘9’, ‘F’, ‘F’, ‘A’, ‘A’, ‘\0’};
char bigvibe[12] = {
‘0’, ‘1’, ‘0’, ‘4’, ‘C’, ‘2’, ‘D’, ‘3’, ‘E’, ‘8’, ‘F’, ‘\0’};
char soundbox[12] = {
‘0’, ‘1’, ‘0’, ‘4’, ‘C’, ‘2’, ‘E’, ‘2’, ‘5’, ‘8’, ‘7’, ‘\0’};

void setup() {
beginSerial(9600);
pinMode(speakerOutPin, OUTPUT);
pinMode(smallVibePin, OUTPUT);
pinMode(bigVibePin, OUTPUT);
pinMode(soundBoxPin, OUTPUT);

}

void loop() {

// PRINTING OUT ARPHID’S ======================================
if (serialAvailable()) {

inByte = serialRead();
//Serial.println(inByte);

switch (inByte) {
case 0x02: // the start byte
// reset the byte array counter:
byteCount = 0;
break;
case 0x03: // the end byte
// print out the tag array:
printTagArray();
break;
case ‘\r’: // carriage return
case ‘\n’: // linefeed
// we don’t care about these, so ignore them
break;
default:
// increment the byte counter:

// there are eleven bytes in the tag ID, so
// if the count is less than 11, add the byte
// to the array:
if (byteCount < 11) {
tagArray[byteCount] = inByte;
byteCount++;
}
// add a final over-and-out byte:
else {
tagArray[byteCount] = ‘\0’;
}
break;
}
}

}

// DETECT THE ARPHIDS ================================================================
void printTagArray() {
// print out the tag array byte by byte:
Serial.print(“tagArray :”);
Serial.print(tagArray);
// print a new line to separate the tags:
Serial.println();

if(strcmp(tagArray, sun) == 0) {
Serial.print(" Sun :");
Serial.print( sun);
slide_up();
}

if(strcmp(tagArray, flower) == 0) {
Serial.print(" Flower :");
Serial.print( flower);

}

if(strcmp(tagArray, star) == 0) {
Serial.print(" Star :");
Serial.print( star);

}

if(strcmp(tagArray, speaker) == 0) {
Serial.print(" Speaker :");
Serial.print( speaker);
speak1();
}

if(strcmp(tagArray, smallvibe) == 0) {
Serial.print(" Smallvibe :");
Serial.print( smallvibe);
vibe1();
}

if(strcmp(tagArray, bigvibe) == 0) {
Serial.print(" Bigvibe :");
Serial.print( bigvibe);
vibe2();
}

if(strcmp(tagArray, soundbox) == 0) {
Serial.print(" Soundbox :");
Serial.print( soundbox);
box1();
}

// print a new line to separate the tags:
Serial.println();
}

// PLAY A SLIDING TONE =======================================
void slide_up() {
int i = 3000;
while(i > 0) {
digitalWrite(speakerOutPin,HIGH);
delayMicroseconds(i%3000);
digitalWrite(speakerOutPin, LOW);
delayMicroseconds(i%3000);
i-=32;
}
int j = 4000;
while(j > 1000) {
digitalWrite(speakerOutPin,HIGH);
delayMicroseconds(j%3000);
digitalWrite(speakerOutPin, LOW);
delayMicroseconds(j%3000);
j-=32;
}
}

void slide_down() {
int i = 0;
while(i < 8000) {
digitalWrite(speakerOutPin,HIGH);
delayMicroseconds(i%8000);
digitalWrite(speakerOutPin, LOW);
delayMicroseconds(i%8000);
i+=16;
}
}

void speak1() {
int i = 3000;
while(i > 0) {
digitalWrite(speakerOutPin,HIGH);
delayMicroseconds(i%3000);
digitalWrite(speakerOutPin, LOW);
delayMicroseconds(i%3000);
i-=64;
}
delay (200);
int j = 5000;
while(j > 1000) {
digitalWrite(speakerOutPin,HIGH);
delayMicroseconds(j%4000);
digitalWrite(speakerOutPin, LOW);
delayMicroseconds(j%4000);
j-=64;
}
}

void vibe1() {
int i = 3000;
while(i > 0) {
digitalWrite(smallVibePin, HIGH);
delayMicroseconds(i%3000);
digitalWrite(smallVibePin, LOW);
delayMicroseconds(i%3000);
i-=64;
}
delay (200);
int j = 5000;
while(j > 1000) {
digitalWrite(smallVibePin, HIGH);
delayMicroseconds(j%4000);
digitalWrite(smallVibePin, LOW);
delayMicroseconds(j%4000);
j-=64;
}
digitalWrite(smallVibePin, LOW);
}

void vibe2() {
int i;
for(i = 0; i < 50000; i++) {
digitalWrite(bigVibePin, HIGH);
}
digitalWrite(bigVibePin, LOW);
}

void box1() {
int i;
for(i = 0; i < 30000; i++) {
digitalWrite(soundBoxPin, HIGH);
}
digitalWrite(soundBoxPin, LOW);
}

Do you know if there’s a way to clear arrays?

S

The two easiest ways to clear an array are a loop with an assignment and the memset function

#include <string.h>

void * memset ( void * dest,
int val,
size_t len
)

Fill memory with a constant byte.

The memset() function fills the first len bytes of the memory area pointed to by dest with the constant byte val.

Returns:
The memset() function returns a pointer to the memory area dest.

There are a couple of things I would try.

Increase the size of your storage array by a couple of bytes without changing anything else. If this solves or changes the symptoms than it is an excellent indicator that you have a memory overrun in your code.

Clear the contents of the array prior to reading a new tag

It might be worthwhile to pair down the functionality of your code to just the component that is having the problem–tag reading. To try and diagnose the problem within a simpler framework.

It looks like you might be able to send message through the serial port to a display device (computer). I would suggest sending prints of the associated variables before and after sections (one at a time) to verify that the code is doing what you expect. For the array it might be helpful to print the entire array in hex rather than as a string.

If your serial port is tied up, I would use the software serial library to send the debug output.

Hi,

thank’s for the advice. I haven’t been able to work with the code for a while and today, when trying to get my head around the memset function you mentioned, the problem turned out to be not in the detection but in some of the feedback voids I’ve created. Some of them send to much data to the port, apparently, creating an overload that crashes the reading tags function. When cutting that code down it works!
Thank’s anyway :slight_smile:
I think the memset to clear arrays might come handy if I increase the number of tags in the future.

Best Wishes and thanks again…

Sara