PinChangeInterrupt with TimerOne

when i m using pin 2 of UNO as interrupt pin it is working fine. But when I want to use A4 pin of UNO board as external interrupt using Pinchangeint.h library,but its not working.

#include "IRremote.h"
#include <PinChangeInt.h>
#define ARDUINOPIN A4

int receiver = A5; 
IRrecv irrecv(receiver);           
decode_results results;            



#include <TimerOne.h>           
volatile int i=0;               // Variable to use as a counter
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 10;                 // Output to Opto Triac

//int dim2 = 0;                   // led control
int dim = 128;                  // Dimming level (0-128)  0 = on, 128 = 0ff                  
int freqStep = 75;    // This is the delay-per-brightness step in microseconds.

void setup() { 
  //Serial.begin(9600);  
  irrecv.enableIRIn(); // Start the IR receiver (classic remote)
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  pinMode(ARDUINOPIN, INPUT_PULLUP);
  PCintPort::attachInterrupt(ARDUINOPIN, zero_cross_detect, RISING);
  //attachPinChangeInterrupt(ARDUINOPIN, zero_cross_detect, RISING);
  //attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  //Timer1.attachPinChangeInterrupt(dim_check, freqStep);
  Timer1.attachInterrupt(dim_check, freqStep);      
}

void zero_cross_detect() 
{    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  i=0;
  digitalWrite(AC_pin, LOW);
}                                 
// Turn on the TRIAC at the appropriate time

void dim_check() 
{                   
  if(zero_cross == true) {              
    if(i>=dim) {                     
      digitalWrite(AC_pin, HIGH);  // turn on light       
      i=0;  // reset time step counter                         
      zero_cross=false;    // reset zero cross detection
    } 
    else {
      i++;  // increment time step counter                     
}}}                                      


void translateIR(int code) // takes action based on IR code received
{
  switch(code)
  {
  case -24481:  
    {
   
     DecreaseSpeed();
   }
    
    break;

  case 24735:  
   {
     IncreaseSpeed();
      
   }
    break;
  }}
  
void loop() {  
 if (irrecv.decode(&results)) // have we received an IR signal?
  {
    //Serial.println(results.value);
    int code = results.value;
    delay(200);
    translateIR(code); 
    irrecv.resume();
  }  

}

void IncreaseSpeed(){
  delay(200);
  if (dim>5)  
  {
    dim = dim - 8;
    if (dim<0) 
    {
      dim=0; 
    }
  }
}

void DecreaseSpeed(){
  delay(200);
  if (dim<127)  
   {
    dim = dim + 8;
    if (dim>127) 
    {
      dim=128; 
    }
   }
}

reciver.ino (2.88 KB)

I believe the analog inputs will not create an interrupt on a rising or falling edge. There was a lot of discussion on this, try this link: Interrupts and analog pins - Programming Questions - Arduino Forum

Correct, pin change interrupts will be triggered on any pin change within a particular port.

Pin A4 is on Port C, the ISR is ISR (PCINT1_vect).

I just read this page, and it helped me a lot in understanding PCI's, and the differences with "external" interrupts:

gilshultz:
I believe the analog inputs will not create an interrupt on a rising or falling edge. There was a lot of discussion on this, try this link: Interrupts and analog pins - Programming Questions - Arduino Forum

I KNOW that on the Uno pin change interrupts work on every I/O pin, since the 3 ports supported
by pin change interrupts cover every pin capable of digital operation.

On the Arduino Mega the situation is very different indeed, as there still only 3 ports supporting
pin change interrupts, and several of those pins aren't brought out at all.

I've only used pin change interrupts directly rather than through a library, so I don't know the ins/outs
of PinChangeInt.h

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