user88587: I want to write a class for a model bike.
For controlling the steering I use a DC motor with two encoders.
To get the steering angle stAng of the bike, I attach a interrupt to the pin ST_ENC_A where one of the encoders is connected to. This interrupt is attached when the bike is initialized with the function bike.begin(). When the interrupt is triggerd, the increment-steering-angle-function incStAng is triggerd.
Said function is member of the class Bike. To make the whole code compile and upload I had to make the increment-steering-angle-function static void incStAng() and the steering-angle-variable volatile static float stAng static.
The code is running, there are no errors or warnings.
Now, when I provide pin ST_ENC_A with 3.3V the interrupt should be triggerd and I should see the printed value of stAng increse in the Serial Monitor. This is not the case. It stays at 0.00.
This is my first real class I am writing. I only want to create one instance of my bike so the static definition of stAng and incStAng should be no problem.
So far I found the following websites helpful for information on 'static' and ISR.
I would be glad if someone could help me and give me a hint why the steering angle is not increasing when I give said pin 3.3V!
Board: Arduino Nano RP2040 Connect
Below you will see the .ino, .h, .cpp files in this order.
// myBike.ino - main file

#include "Bike.h"

Bike bike;

void setup() {
  Serial.begin(9600);
  bike.begin();
}

void loop() {
  float val = bike.getStAng();
  Serial.println(val);
}

// Bike.h - header file for the Bike library

#ifndef Bike_h
#define Bike_h

#include "Arduino.h"

class Bike {
  public:
    Bike();
    void begin();
    float getStAng();
  private:
    volatile static float stAng;
    static void incStAng();
};

#endif

// Bike.cpp - implementation file for the Bike library

#include "Bike.h"

#define ST_ENC_A      12
#define ST_ENC_B      11

#define DEG_PER_CNT   0.51428571428

Bike::Bike() {
  pinMode(ST_ENC_A, INPUT);
  pinMode(ST_ENC_B, INPUT);
}

void Bike::begin() {
  attachInterrupt(digitalPinToInterrupt(ST_ENC_A),incStAng,RISING);
}

void Bike::incStAng() {
  volatile float stAng;
  int b = digitalRead(ST_ENC_B);
  if(b==HIGH){
    stAng = stAng - DEG_PER_CNT;
  }else{
    stAng = stAng - DEG_PER_CNT;
  }
}

float Bike::getStAng() {
  volatile float stAng;
  return stAng;
}




user7508: You have three different variables named stAng here:

a static member of the Bike class, which has been declared but not
defined, and is never used
a local variable of the Bike::incStAng() method
a local variable of the Bike::getStAng() method

There should be only one such variable. For this, you should define the
first one: in Bike.cpp
volatile float Bike::stAng;

and get rid of the other two.
Side note: I would recommend avoiding floating point computations within
an ISR, as these can be quite slow. Instead, count the integer number
of encoder steps in the ISR, and convert to an angle within
Bike::getStAng().

88587: <p>I want to write a class for a model bike.</p>
<p>For controlling the steering I use a DC motor with two encoders.
To get the steering angle <code>stAng</code> of the bike, I attach a interrupt to the pin <code>ST_ENC_A</code> where one of the encoders is connected to. This interrupt is attached when the bike is initialized with the function <code>bike.begin()</code>. When the interrupt is triggerd, the increment-steering-angle-function <code>incStAng</code> is triggerd.
Said function is member of the class <code>Bike</code>. To make the whole code compile and upload I had to make the increment-steering-angle-function <code>static void incStAng()</code> and the steering-angle-variable <code>volatile static float stAng</code> static.</p>
<p>The code is running, there are no errors or warnings.</p>
<p>Now, when I provide pin <code>ST_ENC_A</code> with 3.3V the interrupt should be triggerd and I should see the printed value of <code>stAng</code> increse in the Serial Monitor. This is not the case. It stays at 0.00.</p>
<p>This is my first real class I am writing. I only want to create one instance of my bike so the static definition of <code>stAng</code> and <code>incStAng</code> should be no problem.</p>
<p>So far I found the following websites helpful for information on '<a href="https://stackoverflow.com/questions/31305717/member-function-with-static-linkage">static</a>' and <a href="https://forum.arduino.cc/t/using-a-class-member-for-isr-is-it-possible/380127" rel="nofollow noreferrer">ISR</a>.</p>
<p>I would be glad if someone could help me and give me a hint why the steering angle is not increasing when I give said pin 3.3V!</p>
<p>Board: Arduino Nano RP2040 Connect</p>
<p>Below you will see the .ino, .h, .cpp files in this order.</p>
<pre><code>// myBike.ino - main file

#include &quot;Bike.h&quot;

Bike bike;

void setup() {
  Serial.begin(9600);
  bike.begin();
}

void loop() {
  float val = bike.getStAng();
  Serial.println(val);
}
</code></pre>
<pre><code>// Bike.h - header file for the Bike library

#ifndef Bike_h
#define Bike_h

#include &quot;Arduino.h&quot;

class Bike {
  public:
    Bike();
    void begin();
    float getStAng();
  private:
    volatile static float stAng;
    static void incStAng();
};

#endif
</code></pre>
<pre><code>// Bike.cpp - implementation file for the Bike library

#include &quot;Bike.h&quot;

#define ST_ENC_A      12
#define ST_ENC_B      11

#define DEG_PER_CNT   0.51428571428

Bike::Bike() {
  pinMode(ST_ENC_A, INPUT);
  pinMode(ST_ENC_B, INPUT);
}

void Bike::begin() {
  attachInterrupt(digitalPinToInterrupt(ST_ENC_A),incStAng,RISING);
}

void Bike::incStAng() {
  volatile float stAng;
  int b = digitalRead(ST_ENC_B);
  if(b==HIGH){
    stAng = stAng - DEG_PER_CNT;
  }else{
    stAng = stAng - DEG_PER_CNT;
  }
}

float Bike::getStAng() {
  volatile float stAng;
  return stAng;
}


</code></pre>
 7508: <p>You have <em>three</em> different variables named <code>stAng</code> here:</p>
<ul>
<li>a static member of the <code>Bike</code> class, which has been declared but not
defined, and is never used</li>
<li>a local variable of the <code>Bike::incStAng()</code> method</li>
<li>a local variable of the <code>Bike::getStAng()</code> method</li>
</ul>
<p>There should be only one such variable. For this, you should define the
first one: in Bike.cpp</p>
<pre class="lang-cpp prettyprint-override"><code>volatile float Bike::stAng;
</code></pre>
<p>and get rid of the other two.</p>
<p>Side note: I would recommend avoiding floating point computations within
an ISR, as these can be quite slow. Instead, count the <em>integer</em> number
of encoder steps in the ISR, and convert to an angle within
<code>Bike::getStAng()</code>.</p>
