Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15346

I cannot get attachInterrupt() to work (invalid use of non-static member function)

$
0
0

I have been trying for days to get this code to work, but I cannot find a solution.
My sketch:

#include <ControlInterrupt.h>

const int commonPin = 2;
const int buttonPins[] = {4,5,6,7,8};

ControlInterrupt controlInterrupt(commonPin, buttonPins);

void setup() {
  // put your setup code here, to run once:
  controlInterrupt.configureCommon(); // Setup pins for  interrupt
  attachInterrupt(digitalPinToInterrupt(commonPin), controlInterrupt.pressInterrupt, FALLING);
  controlInterrupt.begin();
}

void loop() {
  // Empty
}

.h

/*
  ControlInterrupt.h - A library for handling multiple buttons using a single interrupt.
                       This is done by using an interrupt pin effectively as the ground,
                       eliminating the need for additional components.
  Authors: Svizel_pritula 05/05/2019
           TheCodeGeek 01/13/2024
*/
#ifndef ControlInterrupt_h
#define ControlInterrupt_h
#include <Arduino.h>
class ControlInterrupt
{
public:
  ControlInterrupt(int commonPin, int buttonPins[]);
  void begin();
  void pressInterrupt();
  void configureCommon();
  void configureDistinct();
  //void getCommand(int button);

private:
  int _commonPin;
  //int _arrLength;
  int _buttonPins[];
};
#endif

.cpp

/*
  ControlInterrupt.h - A library for handling multiple buttons using a single interrupt.
                       This is done by using an interrupt pin effectively as the ground,
                       eliminating the need for additional components.
  Authors: Svizel_pritula 05/05/2019
           TheCodeGeek 01/13/2024
*/
#include <Arduino.h>
#include <ControlInterrupt.h>

unsigned long lastFire;

ControlInterrupt::ControlInterrupt(int commonPin, int buttonPins[])
//, String commandOrder[]
{
  _commonPin = commonPin;
  _arrLength = sizeof(buttonPins);
  _buttonPins[arrLength] = buttonPins;
  //_commandOrder[arrLength] = commandOrder;

  lastFire = 0;
}


void ControlInterrupt::begin()
{
  Serial.begin(9600);
}

void ControlInterrupt::pressInterrupt()
{ // ISR
  if (millis() - lastFire < 200)
  { // Debounce
    return;
  }
  lastFire = millis();

  configureDistinct(); // Setup  pins for testing individual buttons

  for (int i = 0; i < (sizeof(buttonPins)/sizeof(int)); i++)
  { // Test each button for press
    if (!digitalRead(_buttonPins[i]))
    {
      getCommand(i);
    }
  }

  configureCommon(); // Return to original  state
}

void ControlInterrupt::configureCommon()
{
  pinMode(_commonPin, INPUT_PULLUP);

  for (int i = 0; i < (sizeof(buttonPins)/sizeof(int)); i++)
  {
    pinMode(_buttonPins[i], OUTPUT);
    digitalWrite(_buttonPins[i], LOW);
  }
}

void ControlInterrupt::configureDistinct()
{
  pinMode(_commonPin, OUTPUT);
  digitalWrite(_commonPin, LOW);

  for (int i = 0; i < (sizeof(buttonPins)/sizeof(int)); i++)
  {
    pinMode(_buttonPins[i], INPUT_PULLUP);
  }
}

void ControlInterrupt::getCommand(int button)
//String ControlInterrupt::getCommand(int button)
{               // Our handler
  String label; // This stores the string to print when controls are used

  Serial.print(button + 1);
  Serial.print(": ");
  switch (button)
  {
  case 0:
    // statement
    label = "UP";
    break;
  case 1:
    // statement
    label = "DOWN";
    break;
  case 2:
    // statement
    label = "CLEAR";
    break;
  case 3:
    // statement
    label = "BACK";
    break;
  case 4:
    // statement
    label = "LEFT";
    break;
  case 5:
    // statement
    label = "ENTER";
    break;
  case 6:
    // statement
    label = "RIGHT";
    break;
  default:
    // statement
    break;
  }
  Serial.print(label);
  //return label;
}

5 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 15346

Trending Articles