PlatformIO Community

Function attachInterrupt does not work

Hi, I have some problem with build my project. I have tried to make interrupt with help of an attachInterrupt function, but compiler is writing “src/main.cpp:24:22: error: ‘myInter’ was not declared in this scope”. But i have function myInter(). The code is bellow:

#include <Arduino.h>

#define TIME_START 0 

unsigned long period_time = 10000; 
unsigned long width_time = 5000; 

//рабочие переменные
unsigned long period_timer;
unsigned long width_timer;

bool flag_timer = false;
volatile bool flag = false;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(9, OUTPUT);
  attachInterrupt(0, myInter, FALLING);
  period_timer = millis();
}

void loop() {
  if(flag == true) {
    if(millis() - period_timer > period_time) {
    period_timer = millis();
    width_timer = millis();
    flag_timer = true;
    digitalWrite(9, HIGH);
  }
  if((millis() - width_timer > width_time) && flag_timer == true) {
    flag_timer = false;
    if(TIME_START) period_timer = millis();
    digitalWrite(9, LOW);
  }
    flag = false;
  }
}

void myInter() {
  flag = true;
}

But, in Arduino IDE all is OK.

Because you write it as a .ino file in the Arduino IDE which generates the function prototypes automatically for you. Your program is not a valid C++ program because you use myInter before it is declared. What you need is a declaration

void myInter();

at the start of the program, e.g. above the setup() function.

This is also all explained in the FAQ. http://docs.platformio.org/en/latest/faq.html#convert-arduino-file-to-c-manually

1 Like

OK, I understood. Thanks a lot for help!!!