Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

Odd results coming from a char* array

$
0
0

Hi all

I'm currently working with Eelcor's excellent port of the RC522 Mifare library.

All is well on the hardware side and most of the code is functioning fine, there's just one section where I'm trying to use a switch/case based on the results of an array lookup (as for loop).

The switch works comparing an integer but the value I want is in a char* array which is the only way I can get the array working with the data I want.

The funny thing occurs when I try to get a variable to take on the int value of the array. The number always comes out as a negative but can also change, I think based on the amount of code but I'm not sure.

I'm positive I'd just being dense and it's something simple but I've gone over it several times and can't figure it out.

Any help appreciated

 

Cheers

Dean

/*
	Example file for communicating with the NFRC522. The program prints the card data.
	Created by Eelco Rouw - Originally adapted by Grant Gibson. 
*/

// Pinout
// SDA  - 2.2
// SCK  - 1.5
// Mosi - 1.7
// Miso - 1.6
// IRQ  - NC
// GND  - GND
// RST  - 1.3
// VCC  - VCC

#include <Mfrc522.h>

// the sensor communicates using SPI, so include the library:
#include <SPI.h>

int chipSelectPin = 10;
int NRSTDP = 5;
int ledpin1 = 13;
int ledpin2 = 12;
int ledpin3 = 11;
int cardint = 5;

Mfrc522 Mfrc522(chipSelectPin,NRSTDP);
unsigned char serNum[5];
int secLevel;
String secLevel2;
unsigned char sectorKey[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char readData[16];

void setup() 
{                
	Serial.begin(9600);                       // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
	// Start the SPI library:
	SPI.begin();
	// Initialize the Card Reader
	digitalWrite(chipSelectPin, LOW);
	pinMode(RED_LED, OUTPUT);
        pinMode(ledpin1, OUTPUT);
        pinMode(ledpin2, OUTPUT);
        pinMode(ledpin3, OUTPUT);
        digitalWrite(ledpin1, LOW);
        digitalWrite(ledpin2, LOW);
        digitalWrite(ledpin3, LOW);
	Mfrc522.Init();  
}

void loop()
{
	unsigned char i,tmp;
	unsigned char status;
	unsigned char str[MAX_LEN];
	unsigned char RC_size;
	unsigned char blockAddr;
        String mycardno;
        char* myarray[3][5]={  //Array of tags and related names, tag numbers copied from serial console after 1st run
          {"205928944228","1251291514471","7748944415","157172894468","611918944247"},
          {"Dad","Mum","Baby","Dog","Cat"},
          {"5","4","3","2","1"}
          };
  	
	status = Mfrc522.Request(PICC_REQIDL, str);	
	if (status == MI_OK)
	{
		Serial.println("Card detected");
		Serial.print(str[0],BIN);
		Serial.print(" , ");
		Serial.print(str[1],BIN);
		Serial.println(" ");
	}

	status = Mfrc522.Anticoll(str);
	memcpy(serNum, str, 5);
	if (status == MI_OK)
	{
		Serial.println("The card's number is  : ");
		Serial.print(serNum[0], DEC);
		Serial.print(" , ");
		Serial.print(serNum[1], DEC);
		Serial.print(" , ");
		Serial.print(serNum[2], DEC);
		Serial.print(" , ");
		Serial.print(serNum[3], DEC);
		Serial.print(" , ");
		Serial.print(serNum[4], DEC);
		Serial.println(" ");
                mycardno = String(serNum[0]) += String(serNum[1]) += String(serNum[2]) += String(serNum[3]) += String(serNum[4]); // Appends the content of the serNum array to give a unique card no
                Serial.println(mycardno);
                Mfrc522.SelectTag(serNum);
                status = Mfrc522.Auth(PICC_AUTHENT1A,1,sectorKey,serNum);
                if (status == MI_OK)
                {
                  Serial.println("Authenticated...\r\n");
                } else
                {
                  Serial.println("Error authenticating...\r\n");
                }
                
                status = Mfrc522.ReadBlock(1, readData);
                if (status == MI_OK)
                {
                  for(i=0; i<16; i++)
                  {                  
                    Serial.write(readData[i]);
                    delay(10);
                  }                  
                } else {
                  Serial.println("Error reading.");
                }
                int i;
                for(i = 0; i < 5; i = i + 1)
                {
                  if (mycardno == myarray[0][i])
                  {
                  Serial.print("Hello ");
                  Serial.println(myarray[1][i]);        // Should Print out name matching tag
                  secLevel = int(myarray[2][i]);        // Tried setting this to the int of the unsigned char
                  secLevel2 = String(myarray[2][i]);    // Setting the string version, works better
                  Serial.print("Security level: ");
                  Serial.println(myarray[2][i]);        // Returns correctly
                  Serial.println(secLevel);             // Returns random number based on current amount of code
                  Serial.println(secLevel2);            // Returns correctly
                }
                /*else {
                  Serial.println("Bugger Off");
                }*/
                }
                {
                  switch (secLevel) {
                    case -9844:
                    digitalWrite(ledpin1, HIGH);
                    digitalWrite(ledpin2, LOW);
                    digitalWrite(ledpin3, LOW);
                    break;
                    case -9842:
                    digitalWrite(ledpin1, LOW);
                    digitalWrite(ledpin2, HIGH);
                    digitalWrite(ledpin3, LOW);
                    break;
                    case -9840:
                    digitalWrite(ledpin1, LOW);
                    digitalWrite(ledpin2, LOW);
                    digitalWrite(ledpin3, HIGH);
                    break;
                    case -9838:
                    digitalWrite(ledpin1, HIGH);
                    digitalWrite(ledpin2, LOW);
                    digitalWrite(ledpin3, HIGH);
                    break;
                    case -9836:
                    digitalWrite(ledpin1, HIGH);
                    digitalWrite(ledpin2, HIGH);
                    digitalWrite(ledpin3, LOW);
                    break;
                  }
                }
  
		delay(1000);
                Mfrc522.Init();	
	}
	//Serial.println(" ");
	Mfrc522.Halt();	                        
}

Viewing all articles
Browse latest Browse all 2077

Trending Articles