user7351: I am trying to implement a function queue scheduling system. INT1 is connected to a button and int1task causes an LED to flash.
typedef void (*funcptr)(void);
TPrioQueue *queue = NULL;

void int1ISR() {
  if (debounce(&int1time)) // Checks if the interrupt is caused by switch bouncing
    enq(queue, (void*) int1task, 1);
}

Next, I execute the function in loop() by:
typedef void (*funcptr)(void);

void loop(){
  funcptr func = (funcptr) deq(queue);
  if (func != NULL)
    (*func)(); // Stuck in a loop
}

For some reason, the LED keeps flashing even though I have not pressed the button. Why does this happen?
When I remove the line below, the ISR works properly.
enq(queue, (void*) int1task, 1)

The relevant sections of the code:
int debounce(unsigned long *debTimer) {
  unsigned long tmp = *debTimer;
  unsigned long currTime = millis();

  if ((currTime-tmp) > 500){
    *debTimer = currTime;
    return 1;
  }
  else
    return 0;
}

// Flashes LED at pin 6 five times at 2 Hz
void int1task(){
  for(int i=0; i<5; i++){
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    delay(250);
  }
}

void setup() {
  pinMode(6, OUTPUT);
  attachInterrupt(1, int1ISR, RISING);
  Serial.begin(115200);
}

Code I used for the queue
Here is the schematic I am using:


user84: Although you did not clearly mention nor show your wiring, it is possible the problem comes from here.
Barring real concurrency issues with your queue implementation, which I already mentioned in another answer, for which fixes have to be implemented anyway, there are two potential wiring problems in your circuit:

You use INT1 interrupt; that means you must connect your button to PIN 3 for a UNO, any other pin won't work.
You did not specify pinMode(3, INPUT_PULLUP) in your setup(), which means you MUST use an external pullup resistor to force a HIGH value, and connect the other pin of the button to GND (or conversely: put a pulldown resistor and wire the other pin to 5V); otherwise, while the button is left unpressed, you will have a floating input, which means its value will vary randomly between HIGH and LOW, triggering your int1ISR many times when you don't expect it.

The simplest way to solve point 2 (floating input) is to use internal pullup resistors:
void setup() {  
  pinMode(3, INPUT_PULLUP);
  pinMode(6, OUTPUT);
  attachInterrupt(1, int1ISR, RISING);   
  Serial.begin(115200);
}

and connect one button pin to Arduino PIN 3, and the other button pin to GND.
That will remove floating input and its consequences.

user84: After checking the queue functions that you use in your sketch, I see there is no protection against concurrent accesses in the enq and deq functions, which is a must-have if you use any of these functions from within an ISR.
I would suggest the simplest changes to these functions as follows:
void enq(TPrioQueue *queue, void *item, int priority)
{
    TPrioQEntry *newEntry=(TPrioQEntry *) malloc(sizeof(TPrioQEntry));

    if(newEntry != NULL)
    {
        newEntry->item=item;
        newEntry->prio=priority;
        newEntry->prev=NULL;
        newEntry->next=NULL;

        // Protect against concurrent accesses starting here
        uint8_t sreg = SREG;
        cli(); 
        if(queue->head==NULL)
            queue->head=newEntry;
        else
        {
            // Find insertion point.

            // First special case: Item at head has lower priority
            if(queue->head->prio > newEntry->prio)
            {
                // Insert item at the start of the queue.
                newEntry->next=queue->head;
                queue->head->prev=newEntry;
                queue->head=newEntry;
            }
            else
            {
                // Find your insertion point
                TPrioQEntry *trav= queue->head;

                while(trav->next!=NULL && trav->prio < newEntry->prio)
                    trav=trav->next;

                newEntry->prev=trav;
                newEntry->next=trav->next;
                if(trav->next != NULL)
                    trav->next->prev=newEntry;

                trav->next=newEntry;
            }
        }

        queue->itemCount++;
        // Protect against concurrent accesses stops here
        SREG = sreg;
    }   
}

void *deq(TPrioQueue *queue)
{
    // Protect against concurrent accesses starting here
    uint8_t sreg = SREG;
    cli(); 
    // Return NULL if queue is empty
    if(queue->head == NULL)
    {
        // Protect against concurrent accesses stops here
        SREG = sreg;
        return NULL;
    }

    void *ret=queue->head->item;
    TPrioQEntry *tmp=queue->head;

    if(queue->head->next!=NULL)
        queue->head->next->prev=NULL;

    queue->head=queue->head->next;
    free(tmp);

    queue->itemCount--;

    // Protect against concurrent accesses stops here
    SREG = sreg;

    return ret;
}


7351: <p>I am trying to implement a function queue scheduling system. INT1 is connected to a button and int1task causes an LED to flash.</p>

<pre><code>typedef void (*funcptr)(void);
TPrioQueue *queue = NULL;

void int1ISR() {
  if (debounce(&amp;int1time)) // Checks if the interrupt is caused by switch bouncing
    enq(queue, (void*) int1task, 1);
}
</code></pre>

<p>Next, I execute the function in loop() by:</p>

<pre><code>typedef void (*funcptr)(void);

void loop(){
  funcptr func = (funcptr) deq(queue);
  if (func != NULL)
    (*func)(); // Stuck in a loop
}
</code></pre>

<p>For some reason, the LED keeps flashing even though I have not pressed the button. Why does this happen?</p>

<p>When I remove the line below, the ISR works properly.</p>

