error message: " **** was not declared in this scope "

Hey guys, I'm working on a fairly simple sketch that reads some data addresses (#3-33) on an RFID card, printing the results to the monitor. I had another variant of this sketch working before, but I am trying to make it a little more elegant and have run into a problem. Whenever i try to compile the sketch, I receive the error message:

FINAL_read_whole_card_with_checking:3: error: 'whichSpace' was not declared in this scope
FINAL_read_whole_card_with_checking:31: error: redefinition of 'int readCard'
FINAL_read_whole_card_with_checking:3: error: 'int readCard' previously defined here

I have read and re-read my code, but I can't figure out what the IDE isn't liking. My code is below (sorry that it isn't formatted nicely, but that's another problem I'm having: clicking "copy for forum" returns "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 40")

Here's my code:

#include <NewSoftSerial.h>
#define RFID_READ 0x01
#define txPin 6
#define rxPin 8

int firstByte;
int secondByte;
int thirdByte;
int fourthByte;
int whichSpace;

NewSoftSerial mySerial(rxPin, txPin);

void setup()
{
  Serial.begin(9600);
  Serial.println("Read RFID addresses #3-33:");
  mySerial.begin(9600);
  pinMode(txPin, OUTPUT);     
  pinMode(rxPin, INPUT);      
}

void suppressAll()                            
{
  if(mySerial.available() > 0)
  { 
    mySerial.read();
    suppressAll();
  }
}

int readCard(whichSpace)
{
   mySerial.print("!RW");
   mySerial.print(RFID_READ, BYTE);
   mySerial.print(whichSpace, BYTE);

   if(mySerial.available() > 0) 
   {   
     val = mySerial.read();                        
     if (val != 1)                            
     {
       suppressAll();
       read(whichSpace);
     }   
  first = mySerial.read();
  second = mySerial.read();   
  third = mySerial.read();
  fourth = mySerial.read();
}
}

void loop()
{
  for (whichSpace = 3; whichSpace <= 33; whichSpace++)
  { 
    readCard(whichSpace)
    
    if(mySerial.available() > 0) {       
        val = mySerial.read();
        Serial.print(whichSpace);
        Serial.print(":"); 
        Serial.print(firstByte, DEC);
        Serial.print(" , ");
      }

      if(mySerial.available() > 0) {        
        val = mySerial.read();
        Serial.print(secondByte, DEC);
        Serial.print(" , ");
      }

      if(mySerial.available() > 0) {       
        val = mySerial.read();
        Serial.print(thirdByte, DEC);
        Serial.print(" , ");
      }

      if(mySerial.available() > 0) {          
        val = mySerial.read();
        Serial.println(fourthByte, DEC);
      }   

      delay(100);
    }
  }
}

Thanks for any/all help you can give!

You are defining a function with a parameter supposedly passed to it that exists already. Any parameter you pass must be unique in the function therefore you need to do at least:-
int readCard(int whichSpace) {
Note that the variable whichSpace used inside the function will not be the same as that defined outside to function. Is that what you want?

Change

int readCard(whichSpace)

to

int readCard(int whichSpace)

To get rid of the whichSpace error.

As Grumpy_Mike already pointed out, you're using two sets of whichSpace - because this one was 'hiding' the global one you defined at the top, the function saw it as a new variable, and since you didn't say specifically that it was an int, it complained.

However, you have another issue: doing duplicate variables like this makes for confusing code. You should rename one, or better yet get rid of one:

  • get rid of the global entry at the top, in which case means adding this to the start of loop():
int whichSpace=0;

or if the global value is needed, get rid of the parameter to int readCard(int whichSpace):

int readCard(void)

Also, the loop() has too many '}' braces in it.

thanks for the responses guys. I've got it working now...adding the int in front of whichspace did the trick.

int readCard(int whichSpace)
[code]Warning:  This code will PROBABLY work but since it calls itself recursively it might crash the stack:

{code]
void suppressAll()                            
{
  if(mySerial.available() > 0)
  { 
    mySerial.read();
    suppressAll();
  }
}

This version has the same effect without the same risk:

void suppressAll()                            
{
  while (mySerial.available() > 0)
  { 
    mySerial.read();
  }
}

[/code]

thanks for the tip john, but that is not an issue. The recursion is limited by the

 if(mySerial.available() > 0)

The way the RFID reader works, there are 5 bytes reported when an address on the card is read: the first is a status byte (OK or error) and then 4 data bytes. So the recursion will only run 5 times, and then mySerial.available (which does just what you would think, reports the number of unread bytes) will be equal to zero, breaking the loop.

thanks for the tip john, but that is not an issue

There is an issue: the recursive version uses more stackspace, which could result in out of RAM (very small chance OK). Therefor Johns iterative version is to be prefered.