Problems with Interrupt on digital pin 0,1 and 13

Status
Not open for further replies.

GeppettoLab

Active member
Hi!
I'm working on a project in which I have to use 10 led buttons and a Teensy 3.2
I have to recognize when 5 of them (and only that five) are pressed. The right configuration opens an electromagnet (by a relè).
At the same time I have to send a message trough Ethernet when the configuration is right. I will use the WIZ812MJ Module then I must to keep free for now pins 10 to 13.
For the button that I have to use (that are retroilluminated by a led) I use the Interrupt to controll the state of the button and the leds.
Then I must use digital pins from 0 to 9 (for now I don't use the relè, probably I will have to reduce the button to 9 to use the relè too) but the first two, connected to pin 0 and 1 doesn't work.
I controlled all the connection, switching cable and buttons and there is noting wrong in the hardware.
I check the function that i use with the interrupts with a serial print but it doesn't shows noting.
I have also eliminate the Serial initialization thinking about some conflicts , but nothing changes.


Is there a problem with interrupts on this two pins? the same appens in a previous setting using pin 13..

Can you Help me?

Code:
// TEENSY 3.2
const int LEVEL = 10;
int sequence[LEVEL] = {0, 1, 1, 0, 0, 0 ,1, 1, 1, 0};       //the right sequence
int yourSequence[LEVEL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};   //user sequence
int relayPin = 6;
boolean light_01 = 0;
boolean light_02 = 0;
boolean light_03 = 0;
boolean light_04 = 0;
boolean light_05 = 0;
boolean light_06 = 0;
boolean light_07 = 0;
boolean light_08 = 0;
boolean light_09 = 0;
boolean light_10 = 0;

int switchPin_01 = 0;
int switchPin_02 = 1;
int switchPin_03 = 2;
int switchPin_04 = 3;
int switchPin_05 = 4;
int switchPin_06 = 5;
int switchPin_07 = 6;
int switchPin_08 = 7;
int switchPin_09 = 8;
int switchPin_10 = 9;

int lightPin_01 = 14 ;
int lightPin_02 = 15;
int lightPin_03 = 16;
int lightPin_04 = 17;
int lightPin_05 = 18;
int lightPin_06 = 19;
int lightPin_07 = 20;
int lightPin_08 = 21;
int lightPin_09 = 22;
int lightPin_10 = 23;

void setup() {
  Serial.begin(9600);
  pinMode(lightPin_01, OUTPUT);
  pinMode(lightPin_02, OUTPUT);
  pinMode(lightPin_03, OUTPUT);
  pinMode(lightPin_04, OUTPUT);
  pinMode(lightPin_05, OUTPUT);
  pinMode(lightPin_06, OUTPUT);
  pinMode(lightPin_07, OUTPUT);
  pinMode(lightPin_08, OUTPUT);
  pinMode(lightPin_09, OUTPUT);
  pinMode(lightPin_10, OUTPUT);

  pinMode(switchPin_01, INPUT);
  pinMode(switchPin_02, INPUT);
  pinMode(switchPin_03, INPUT);
  pinMode(switchPin_04, INPUT);
  pinMode(switchPin_05, INPUT);
  pinMode(switchPin_06, INPUT);
  pinMode(switchPin_07, INPUT);
  pinMode(switchPin_08, INPUT);
  pinMode(switchPin_09, INPUT);
  pinMode(switchPin_10, INPUT);

//  pinMode(relayPin, OUTPUT);
//
//  digitalWrite(relayPin, LOW);

  digitalWrite(lightPin_01, LOW);
  digitalWrite(lightPin_02, LOW);
  digitalWrite(lightPin_03, LOW);
  digitalWrite(lightPin_04, LOW);
  digitalWrite(lightPin_05, LOW);
  digitalWrite(lightPin_06, LOW);
  digitalWrite(lightPin_07, LOW);
  digitalWrite(lightPin_08, LOW);
  digitalWrite(lightPin_09, LOW);
  digitalWrite(lightPin_10, LOW);


  attachInterrupt(switchPin_01, lightOn01, FALLING);
  attachInterrupt(switchPin_02, lightOn02, FALLING);
  attachInterrupt(switchPin_03, lightOn03, FALLING);
  attachInterrupt(switchPin_04, lightOn04, FALLING);
  attachInterrupt(switchPin_05, lightOn05, FALLING);
  attachInterrupt(switchPin_06, lightOn06, FALLING);
  attachInterrupt(switchPin_07, lightOn07, FALLING);
  attachInterrupt(switchPin_08, lightOn08, FALLING);
  attachInterrupt(switchPin_09, lightOn09, FALLING);
  attachInterrupt(switchPin_10, lightOn10, FALLING);
}

void loop()
{

  if (seq_cmp(yourSequence, sequence)) Serial.println("OK"); //compare the two sequences

}

void lightOn01() { //if the button is pressed change the state of the LED and change the value in the yourSequence array
  Serial.println("1 pressed");
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_01 == LOW) light_01 = HIGH;
    else light_01 = LOW;
    yourSequence[0] = light_01;
    digitalWrite(lightPin_01, light_01);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn02() {
  Serial.println("2 pressed");
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_02 == LOW) light_02 = HIGH;
    else light_02 = LOW;
    yourSequence[1] = light_02;
    digitalWrite(lightPin_02, light_02);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn03() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_03 == LOW) light_03 = HIGH;
    else light_03 = LOW;
    yourSequence[2] = light_03;
    digitalWrite(lightPin_03, light_03);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn04() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_04 == LOW) light_04 = HIGH;
    else light_04 = LOW;
    yourSequence[3] = light_04;
    digitalWrite(lightPin_04, light_04);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn05() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_05 == LOW) light_05 = HIGH;
    else light_05 = LOW;
    yourSequence[4] = light_05;
    digitalWrite(lightPin_05, light_05);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn06() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_06 == LOW) light_06 = HIGH;
    else light_06 = LOW;
    yourSequence[5] = light_06;
    digitalWrite(lightPin_06, light_06);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn07() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_07 == LOW) light_07 = HIGH;
    else light_07 = LOW;
    yourSequence[6] = light_07;
    digitalWrite(lightPin_07, light_07);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn08() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_08 == LOW) light_08 = HIGH;
    else light_08 = LOW;
    yourSequence[7] = light_08;
    digitalWrite(lightPin_08, light_08);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn09() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_09 == LOW) light_09 = HIGH;
    else light_09 = LOW;
    yourSequence[8] = light_09;
    digitalWrite(lightPin_09, light_09);
  }
  last_interrupt_time = interrupt_time;
}

