Can you have ISR’s in a structure? 2nd question

Hi Folks,

I have one problem left, that is I can't access variable KeyPressed.

C:\Users\jmazu\OneDrive\Documents\Arduino\SwitchTest/SwitchTest.ino:71: undefined reference to sControlKeys::KeyPressed' C:\Users\jmazu\AppData\Local\Temp\cc8eq38u.ltrans0.ltrans.o: In function Initialise':
C:\Users\jmazu\OneDrive\Documents\Arduino\SwitchTest/SwitchTest.ino:63: undefined reference to `sControlKeys::KeyPressed'

Any ideas?

FYI: I'm using a structure so I can encapsulate everything to do with the Control Switches. There will only be one instance.

#define O_PIN_CLOCK       22
#define O_PIN_LATCH       23
#define I_PIN_DATA        24
#define I_PIN_RUN_STATUS  25
#define I_PIN_KEY_PRESSED 26

  
int Index;
int Value;
int Mask;

struct sControlKeys {
  #define CLOCK_LOW     false
  #define CLOCK_HIGH    true

  #define SERIAL_MODE   false
  #define PARALLEL_MODE true

   private:
      //Arduino Digital Pins.
      int _O_Pin_Clock;          // Output - Clocks the shift register.  Low to High is the active clock edge.
      int _O_Pin_Latch;          // Output - Sets the mode of the shift Register.  Parallel: High.  Shift: Low
      int _I_Pin_Data;           // Input  - Receives a bit (Q8) from the shift register.
      int _I_Pin_Run_Status;     // Input  - State of the Run/Stop switch.  Run: High.  Low: Stop.
      int _I_Pin_Key_Pressed;    // Input  - Receives a positive pulse whenever a key is pressed.
            
      volatile static boolean KeyPressed;

   public:
      // ********* Informs the sructure which pins to use. **********
      void SetPin_Clock(int PinNumber) {
         _O_Pin_Clock = PinNumber;
      }

      void SetPin_Latch(int PinNumber) {
         _O_Pin_Latch = PinNumber;
      }

      void SetPin_Data(int PinNumber) {
         _I_Pin_Data = PinNumber;
      }

      void SetPin_RunStatus(int PinNumber) {
         _I_Pin_Run_Status = PinNumber;
      }

      void SetPin_KeyPressed(int PinNumber) {
         _I_Pin_Key_Pressed = PinNumber;
      }

      void Initialise(void) {
         // Assigns the pin numbers
         pinMode(_O_Pin_Clock,       OUTPUT);
         pinMode(_O_Pin_Latch,       OUTPUT);
         
         pinMode(_I_Pin_Data,        INPUT);
         pinMode(_I_Pin_Run_Status,  INPUT);
         pinMode(_I_Pin_Key_Pressed, INPUT);
   
         digitalWrite(_O_Pin_Clock, CLOCK_LOW);          // Clock low.
         digitalWrite(_O_Pin_Latch, PARALLEL_MODE);      // Shift register in parallel load mode.

         KeyPressed = false;

         attachInterrupt(digitalPinToInterrupt(_I_Pin_Key_Pressed), KeyPressedISR, RISING);
         
      }
      
      // Handles the key pressed interrupt.
      static void KeyPressedISR(void) {
         KeyPressed = true;
      }

      int ReadControlSwitches(void) {
        
        //Latch the data.
        digitalWrite(O_PIN_LATCH, SERIAL_MODE);

        //Read the data.
        Value = 0;
        Mask = 0b00100000;
  
        for (Index = 1; Index <= 6; Index++) {

          if (digitalRead(I_PIN_DATA) != 0)
             Value |= Mask;

          // Cycle the clock.
          digitalWrite(O_PIN_CLOCK, CLOCK_HIGH);
          digitalWrite(O_PIN_CLOCK, CLOCK_LOW);

          // Left shift one place
          Mask = Mask >> 1;
        }

        // Put the shift register into parallel load mode.
        digitalWrite(O_PIN_LATCH, PARALLEL_MODE);
    
        return Value;  
      
      }
} ControlKeys;

//**********************************************************************//

void setup() {
   
  ControlKeys.SetPin_Clock(O_PIN_CLOCK);
  ControlKeys.SetPin_Latch(O_PIN_LATCH);
  ControlKeys.SetPin_Data(I_PIN_DATA);
  ControlKeys.SetPin_RunStatus(I_PIN_RUN_STATUS);
  ControlKeys.SetPin_KeyPressed(I_PIN_KEY_PRESSED);
  
  ControlKeys.Initialise();
  
  Serial.begin(9600);

}

void loop() {

  ControlKeys.ReadControlKeys();
   
  Serial.println();
  Serial.print("h");
  Serial.print(Value, HEX);
  
  delay(2000);

}

You've declared 'KeyPressed' but haven't defined it ...... i.e. you haven't allocated its storage.

  ControlKeys.ReadControlKeys();

Where in the sketch is the ReadControlKeys() function ?

Just above setup() put:

volatile boolean sControlKeys::KeyPressed = false;

Hi John,

That worked. I have no idea why.

Hi UKHB,

I've been working in a couple of windows and just mis-spelled it.
No idea why the compiler didn't pick that up.

Because, like @gfalvo said, you 'declared' a static variable but did not 'define' it. The definition is where the space is allocated. The declaration just says "There is a variable named this defined somewhere. The linker will tell you where in memory it is defined."

It did for me and gave the error

'struct sControlKeys' has no member named 'ReadControlKeys';

Hi again,

I think I have my answer, have a look at these results.

I wrote a for loop for the first 52 pin numbers and displayed the corresponding Interrupt Number.

Not what I was expecting at all.

Time for a major redesign. :frowning:

0, -1
1, -1
2, 0 <---
3, 1 <--
4, -1
5, -1
6, -1
7, -1
8, -1
9, -1
10, -1
11, -1
12, -1
13, -1
14, -1
15, -1
16, -1
17, -1
18, 5 <---
19, 4 <---
20, 3 <---
21, 2 <---
22, -1
23, -1
24, -1
25, -1
26, -1
27, -1
28, -1
29, -1
30, -1
31, -1
32, -1
33, -1
34, -1
35, -1
36, -1
37, -1
38, -1
39, -1
40, -1
41, -1
42, -1
43, -1
44, -1
45, -1
46, -1
47, -1
48, -1
49, -1
50, -1
51, -1
52, -1

I honestly thought there were more interrupt pins on a Mega. :frowning:

See attachInterrupt() - Arduino Reference

For more interrupts, you can use the PinChangeInterrupt library.

A little time spent in advance studying the documentation would have prevented that disappointment.

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