Newbie Question about using a Function

Hello,

i am very new in Arduino, but have some expierience in C#.

So i have my Arduino Mega connected and running. I need them to run Encoders.
For that i have includet this lib: Rotary Lib

It runs good so far.

Bevore the setup funktion i have created my rotary object:
Rotary r_nav1_inner = Rotary(2, 3);

Then in the Setup function i start the Encorder:
r_nav1_inner.begin(true);

in the loop i call the funtion for the encoder:
process_nav1_inner_enc();

This is the funktion:

void process_nav1_inner_enc()
{
	unsigned char result = r_nav1_inner.process();
	
	if (result) {
		Serial.println("process_nav1_inner_enc");
		Serial.println(result);
		client.println("Arn.Resp:13=0");
		switch (result)
		{
			case 16:
				client.println("Arn.Resp:13=1");
				break;
			case 32:
				client.println("Arn.Resp:13=-1");
				break;
		}
	}
}

But now i want to optimize the code a little bit and process all encoders in only one funktion.
For that i have changed this very little funktion to:

void process_encoders(Rotary r, String sv)
{
	unsigned char result = r.process();
	if (result) {
		Serial.println("process_encoders");
		Serial.println(result);
		client.println("Arn.Resp:" + sv + "=0");
		switch (result)
		{
			case 16:
			client.println("Arn.Resp:" + sv + "=1");
			break;
			case 32:
			client.println("Arn.Resp:" + sv + "=-1");
			break;
		}
	}
}

For that i changed the call for the funktion to this:
process_encoders(r_nav1_inner, sv_nav1_enc_inner);

But this dosent work.

But when i change the line
unsigned char result = r.process();
back to
unsigned char result = r_nav1_inner.process();

It works again.

I dont understand why?
In Cä i do that every time like this.
But in Arduino it seams not to work.

Can anyone give me a tip what i a doing wrong here ?

Thanks
Matthias

What is the data type of sv_nav1_enc_inner ?
Where and when is it declared and given a value ?

Please post a complete program

You're passing the Rotary object by value. The local variable r will be a copy of the global variable r_nav1_inner. Inside of your function, you call the process function to update r, but at the end of the function, the local variable r is destroyed, and r_nav1_inner is never updated.

Pass r by reference. Also pass sv by const reference to avoid unnecessary copies.

void process_encoders(Rotary &r, const String &sv) {
 ...
}

Pieter

Hello,
the other variable is a string.

But i changed it to PieterP's code with the &, and now it works like a charme !!

Its a bit different to C# :slight_smile:

Thanks a lot !!
Matthias

Hello again,

the script is working fine now.

But there is now another Problem.
As describet above i use this Rotary Lib.

Now i have 3 different types of Encoders it seams.
On the picture,

With my script, when i turn

-the left encoder 1 step it prints 2 time in the Serial Output

  • the middle encoder 1 step it prints one times
  • the right encoder prints only after 2 steps one serial output.

Have you any ideas what i can do against this ?

Matthias

FltAlt_Enc.ino (5.2 KB)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.