<pre><code>enq(queue, (void*) int1task, 1)
</code></pre>

<p>The relevant sections of the code:</p>

<pre><code>int debounce(unsigned long *debTimer) {
  unsigned long tmp = *debTimer;
  unsigned long currTime = millis();

  if ((currTime-tmp) &gt; 500){
    *debTimer = currTime;
    return 1;
  }
  else
    return 0;
}

// Flashes LED at pin 6 five times at 2 Hz
void int1task(){
  for(int i=0; i&lt;5; i++){
    digitalWrite(6, HIGH);
    delay(250);
    digitalWrite(6, LOW);
    delay(250);
  }
}

void setup() {
  pinMode(6, OUTPUT);
  attachInterrupt(1, int1ISR, RISING);
  Serial.begin(115200);
}
</code></pre>

<p><a href="https://www.dropbox.com/sh/l6byqdd4r32rapd/AAD8Pxj7iiSoRjQvksr_U3o8a?dl=0" rel="nofollow noreferrer">Code I used for the queue</a></p>

<p>Here is the <a href="https://www.dropbox.com/s/51f8174815zycrg/schematic.pdf?dl=0" rel="nofollow noreferrer">schematic</a> I am using:</p>

<p><img src="https://i.stack.imgur.com/WJRhr.png" alt="Enter image description here"></p>
 84: <p>Although you did not clearly mention nor show your wiring, it is possible the problem comes from here.</p>

<p>Barring <strong>real concurrency issues</strong> with your queue implementation, which I already mentioned in another answer, for which fixes have to be implemented anyway, there are two potential wiring problems in your circuit:</p>

<ol>
<li>You use <strong>INT1</strong> interrupt; that means you must connect your button to <strong>PIN 3</strong> for a UNO, any other pin won't work.</li>
<li>You did not specify <code>pinMode(3, INPUT_PULLUP)</code> in your <code>setup()</code>, which means <strong>you MUST use an external pullup resistor</strong> to force a <code>HIGH</code> value, and connect the other pin of the button to <code>GND</code> (or conversely: put a pulldown resistor and wire the other pin to <code>5V</code>); otherwise, while the button is left unpressed, you will have a <strong>floating input</strong>, which means its value will vary randomly between HIGH and LOW, triggering your <code>int1ISR</code> many times when you don't expect it.</li>
</ol>

<p>The simplest way to solve point 2 (floating input) is to use internal pullup resistors:</p>

<pre><code>void setup() {  
  pinMode(3, INPUT_PULLUP);
  pinMode(6, OUTPUT);
  attachInterrupt(1, int1ISR, RISING);   
  Serial.begin(115200);
}
</code></pre>

<p>and connect one button pin to Arduino PIN 3, and the other button pin to <code>GND</code>.</p>

<p>That will remove floating input and its consequences.</p>
 84: <p>After checking the queue functions that you use in your sketch, I see there is no protection against concurrent accesses in the <code>enq</code> and <code>deq</code> functions, which is a <strong>must-have</strong> if you use any of these functions from <strong>within an ISR</strong>.</p>

<p>I would suggest the simplest changes to these functions as follows:</p>

<pre><code>void enq(TPrioQueue *queue, void *item, int priority)
{
    TPrioQEntry *newEntry=(TPrioQEntry *) malloc(sizeof(TPrioQEntry));

    if(newEntry != NULL)
    {
        newEntry-&gt;item=item;
        newEntry-&gt;prio=priority;
        newEntry-&gt;prev=NULL;
        newEntry-&gt;next=NULL;

        // Protect against concurrent accesses starting here
        uint8_t sreg = SREG;
        cli(); 
        if(queue-&gt;head==NULL)
            queue-&gt;head=newEntry;
        else
        {
            // Find insertion point.

            // First special case: Item at head has lower priority
            if(queue-&gt;head-&gt;prio &gt; newEntry-&gt;prio)
            {
                // Insert item at the start of the queue.
                newEntry-&gt;next=queue-&gt;head;
                queue-&gt;head-&gt;prev=newEntry;
                queue-&gt;head=newEntry;
            }
            else
            {
                // Find your insertion point
                TPrioQEntry *trav= queue-&gt;head;

                while(trav-&gt;next!=NULL &amp;&amp; trav-&gt;prio &lt; newEntry-&gt;prio)
                    trav=trav-&gt;next;

                newEntry-&gt;prev=trav;
                newEntry-&gt;next=trav-&gt;next;
                if(trav-&gt;next != NULL)
                    trav-&gt;next-&gt;prev=newEntry;

                trav-&gt;next=newEntry;
            }
        }

        queue-&gt;itemCount++;
        // Protect against concurrent accesses stops here
        SREG = sreg;
    }   
}

void *deq(TPrioQueue *queue)
{
    // Protect against concurrent accesses starting here
    uint8_t sreg = SREG;
    cli(); 
    // Return NULL if queue is empty
    if(queue-&gt;head == NULL)
    {
        // Protect against concurrent accesses stops here
        SREG = sreg;
        return NULL;
    }

    void *ret=queue-&gt;head-&gt;item;
    TPrioQEntry *tmp=queue-&gt;head;

    if(queue-&gt;head-&gt;next!=NULL)
        queue-&gt;head-&gt;next-&gt;prev=NULL;

    queue-&gt;head=queue-&gt;head-&gt;next;
    free(tmp);

    queue-&gt;itemCount--;

    // Protect against concurrent accesses stops here
    SREG = sreg;

    return ret;
}
</code></pre>
