Interrupts executing just one time

Hello,

I am new to Arduino, I am using Arduino Mega and Teensyduino 2.0++ boards for testing

On both boards I am trying a simple application to test interrupts

One external interrupt with a pin and one Timer Interrupt with TimerThree library.

Here is the code:

#include <TimerThree.h>

void blink_test(){
  bool t=~digitalRead(6);
  digitalWrite(6,t);
}

void interr_test(){
  bool t=false;
  if(digitalRead(19)){
    t=~digitalRead(9);
    digitalWrite(9,t);}
}

void setup() {
  noInterrupts();
  pinMode(6,OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(18,INPUT);
  pinMode(19,INPUT);
  pinMode(5,OUTPUT);
  digitalWrite(9,LOW);
  digitalWrite(8,LOW);
  digitalWrite(6,LOW);
  
  Timer3.initialize(1000000);
  Timer3.attachInterrupt(blink_test);
  attachInterrupt(digitalPinToInterrupt(19),interr_test,CHANGE);
  interrupts();
}

void loop() {
  digitalWrite(8,digitalRead(18));
  digitalWrite(5,SREG<<7);
}

The results I am getting is that whenever the Interrupt is triggered it is run just one time, I am sure that I am not getting stuck on the interrupts because, the code on the loop keeps working even after I had executed the interrupts.

Any advice would be appreciated.

    t=~digitalRead(9);

Two cases. If digitalRead(9) returns zero, then the ~ operator changes it to all ones (0xFFFF). If digitalRead(9) returns one, then the ~ operator changes it to 0xFFFE. Either way, t will be set to true because both 0xFFFF and 0xFFFE are non-zero.

What are you actually trying to accomplish with the ~ operator?

Pete

el_supremo:     t=~digitalRead(9);

Two cases. If digitalRead(9) returns zero, then the ~ operator changes it to all ones (0xFFFF). If digitalRead(9) returns one, then the ~ operator changes it to 0xFFFE. Either way, t will be set to true because both 0xFFFF and 0xFFFE are non-zero.

What are you actually trying to accomplish with the ~ operator?

Pete

Good point,

try use "!" rather than "~"

You also do not need to use a variable t:

digitalWrite(1, !digitalRead(1));

The purpose of that routine appears to be to toggle pin 9 if pin 19 is HIGH. Why not use bitSet(PINH,6)?

Thanks for your feedback, indeed the problem was using ~ instead of !, this solved the issue.

Regarding the bitSet suggestion, as I mentioned I am new to Arduino, I usually program on C, what is the advantage of the bitSet funtion over digitalWrite ?

adolfoe: Thanks for your feedback, indeed the problem was using ~ instead of !, this solved the issue.

Regarding the bitSet suggestion, as I mentioned I am new to Arduino, I usually program on C, what is the advantage of the bitSet funtion over digitalWrite ?

bitSet() is another Arduino function (look here) that provides access to the bits of any register whereas digitalWrite() specifically targets the PORTn output register which is then mapped via the pin number you supply. digitalRead() maps the pin to the appropriate bit to the input register, PINn. When a write is made (bitSet) to the input register (PINn) it toggles the value on the pin of the corresponding output register. This is how fast toggles are made when performing software PWM and uses considerably fewer resources. It's perhaps at the next level in your road to understanding, but you can't stay at the same level forever.

adolfoe: Regarding the bitSet suggestion, as I mentioned I am new to Arduino, I usually program on C, what is the advantage of the bitSet funtion over digitalWrite ?

BitSet is a convenience function for people new to C who haven't learned to use the bit-fiddling operators yet.