Skip to main content
Arduino

Return to Question

Commonmark migration
Source Link

#Edit: Per Nick Gammon's answer, I now have this.

Edit: Per Nick Gammon's answer, I now have this.

#Edit: Per Nick Gammon's answer, I now have this.

Edit: Per Nick Gammon's answer, I now have this.

Updated question to reflect suggestions in answer.
Source Link
Marcel
  • 181
  • 4
  • 11

#Edit: Per Nick Gammon's answer, I now have this.

MotorEncoderMaster.ino

#include </home/marcel/dev/Arduino/encoders/MotorEncoderMaster/Encoders.h>
Encoders * Encoders::instances [2] = { NULL, NULL };
// instances of our class
Encoders right;
Encoders left;
void setup (){
 right.begin (2); // pin D2
 left.begin (3); // pin D3
} // end of setup
void loop (){
 // whatever
} // end of loop

Encoders.h:

class Encoders{
 volatile bool switchChanged;
 static Encoders * instances [2];
 static void switchPressedExt0(){
 if(Encoders::instances [0] != NULL){
 Encoders::instances [0]->switchPressed();
 }
 }
 static void switchPressedExt1 (){
 if (Encoders::instances [1] != NULL){
 Encoders::instances [1]->switchPressed();
 }
 }
 public:
 void begin (const int whichPin){
 pinMode (whichPin, INPUT_PULLUP);
 switch (whichPin){
 case 2:
 attachInterrupt(0, switchPressedExt0, CHANGE);
 instances [0] = this;
 break;
 case 3:
 attachInterrupt(1, switchPressedExt1, CHANGE);
 instances [1] = this;
 break;
 }
 }
 void switchPressed(){
 switchChanged = true;
 }
};

When I try to compile this, I get :

In file included from MotorEncoderMaster.ino:1:0:
/home/marcel/dev/Arduino/encoders/Encoders.h: In static member function ‘static void Encoders::switchPressedExt0()’:
/home/marcel/dev/Arduino/encoders/Encoders.h:7:34: error: ‘NULL’ was not declared in this scope
 if(Encoders::instances [0] != NULL){
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h: In static member function ‘static void Encoders::switchPressedExt1()’:
/home/marcel/dev/Arduino/encoders/Encoders.h:13:35: error: ‘NULL’ was not declared in this scope
 if (Encoders::instances [1] != NULL){
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h: In member function ‘void Encoders::begin(int)’:
/home/marcel/dev/Arduino/encoders/Encoders.h:21:24: error: ‘INPUT_PULLUP’ was not declared in this scope
 pinMode (whichPin, INPUT_PULLUP);
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h:21:36: error: ‘pinMode’ was not declared in this scope
 pinMode (whichPin, INPUT_PULLUP);
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h:24:45: error: ‘CHANGE’ was not declared in this scope
 attachInterrupt(0, switchPressedExt0, CHANGE);
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h:24:51: error: ‘attachInterrupt’ was not declared in this scope
 attachInterrupt(0, switchPressedExt0, CHANGE);
 ^

#Edit: Per Nick Gammon's answer, I now have this.

MotorEncoderMaster.ino

#include </home/marcel/dev/Arduino/encoders/MotorEncoderMaster/Encoders.h>
Encoders * Encoders::instances [2] = { NULL, NULL };
// instances of our class
Encoders right;
Encoders left;
void setup (){
 right.begin (2); // pin D2
 left.begin (3); // pin D3
} // end of setup
void loop (){
 // whatever
} // end of loop

Encoders.h:

class Encoders{
 volatile bool switchChanged;
 static Encoders * instances [2];
 static void switchPressedExt0(){
 if(Encoders::instances [0] != NULL){
 Encoders::instances [0]->switchPressed();
 }
 }
 static void switchPressedExt1 (){
 if (Encoders::instances [1] != NULL){
 Encoders::instances [1]->switchPressed();
 }
 }
 public:
 void begin (const int whichPin){
 pinMode (whichPin, INPUT_PULLUP);
 switch (whichPin){
 case 2:
 attachInterrupt(0, switchPressedExt0, CHANGE);
 instances [0] = this;
 break;
 case 3:
 attachInterrupt(1, switchPressedExt1, CHANGE);
 instances [1] = this;
 break;
 }
 }
 void switchPressed(){
 switchChanged = true;
 }
};

When I try to compile this, I get :

In file included from MotorEncoderMaster.ino:1:0:
/home/marcel/dev/Arduino/encoders/Encoders.h: In static member function ‘static void Encoders::switchPressedExt0()’:
/home/marcel/dev/Arduino/encoders/Encoders.h:7:34: error: ‘NULL’ was not declared in this scope
 if(Encoders::instances [0] != NULL){
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h: In static member function ‘static void Encoders::switchPressedExt1()’:
/home/marcel/dev/Arduino/encoders/Encoders.h:13:35: error: ‘NULL’ was not declared in this scope
 if (Encoders::instances [1] != NULL){
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h: In member function ‘void Encoders::begin(int)’:
/home/marcel/dev/Arduino/encoders/Encoders.h:21:24: error: ‘INPUT_PULLUP’ was not declared in this scope
 pinMode (whichPin, INPUT_PULLUP);
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h:21:36: error: ‘pinMode’ was not declared in this scope
 pinMode (whichPin, INPUT_PULLUP);
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h:24:45: error: ‘CHANGE’ was not declared in this scope
 attachInterrupt(0, switchPressedExt0, CHANGE);
 ^
/home/marcel/dev/Arduino/encoders/Encoders.h:24:51: error: ‘attachInterrupt’ was not declared in this scope
 attachInterrupt(0, switchPressedExt0, CHANGE);
 ^
Minor fixes
Source Link
Greenonline
  • 3.2k
  • 7
  • 36
  • 48

I am trying to write ana library for encoders.

I tried answers on other threads. Most said to make the functions passed to attachInterrupt staticattachInterrupt()static. That threw different errors:

The arduinoArduino in question is a Mega but I doubt that matters.

I am trying to write an library for encoders.

I tried answers on other threads. Most said to make the functions passed to attachInterrupt static. That threw different errors:

The arduino in question is a Mega but I doubt that matters.

I am trying to write a library for encoders.

I tried answers on other threads. Most said to make the functions passed to attachInterrupt()static. That threw different errors:

The Arduino in question is a Mega but I doubt that matters.

Source Link
Marcel
  • 181
  • 4
  • 11
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /