Encoder.h myEnc.write(32) doesn't compile error: 'myEnc' does not name a type

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.

Dazz

The line:

myEnc.write(32);

is not inside of a function.

ToddL1962:
The line:

myEnc.write(32);

is not inside of a function.

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.

Dazz

Hi
Within Encoder.h, the read write methods are declared as:

#ifdef ENCODER_USE_INTERRUPTS
	inline int32_t read() {
		if (interrupts_in_use < 2) {
			noInterrupts();
			update(&encoder);
		} else {
			noInterrupts();
		}
		int32_t ret = encoder.position;
		interrupts();
		return ret;
	}
	inline void write(int32_t p) {
		noInterrupts();
		encoder.position = p;
		interrupts();
	}

so with my limited understanding:

int n = myEnc.read(); // works and

myEnc.write(n);       // should also work but doesn't compile.

Dazz

All executable code must be contained within a function.

Hi

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.

I had:

void setup() {
...
}

myEnc.write(32);


void loop() {
...
}

whereas:

void setup() {
  ...
  myEnc.write(32);
  ...
}



void loop() {
  ...
  myEnc.write(32);
  ...
}

both work.

Problem solved.

Cheers.

Dazz

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.

Hi

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.

Dazz

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.

On the other hand,

bool checkTheButton() {}

.. is a function which returns a boolean.

Hi

I am too old to remember when I last used BASIC but In Perl, the terms function, subroutine, and method are the same but in some programming languages, these are considered different.

For Java There is no function term in java. We Generally call them method. While in other language like c++ it is called a function.

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.

Dazz