Beginning Code Error Message

I keep receiving an error message "error: 'distance' was not declared in this scope" regarding the line with the table entries in them.

/* ir-distance sketch

  • prints distance and changes LED flash rate based on distance from IR sensor
    */

const int ledPin = 13; // the pin connected to the LED to flash
const int sensorPin = 0; // the analog pin connected to the sensor

const long referenceMv = 5000; // long int to prevent overflow when multiplied

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop()
{
int val = analogRead(sensorPin);
int mV = (val * referenceMv) / 1023;

Serial.print(mV);
Serial.print(",");
int cm = getDistance(mV);
Serial.println(cm);

digitalWrite(ledPin, HIGH);
delay(cm * 10); // each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay(cm * 10);

delay(100);
}

// the following is used to interpolate the distance from a table
// table entries are distances in steps of 250 millivolts
const int NBR_ELEMS=10;
const int firstElement = 250; //first entry is 250 mV
const int interval = 250; //millivolts between each element
static int distance[TABLE_ENTRIES] = {150,140,130,100,60,50,40,35,30,25,20,15};

int getDistance(int mV)
{
if( mV > INTERVAL * TABLE_ENTRIES )
return distance[TABLE_ENTRIES-1]
else
{
int index = mV / INTERVAL;
float frac = (mV % 250) / (float)INTERVAL;
return distance[index] - ((distance[index] - distance[index+1]) * frac);
}
}

How do I fix the problem?

That code breaks here for a different reason: TABLE_ENTRIES is not defined.

If you share the actual code that generated that error you might get better help.

-br

The code is from a book, O'Reilly's "Arduino Cookbook."

How would I define TABLE_ENTRIES? Or is it perhaps the case I'm not copying the code correctly?

How would I define TABLE_ENTRIES?

Just like in the book.

BaldisEmpire:
is it perhaps the case I'm not copying the code correctly?

The code is clearly incomplete (NBR_ELEMENTS is defined but not used, and TABLE_ENTRIES is used but not defined), so either you have copied it incorrectly or the original source was faulty. (Your error message "'distance' was not declared in this scope" was a side effect of the first error about TABLE_ENTRIES not being defined). If you add a definition for TABLE_ENTRIES then you get a further error about INTERVAL not being defined, and a missing semicolon.

It's not like O'Reilly to publish code this bad - are you sure this is the code exactly as O'Reilly published it? To me it looks more like working code that somebody has then hacked about.

I don't think it's O'Reilly's mistake.

I'll review and get back to you.

It's probably best I start from the beginning of the book, working through minor projects, before trying what's halfway through the book.

Otherwise noob.

In all of my years I've never seen a reply of this Calibre.. I am truly Impressed.

I don't think it's O'Reilly's mistake.

I'll review and get back to you.

It's probably best I start from the beginning of the book, working through minor projects, before trying what's halfway through the book.

Otherwise noob. No, a Student.

That kind of personal honesty is wonderful to see.

Doc

BaldisEmpire:
I keep receiving an error message "error: 'distance' was not declared in this scope" regarding the line with the table entries in them.

I got a whole lot of other errors.

sketch_apr15b:39: error: 'TABLE_ENTRIES' was not declared in this scope
sketch_apr15b.ino: In function 'int getDistance(int)':
sketch_apr15b:43: error: 'INTERVAL' was not declared in this scope
sketch_apr15b:43: error: 'TABLE_ENTRIES' was not declared in this scope
sketch_apr15b:44: error: 'distance' was not declared in this scope
sketch_apr15b:45: error: expected ';' before 'else'
sketch_apr15b:49: error: 'distance' was not declared in this scope

How to use this forum

Code tags. Error messages.

BaldisEmpire:
The code is from a book, O'Reilly's "Arduino Cookbook."

Judging by this excerpt I found:

Either you didn't key it in correctly, or they have amended the book.

Docedison, thank you, but...

I chalk it up to the Singularity. Submit to it and it will submit to us. But, such is a topic for another place.

Either way, I'm still confused by your replies.

I'm starting with the beginning of the book and will be back in a few years.

Remember, variable names are case sensitive, so "interval" and "INTERVAL" are different variables. Also, in the code that you pasted, it seems that "NBR_ELEMS" is where "TABLE_ENTRIES" should be.

Based on this, I have a question to the more seasoned coders here. The declarations for the table stuff appears to be in a global scope, outside of any functions, including setup() and loop(). But they are actually written after the setup() and loop() functions. Is this still valid, or should theses declarations be written in his code before the setup() and loop() functions? (Is the order of those two functions actually important, or does it just make sense for reading purposes?)

The declarations for the table stuff appears to be in a global scope, outside of any functions, including setup() and loop(). But they are actually written after the setup() and loop() functions. Is this still valid, or should theses declarations be written in his code before the setup() and loop() functions?

Outside of any function means before any functions, between two functions, after all functions, or any combination of the above.