Problem with ISR functions (solved)

I'm currently working on a project that involves reading multiple pwm inputs using the Pin Change Interrupt Library. I had it working recently, but now it just says " Motors_rising was not declared in this scope" even though it has been declared. I've tried moving around the order of the functions but it doesn't seem to do anything. I'm using an Arduino Uno Rev 3.

#include "PinChangeInterrupt.h"
#include <Servo.h>
#include <SPI.h>
#include <TMRpcm.h>
#include <SD.h>

#define MotorPWM 2
#define SMMPWM 3
#define OTHERIN A0
#define WBDrop A2
#define MUSIC A4
#define SD_ChipSelectPin 10

volatile int motor_pwm_value = 0;
volatile int motor_pwm_prev = 0;
volatile int motor_prev_time = 0;

volatile int SMM_prev = 0;
volatile int SMM_pwm = 0;
volatile bool single_motor=false;

volatile int Music_pwm = 0;
volatile int Music_prev = 0;
volatile bool Music_state = false;

volatile int Drop_pwm = 0;
volatile int Drop_prev = 0;
volatile bool Drop_state = false;

volatile int Other_pwm = 0;
volatile int Other_prev = 0;
volatile bool Other_state = false;

char dataString[210];
int count = 0;

void update_sd_card(){
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  //dataFile.println(dataString);
  dataFile.close();
  count=0;
}

void Motors_falling() {
  attachPCINT(digitalPinToPCINT(MotorPWM), Motors_rising, RISING);
  motor_pwm_value = micros()-motor_prev_time;
  Serial.println(motor_pwm_value);
  if((motor_pwm_value != motor_pwm_prev)&&(single_motor==false)){
    Motors.writeMicroseconds(motor_pwm_value);
  }
  motor_pwm_prev = motor_pwm_value;
}

void WB_falling(){
  attachPCINT(digitalPinToPCINT(WBDrop), WB_rising, RISING);
  Drop_pwm = micros()-Drop_prev;
if (Drop_pwm>1500){ //change value
    if (Drop_state==false){
      Drop_servo.write(90); //change value
    }
    Drop_state=true;

  }else{
    if (Drop_state==true){
      Drop_servo.write(0);
    }
    Drop_state=false;
}

void MUS_falling(){
  attachPCINT(digitalPinToPCINT(MUSIC), MUS_rising, RISING);
  Music_pwm = micros()-Music_prev;
  if (Music_pwm>1500){ //change value
    if (Music_state==false){
      speaker.play("song1.wav"); //play ride of the valkeries
    }
    Music_state=true;
    
    //digitalWrite(9, HIGH); //for buzzer
  }else{
    if (Music_state==true){
      speaker.pause();
      //digitalWrite(9, LOW); //for buzzer
    }
    Music_state=false;
  }
}

void SMM_falling(){
  attachPCINT(digitalPinToPCINT(SMMPWM), SMM_rising, RISING);
  SMM_pwm = micros()-SMM_prev;
  if (SMM_pwm>1800){
    if (single_motor==false){
      Motors.write(30);
    }
    single_motor = true;
    Serial.println("Motors off");
  }else{
    single_motor = false;
  }
  Serial.println(SMM_pwm);
}

void Motors_rising() {
  attachPCINT(digitalPinToPCINT(MotorPWM), Motors_falling, FALLING);
  motor_prev_time = micros();
}

void MUS_rising(){
  attachPCINT(digitalPinToPCINT(MUSIC), MUS_falling, FALLING);
  Music_prev=micros();
  
}

void OTHR_rising(){
  attachPCINT(digitalPinToPCINT(OTHERIN), OTHR_falling, FALLING);
  Other_prev=micros();
  
}

void WB_rising(){
  attachPCINT(digitalPinToPCINT(WBDrop), WB_falling, FALLING);
  Drop_prev=micros();
  
}

void SMM_rising(){
  attachPCINT(digitalPinToPCINT(SMMPWM), SMM_falling, FALLING);
  SMM_prev=micros();
  
}

Servo Motors;
Servo Drop_servo;

TMRpcm speaker;

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

  Motors.attach(7);
  Drop_servo.attach(6);
  pinMode(10, OUTPUT);
  pinMode(MotorPWM, INPUT_PULLUP);
  pinMode(SMMPWM, INPUT_PULLUP);
  pinMode(MUSIC, INPUT_PULLUP);
  pinMode(OTHERIN, INPUT_PULLUP);
  pinMode(WBDrop, INPUT_PULLUP);
  
  if (!SD.begin(10)) {
  Serial.println("SD fail");
  }
  
  speaker.speakerPin = 9;
  speaker.setVolume(5);
  
  
  attachPCINT(digitalPinToPCINT(SMMPWM), SMM_rising, RISING);
  attachPCINT(digitalPinToPCINT(MUSIC), MUS_rising, RISING);
  attachPCINT(digitalPinToPCINT(OTHERIN), OTHR_rising, RISING);
  attachPCINT(digitalPinToPCINT(WBDrop), WB_rising, RISING);
  attachPCINT(digitalPinToPCINT(MotorPWM), Motors_rising, RISING);
  
}
 
void loop() { 
  delay(50);
  noInterrupts();
  dataString[count]=MotorPWM;
  count++;
  dataString[count]=single_motor;
  count++;
  dataString[count]=Music_state;
  count++;
  dataString[count]=Drop_state;
  count++;
  dataString[count]=Other_state;
  count++;
  interrupts();
  if (count>200){
    update_sd_card();
  }
  
}

Thanks for your help.

attachPCINT cannot be safely called inside an interrupt service routine.

As mentioned, don't do this. Instead, interrupt on change, and detect whether the change was rising or falling. The library documentation clearly explains how to do this.

Get Trigger on mode CHANGE

// Only use this in the attached interrupt function.
uint8_t trigger = getPinChangeInterruptTrigger(digitalPinToPCINT(pinTick));
if(trigger == RISING)
  // Do something
else if(trigger == FALLING)
  // Do something
else
  // Wrong usage (trigger == CHANGE)

That kind of error generally arises when you have a missing brace somewhere above the error.
It looks to me like it is in this function

void WB_falling(){
  attachPCINT(digitalPinToPCINT(WBDrop), WB_rising, RISING);
  Drop_pwm = micros()-Drop_prev;
if (Drop_pwm>1500){ //change value
    if (Drop_state==false){
      Drop_servo.write(90); //change value
    }
    Drop_state=true;

  }else{
    if (Drop_state==true) {
      Drop_servo.write(0);
    }
    Drop_state=false;
}
1 Like

That seems to have fixed it. Thanks for your help

1 Like

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