// 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};
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.
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
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.