PinChangeInt.h Teensy2

Status
Not open for further replies.

mercapto

Member
Hi,
I want to convert electrical signals into a keyboard button press (reliable and as fast as possible). Therefore I choose ext. interrupts and it is working fine for the 4 ext interrupts I have on a Teensy2. But I need 5 input channels.

google directed me to the PinChangeInt.h. But I am not able to use this library. Is this library running on a teensy2 in priciple?

If I use PCintPort::attachInterrupt(TriggerIn, trigger, RISING); instead of attachInterrupt none of the Interrupts will work. With the code below the Interrupts on Pin 5-8 are working.

Code:
#define BAUD 57600
#define DEFAULT_OUT_PULSE_MSEC 50

#define       NO_PIN_ARDUINO // do not save Interrupt pin
//#define       NO_PORTB_PINCHANGES
//#define       NO_PORTC_PINCHANGES
//#define       NO_PORTD_PINCHANGES

#include <PinChangeInt.h>

const int LedOutPin = 11; // LED PIN 11 Teensy 2, 14 Teensy 3
const int LedRespOut1 = 12; // LED Response 1
const int LedRespOut2 = 13; // LED Response 2
const int LedRespOut3 = 14; // LED Response 3
const int LedRespOut4 = 15; // LED Response 4

const int TriggerIn = 9; // Trigger Interrupt Pin  PC6
const int Response1 = 5; // Response Button 1
const int Response2 = 6; // Response Button 2
const int Response3 = 7; // Response Button 3
const int Response4 = 8; // Response Button 4

volatile unsigned long g_outPulseStart;
volatile unsigned long g_out_RespStart1;
volatile unsigned long g_out_RespStart2;
volatile unsigned long g_out_RespStart3;
volatile unsigned long g_out_RespStart4;

// Bounce delay
int g_bounceDelay = 10;
unsigned int g_outPulseDuration = DEFAULT_OUT_PULSE_MSEC;

int i;

void setup()
{
  i = 0;
  //Serial.begin(BAUD);
  pinMode(LedOutPin, OUTPUT);
  pinMode(LedRespOut1, OUTPUT);
  pinMode(LedRespOut2, OUTPUT);
  pinMode(LedRespOut3, OUTPUT);
  pinMode(LedRespOut4, OUTPUT);
  
  pinMode(TriggerIn, INPUT);
  pinMode(Response1, INPUT_PULLUP);
  pinMode(Response2, INPUT_PULLUP);
  pinMode(Response3, INPUT_PULLUP);
  pinMode(Response4, INPUT_PULLUP);
  
  attachInterrupt(TriggerIn, trigger, RISING);
  attachInterrupt(Response1, response1, FALLING);
  attachInterrupt(Response2, response2, FALLING);
  attachInterrupt(Response3, response3, FALLING);
  attachInterrupt(Response4, response4, FALLING);
        
}


void loop()
{
// Reset the output, if needed:
    // Turn off the output pin after the requested duration.
    // Detect and correct counter wrap-around:
    if(millis()<g_outPulseStart) g_outPulseStart += 4294967295UL;
    if(millis()-g_outPulseStart > g_outPulseDuration)
      digitalWrite(LedOutPin, LOW);
      
    if(millis()<g_out_RespStart1) g_out_RespStart1 += 4294967295UL;
    if(millis()-g_out_RespStart1 > g_outPulseDuration)
      digitalWrite(LedRespOut1, LOW);    
    
    if(millis()<g_out_RespStart2) g_out_RespStart2 += 4294967295UL;
    if(millis()-g_out_RespStart2 > g_outPulseDuration)
      digitalWrite(LedRespOut2, LOW);    
 
    if(millis()<g_out_RespStart3) g_out_RespStart3 += 4294967295UL;
    if(millis()-g_out_RespStart3 > g_outPulseDuration)
      digitalWrite(LedRespOut3, LOW);    
 
    if(millis()<g_out_RespStart4) g_out_RespStart4 += 4294967295UL;
    if(millis()-g_out_RespStart4 > g_outPulseDuration)
      digitalWrite(LedRespOut4, LOW);    
 
 
 }

void trigger()
{
   if (millis() - g_outPulseStart >= g_bounceDelay)
   {
   Keyboard.print("t");
   //Serial.print(i);
//   Serial.send_now() // speed up?
   i = i+1;
   digitalWrite(LedOutPin, HIGH);
   g_outPulseStart = millis();
   }
}
void response1()
{
  if (millis() - g_out_RespStart1 >= g_bounceDelay)
   {
    Keyboard.print("1");
    digitalWrite(LedRespOut1, HIGH);
    g_out_RespStart1 = millis();
   }
}

void response2()
{
  if (millis() - g_out_RespStart2 >= g_bounceDelay)
   {
    Keyboard.print("2");
    digitalWrite(LedRespOut2, HIGH);
    g_out_RespStart2 = millis();
   }
}

void response3()
{
  if (millis() - g_out_RespStart3 >= g_bounceDelay)
   {
    Keyboard.print("3");
    digitalWrite(LedRespOut3, HIGH);
    g_out_RespStart3 = millis();
   }
}

void response4()
{
  if (millis() - g_out_RespStart4 >= g_bounceDelay)
   {
    Keyboard.print("4");
    digitalWrite(LedRespOut4, HIGH);
    g_out_RespStart4 = millis();
   }
}
 
I used the playground one. Why, is there a difference for Teensy 2.0 ? I used a nano and the library works fine. I have a lot of Teensy 2.0 's for another application where they work great and I was hoping to use them for this also. I understand thar the 3.1 is diferent and will nt support the interrupt library (Yes ?)
 
One could use attachInterrupt() if the change is signficant in LOW, HIGH, RISING, FALLING rather than any toggle of state. If the nature of the signal permits, the ISR can change the condition for the next interrupt using detachInterrupt().
But direct access to the NVIC would be better.
 
Status
Not open for further replies.
Back
Top