Error in programming RFID reader

I am trying to read the ID of a card with an RFID reader, connected to arduino. I want to read the ID and store it to the progmem.
The main task is that the arduino shpould activate a relay when a card is read. When the same card is read again, it should deactivate. If another card is scanned, while activated, this one should not have an effect.
Therefore I want to store the ID in the memory and then compare this with the next ID of the next card and than look if there should be an action of the relay.
For the moment I only have code of the part of the reading and the storage, but there is an error when I want to write it to the memory…

// RFID reader for Arduino 
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> 
// Modified for Arudino by djmatic

#include <avr/pgmspace.h>
#define RELAY1  6  
int  val = 0; 
char code[9]; 
char myChar[9];
char codememory[9];
int bytesread = 0;
int value = 0;
int k;


void setup() { 

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
digitalWrite(2, LOW);                  // Activate the RFID reader
pinMode(RELAY1, OUTPUT);   //Use a relay as output to turn on and of solenoid
pinMode(13, OUTPUT); //the led
}  


 void loop() { 

  if(Serial.available() > 0) {          // if data available from reader 
 
    if((val = Serial.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {            // read 10 digit code 
       
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        }
      }      
      if(bytesread == 10) {              // if 10 digit read is complete 
        Serial.print("TAG code is: ");   // possibly a good TAG 
        Serial.println(code);            // print the TAG code 
      } 
      bytesread = 0; 
      
      digitalWrite(2, HIGH);                 // deactivate the RFID reader for a moment so it will not flood
      delay(1500);                           // wait for a bit 
      digitalWrite(2, LOW);                  // Activate the RFID reader
     
     char codememory[9] PROGMEM  = {code};
     Serial.print("The code saved is: ");
     Serial.println(codememory);
     //digitalWrite(13, HIGH);
      }
         
    for (k = 0; k < strlen(codememory); k++)
  {
    myChar =  pgm_read_byte_near(codememory + k);
    Serial.print("The code read from memory is: ");
  }
  Serial.println(myChar);        
    
  }
 }

What is this line going to do?

char codememory[9] PROGMEM  = {code};

You can not use program memory from within a program, it is for set up only.

You want to use EEPROM for this. http://www.arduino.cc/en/Reference/EEPROM

Read how to use this forum sticky and edit that first post to include code tags before posting again.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” button above the posting area (It looks like a scroll with < > inside it).

How to use this forum

Plus what Mike said.

I can't use the flash memory? I did this because my teacher advised it to me. Before I was using EEPROM indeed but then there were other errors..

I can't use the flash memory? I did this because my teacher advised it to me.

No you can't. Your teacher is mistaken in his advice.

Code to write to flash memory can only run from the bootloader area of memory. The bootloader does not support that currently, altho the question does come up from time to time. See this part of the datasheet:

29.2 Application and Boot Loader Flash Sections The Flash memory is organized in two main sections, the Application section and the Boot Loader section (see Figure 29-2 on page 312). The size of the different sections is configured by the BOOTSZ Fuses as shown in Table 29-7 on page 320 and Figure 29-2 on page 312. These two sections can have different level of protection since they have different sets of Lock bits. 29.2.1 Application Section The Application section is the section of the Flash that is used for storing the application code. The protection level for the Application section can be selected by the application Boot Lock bits (Boot Lock bits 0), see Table 29-2 on page 313. The Application section can never store any Boot Loader code since the SPM instruction is disabled when executed from the Application section. 29.2.2 BLS – Boot Loader Section While the Application section is used for storing the application code, the The Boot Loader software must be located in the BLS since the SPM instruction can initiate a programming when executing from the BLS only. The SPM instruction can access the entire Flash, including the BLS itself. The protection level for the Boot Loader section can be selected by the Boot Loader Lock bits (Boot Lock bits 1), see Table 29-3 on page 313.

stdavid: I can't use the flash memory? I did this because my teacher advised it to me. Before I was using EEPROM indeed but then there were other errors..

Your teacher may well have meant to store the "correct" ID into flash memory, and comparing incoming ones to that, which is feasible.

Before I was using EEPROM indeed but then there were other errors..

You can change the contents of EEPROM. Don't throw out all the code just because you got an error. Fix the error.