void lightOn10() {
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)
  {
    if (light_10 == LOW) light_10 = HIGH;
    else light_10 = LOW;
    yourSequence[9] = light_10;
    digitalWrite(lightPin_10, light_10);
  }
  last_interrupt_time = interrupt_time;
}

//compare the two sequences
boolean seq_cmp(int *a, int *b) { 
  for (int n = 0; n < LEVEL; n++) if (a[n] != b[n]) return false;
  return true;
}
 
Maybe hard to say, without seeing the actual wiring.

That is How is pin 0 and 1 hooked up?

Lets looks at switchPin_01

You set the pin to Input: pinMode(switchPin_01, INPUT);
and likewise you set the interrupt: attachInterrupt(switchPin_01, lightOn01, FALLING);

So I am assuming you have pin 0 hooked up to button, and the other side of the button is probably hooked to ground?

If so what sets the state of pin 0 when you are not pressing the button? Does it have an external Pull Up resistor?

If not, have you tried changing the pinMode to : pinMode(switchPin_01, INPUT_PULLUP);

As for Pin13, there are other complications, in that it has an LED hooked up to it...
 
Thanks Kurte for the reply

That is How is pin 0 and 1 hooked up?
Pin 0 and 1 as the others has a pulldown resistor and are connected to the switch. The other pin of the switch are connected to 3,3V
I attach a picture of my simple circuit.

IMG_20180411_114418.jpgIMG_20180411_114356.jpg
As for Pin13, there are other complications, in that it has an LED hooked up to it...

What kind of problem can the Other LED gives?

Thanks!
 
Hopefully others will spot something obvious. Forgot to ask if the buttons are Normally open or closed?

That is when do you expect the falling condition? If they are pulled down, and the other side is 3.3v, with NO buttons, then pressing the button will cause the signal to go up to 3.3v and when you release, it will fall back down... So falling on release? If NC button, then 3.3v when not pressed, and then would be pulled low when button is pressed...

Note: I would normally not use interrupts for this, and instead use bounce library... but in theory it should work. I would double check wiring as I don't always trust breadboards to give good connections, especialy sometimes with resistors... I would maybe try maybe hacking the circuit up, to have the LED hooked up to the input pin, such that you can visually see if the state changes...
 
Forgot to ask if the buttons are Normally open or closed?
The button is normally open and it works with all the other pins in that configuration.
The ones that gives problems are 0, 1 and 13.
I don't think is an hardware problem, and I tried to visualize from Serial if it recognize the pressure of that button. Phisically it does, but the interrupts doesnt call the corrispondent function.

I can try with the bounce but i need a fast answer from the circuit and I'm worried that it is less permorming.
Eventually I can use the other pins (24 to 33), but there is some header that can be used for that?
 
Ops!
I'm so sorry, I realize that I'm using a Teensy LC instead a 3.2
In the LC the pin 0 an 1 doesn't have interrupt capability, but the 13 yes and it doesn't work.
I replaced the LC with a 3.2 and now the pin that isn't working are 3, 7 and 8
IMG_20180411_170956.jpg
As before using the serial monitor I verify that the interrupts doesn't call the funcion.
With the same method i see that the button that i'm using has bouncing problems because someone calls several times the function (i'm not expecting this using the interrupts...)

some advice?

Thanks
Luca
 
Mechanical chatter in pushbuttons becomes a bigger problem as you use faster chips, and Teensy 3.2 with interrupts is very fast.

Normally the best way to read pushbuttons is with the Bounce library. Look at File > Examples > Teensy > USB_Keyboard > Buttons for an example.
 
Thanks all,
I write down mine Bounce function but I will try this one.
I found the problem with the interrupt pins. Easily, the board is broken and that three pins doesn't work.
It remains the problem with pin 13 on Teensy LC but I will not use it for this project.
Now with the interrupts everythings works well, but I will try also the officical Bounce Library.

Thanks again
Luca
 
Remember pin 13 has a LED and 470 ohm resistor. Usually a much stronger pullup resistor is needed if you wish to use a pushbutton on pin 13.
 
Status
Not open for further replies.
Back
Top