Hi
I want to use the Encoder library by Paul Stoffregen. This has been around for a long time so it is unlikely to contain bugs.
I have a project Test Encoder stored in directory Aduino/Test Encoder.
The library files are stored in the directory Aduino/libraries/Encoder.
I want to be able to read and write the encoder value.
The encoder library includes read/write commands.
The following code does nothing but demonstrate the problem.
The code will not compile with the myEnc.write(32); line.
#include <Encoder.h>
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// the pins connected to the encoder.
Encoder myEnc(2,3);
void setup() {
}
myEnc.write(32);
void loop() {
long newPosition = myEnc.read();
}
My reading indicates that the error message is because the library path and file location is not standard. I have checked that the path is OK. If there was a problem with the file path, I would expect the myEnc.read() command would also raise an error message. It doesn't.
I am assuming that the code is right but I am doing something wrong. I can't figure out what that is.
OK so I don't see why it should be inside a function. A function would have an output. My interpretation is that the argument in the write command is an input with a void return.
This is the first time I have coded in C so I suspect I am missing key knowledge about command structure.
I don't do cryptic crosswords, so it took me a while to figure out how to apply the advice received to fix my mistake.
For the benefit of those that might read this is the future, the sample code above didn't compile because the statement was outside of the setup and loop functions.
dazz100:
I don't do cryptic crosswords, so it took me a while to figure out how to apply the advice received to fix my mistake.
How is All executable code must be contained within a function. "cryptic crosswords"? You have a line of executable code myEnc.write(32); outside of a function (e.g., loop()). Declarations, definitions, functions, and code are rather simple terms that need to be understood in order to program at the most basic levels.
I am not a pro programmer but I have used a variety of languages since the 1970's. Other languages I have used, define a function as something with an output. On that basis, I did not recognise loop() or setup() as functions. So telling me that all executable code needs to be inside a function was cryptic.
I do appreciate the time and effort people donate to help people like me. I understand that the explanations were technically correct. What I didn't understand was how to apply the explanations to solve my problem. I am an old dog trying to learn new tricks.
If someone had said I need to move the statement inside the scope of loop() or setup(), which are defined as functions in C, that I would have understood and applied.
dazz100:
Other languages I have used, define a function as something with an output.
I had to refresh my memory on that one, and found this which says that in Basic,
a function returns a value, where a subroutine just repeats lines of code
That distinction is unnecessary here, where the oops edit () word in front of the function name tells us the return type, and if it says "void" there is none, making it a "subroutine" not a "function" by the quoted terminology for Basic.
So..
void setup() {}
//and
void loop()
.. are both functions but neither returns anything.
Which only shows that the definition of a function, subroutine, method, class etc is specific to each language. So although the answers received were technically correct for C, they were also ambiguous and therefore cryptic. It wasn't until I looked up the C specific definition of a function that I understood the answers. Without the answers received on this forum, it would have taken me a lot longer to find my mistake.