Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: teensy 4.1 External Interrupt ?

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    15

    teensy 4.1 External Interrupt ?

    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.

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,325
    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.

  3. #3
    Senior Member
    Join Date
    Jul 2020
    Posts
    1,038
    Forum rule?

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,229
    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.

  5. #5
    Junior Member
    Join Date
    Sep 2020
    Posts
    15
    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.
    Click image for larger version. 

Name:	WhatsApp Image 2021-04-26 at 7.33.24 PM.jpg 
Views:	16 
Size:	62.3 KB 
ID:	24588

  6. #6
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,325
    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 by Frank B; 04-26-2021 at 02:56 PM.

  7. #7
    Junior Member
    Join Date
    Sep 2020
    Posts
    15
    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.
    Click image for larger version. 

Name:	WhatsApp Image 2021-04-27 at 10.32.03 AM.jpg 
Views:	10 
Size:	46.6 KB 
ID:	24604

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,325
    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 by Frank B; 04-27-2021 at 05:41 AM.

  9. #9
    Junior Member
    Join Date
    Sep 2020
    Posts
    15
    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

  10. #10
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,325
    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...

  11. #11
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,325
    Code:
    
    void checkPosition() 
    {
     code for pins 21, 23 here...
    }
    
    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();
                checkPosition();
        }
    
    
    [...]
    another loop..
     while(blah) {
                checkPosition();
     }
    [...]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •