Running out of memory for (seemingly) no reason!


here's code. reason i'm running out of code. please take gander. thanks!

code: [select]
#include <softwareserial.h>

#include <sd.h>

softwareserial myserial(2, 4);

int resetpin = 3;  //reader reset pin.
int ledpin = 7;  //status led pin.
int cs_pin = 10;

void setup() {

  serial.begin(9600);  //initialize main serial connection.
  myserial.begin(9600);  //initialize reader serial connection.
  pinmode(resetpin, output);  //define reset pin output.
  pinmode(ledpin, output);  //define led pin output.
  pinmode(cs_pin, output);
  digitalwrite(resetpin, high);  //pull reset pin high.
  serial.println("initializing sd card");

  if (!sd.begin(cs_pin)) {
    serial.println("card failed. quitting");
    return;
  }

  serial.println("sd card ready");
  serial.println("waiting tag");
}

void loop() {

  char readbyte, tempbyte, addbyte[13], tagstring[13], mastertag[13] = {
    '4', 'e', '0', '0', '0', '4', '3', 'd', '3', '8', '4', 'f'        };
  int index = 0, filesize;
  boolean reading = false, cardreading = false, cardready = false;
  file logread = sd.open("log.txt", file_write);

  while (myserial.available() > 0) {

    readbyte = myserial.read();

    if(readbyte == 2) reading = true;
    if(readbyte == 3) reading = false;

    if(reading && readbyte != 2 && readbyte != 10 && readbyte != 13){
      tagstring[index] = readbyte;
      index ++;
    }
  }

  printtag(tagstring);

  if (strlen(tagstring) != 0) {
    if (database(tagstring) == true) {
      serial.println("match found");
      digitalwrite(ledpin, high);
      delay(1000);
      digitalwrite(ledpin, low);
    }
    else {

      serial.println("no match found");
    }

  }

  if (compare(mastertag, tagstring)) {
    serial.println("master tag found, entering admin mode");
    resetreader();
    index = 0;
    delay(3000);

    while (cardready == false) {
        while (myserial.available() > 0) {
          tempbyte = myserial.read();

          if (tempbyte == 2) cardreading = true;
          if (tempbyte == 3) cardreading = false;

          if (cardreading && tempbyte != 2 && tempbyte != 10 && tempbyte != 13) {
            addbyte[index] = tempbyte;
            index ++;
          }
        }
        if (index == 12) {
          serial.println("done reading");
          filesize = logread.size();
          logread.seek(filesize);
          logread.print(addbyte[0]);
          logread.print(addbyte[1]);
          logread.print(addbyte[2]);
          logread.print(addbyte[3]);
          logread.print(addbyte[4]);
          logread.print(addbyte[5]);
          logread.print(addbyte[6]);
          logread.print(addbyte[7]);
          logread.print(addbyte[8]);
          logread.print(addbyte[9]);
          logread.print(addbyte[10]);
          logread.print(addbyte[11]);
          logread.close();
          cardready = true;
        }
    }
  }
  cleartag(tagstring);
  resetreader();
  delay(200);
}

void resetreader() {
  digitalwrite(resetpin, low);
  digitalwrite(resetpin, high);
  delay(150);
}

boolean compare(char one[], char two[]) {

  (int = 0; < 12; i++) {
    if (one[i] != two[i]) {
      return false;
    }
  }
  return true;
}

void cleartag(char one[]) {

  (int = 0; < strlen(one); i++) {
    one[i] = 0;
  }
}

void printtag(char one[]) {

  int printer = 0;

  if (!strlen(one) == 0) {

    (int = 0; < 12; i++) {
      serial.print(one[i]);
      printer++;
    }
    if (printer == 12) {
      serial.println();
    }
  }
}

boolean database(char one[]) {

  char sdarray[13];
  boolean match = false;
  file logread = sd.open("log.txt", file_write);

if (logread) {
    logread.seek(0);
    while (logread.available() && match == false) {
      (int = 0; < 12; i++) {
        sdarray[i] = logread.read();
      }

      if (compare(one, sdarray) && match == false) {
        match = true;
        logread.close();
        return true;
      }
    }

    if (match == false && !logread.available()) {
      logread.close();
      return false;
    }
  }
}

moderator edit: thread locked. don't start new topic when asked same question in thread. thanks. (nick gammon)

http://arduino.cc/forum/index.php/topic,100364


Arduino Forum > Using Arduino > Programming Questions > Running out of memory for (seemingly) no reason!


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial