teensy 4.1 External Interrupt ?

nick703

Member
I have a teensy 4.1 and i want to use two external input connected to pin no. 21 and 23.

Now i have configure those pin as a interrupt function. so how to get below routing in interrupt function

21 23
HIGH LOW message ("All OK");
LOW HIGH message (" Move the Position");
HIGH HIGH messafe( " Error ") ;


please somebody help me. i have tried in polled method and this working but not in external interrupt function.
 
HIGH HIGH is not detectable by an interrupt.
Best is to use the Bounce2 library.

If you print a message, it means there is more than enough time, and absolutely no need for interrupts.
 
Just use the Arduino attachInterrupt function, like this

Code:
int count = 0;

void setup() {
  pinMode(21, INPUT_PULLDOWN);
  attachInterrupt(digitalPinToInterrupt(21), myfunction, RISING);
}

void myfunction() {
  count = count + 1;
  Serial.print("Move the Position ");
  Serial.println(count);
}

void loop() {
}

And if you're trying to read pushbuttons or swtiches, interrupts are usually the worst way because of mechanical chatter. Only use interrupts for clean digital signals.
 
thanks for you reply,

Actually i have to use interrupt because my requirement is 2 input is connected teensy 4.1 pin no. 21 and 23 using opto coupler. below is the circuit diagram.
When those two input connected together then i don't want any message but whenever any input is break that time i got interrupt signal message that error occurred. and also when i have connect those 2 input using ground then message i got like Move Position.
below is my circuit if any correction or any other circuit needed please tell me.
WhatsApp Image 2021-04-26 at 7.33.24 PM.jpg
 
I don't see why this needs interrupts... but ok.. didn't you say you have already working code?

Are these couplers fed by switches?
 
Last edited:
i need interrupts because in my other code is some time get into endless loop so i wake up using interrupts. so i need interrupt otherwise i have a polled method to check this pin via input pin. but when my other stuff is running in endless loop that time i didn't get right.

those two input is separate connected via object that means whenever contact like switch inserted between two input pin i recognize there is connected. but when not object inserted i got message that something error. and when ground connected those 2 pin i got move position like below inserted picture. if any modification required in circuit please suggest me.
WhatsApp Image 2021-04-27 at 10.32.03 AM.jpg
 
Nick, it is important to know if the switches are mechanical switches or not.
Mechanical switches bounce. Always.
Bouncing means, if a switch changes its state, youl'll get not one interrupt triggered - you'll get dozens, perhaps.
Then Arduino always uses an infinite loop. Why can't you do the polling (with bounce-library) in your loop?
Using interrupts is not a good way here.
Not only because of bouncing - also printing inside an interrupt must be avoided. It can crash the program.

Edit: You would have to set a flag inside the interrupt - and you guess it - poll that flag in your loop and print your text...

So, don't think interrupts make anything easier.... the code will be much more complex than the one you have.
 
Last edited:
yes you are right brother. but suppose my program is like when i press any key and i want my motor down side and in this time i suppose i loss my contact of inputA and inputB. that time i didn't use polled methed. like below code for only understand.
Code:
void loop()
{
     myKeyPad.scanAllraw();
     myKeyPad.ky_db();
     if(KeyLeft)
     {
         while(myKeyPad.ky_rdy == 1)
	 {
	      if ((micros() - k_hold) > 1500)
	     {
		digitalWrite(Weight_Dir, LOW);
		digitalWrite(Weight_Pulse, HIGH);
		 delayMicroseconds(25);
		digitalWrite(Weight_Pulse, LOW);
		k_hold = micros();
	     }
	    myKeyPad.ky_db();
	}
     }
     else if (KeyRight)
     {
     }
    /*  and so more Key*/	
}


so above code when i pressing keyleft continuously that time my controller stuck in this loop right. in this method i have checking with polling is difficult i think. so i have use external interrupt to execute code at sudden change. because my code have such a long listing of code. if any suggestion than give me .thanks
 
Simple :) Create a function that performs the query(polling) and call it whenever you think you need it. Insert the call into your loops and everthing is good...
 
Code:
[COLOR=#ff0000]void checkPosition() 
{
 code for pins 21, 23 here...
}[/COLOR]

void loop()
{

[...]
         while(myKeyPad.ky_rdy == 1)
     {
          if ((micros() - k_hold) > 1500)
         {
        digitalWrite(Weight_Dir, LOW);
        digitalWrite(Weight_Pulse, HIGH);
         delayMicroseconds(25);
        digitalWrite(Weight_Pulse, LOW);
        k_hold = micros();
         }
        myKeyPad.ky_db();
[COLOR=#ff0000]            checkPosition();[/COLOR]
    }


[...]
another loop..
 while(blah) {
[COLOR=#ff0000]            checkPosition();[/COLOR]
 }
[...]
 
Back
Top