Interrupt HELP!!

hello,
Im a noob at using the arduino. When I run this program I get stuck in the interrupt please help.

carchecker.ino (578 Bytes)

The code will have issues but it does look ok.

How do you know it gets "stuck", can you see inside the CPU die because there is no indication to a human one way or the other.


Rob

when a button is pressed it stops saying looping and does nothing. doesnt even ass the 1 to the counter. Any help please

How do you know that, are you psychic? Here is your code

/* CTPR 03/16/2014

    Funtion for sensing if traffic is on a bridge. 
    Counts cars on vs cars off bridge on each lane 
    of trafic. note we are counting all rows of 
    tires going on and off bridge.
*/

int countcars=0;
volatile int state = LOW;

void setup()
{
  attachInterrupt(0, onbridge, RISING);//when pin 2 goes from low to high do onbridge
  attachInterrupt(1, offbridge, RISING);//When pin 3 goes from low to high do offbridge
}

void loop()
{
  //do things that need to be done here
}

void onbridge()
{
  countcars++;
}

void offbridge()
{
  countcars--;
}

Please point out the part that allows you to tell what the program is doing.


Rob

How do you know it gets "stuck", can you see inside the CPU die because there is no indication to a human one way or the other.

Your funny Rob. :grin:

Compared to an Arduino cars move at a snail's pace. There is no need for the complexity of interrupts.

...R

Robin2:
Compared to an Arduino cars move at a snail's pace. There is no need for the complexity of interrupts.

...R

a). Interrupts are COOL.
b). Interrupts are a great way of not having to do other stuff. Just catch the interrupt and increment.
c). Interrupts are a great way of learning that "volatile" doesn't help when your variable is being modified in the MIDDLE of an operation.

jfhaugh:
a). Interrupts are COOL.
b). Interrupts are a great way of not having to do other stuff. Just catch the interrupt and increment.
c). Interrupts are a great way of learning that "volatile" doesn't help when your variable is being modified in the MIDDLE of an operation.

They are cool if you are competent to debug code that uses them. Otherwise they are a nightmare. A lot of newbies here seem to have problems with debugging.

They are cool if the rest of your code is competent. Otherwise they just make the spaghetti even harder to disentangle.

Not sure what you mean by c). It sounds like evidence of the debugging nightmare. If not I would be interested to hear what you mean.

...R

Try declaring countcars as volatile int. Looks like the compiler doesn't know it can change.

Try declaring countcars as volatile int. Looks like the compiler doesn't know it can change.

This, Robin2, points out an issue that jfhaugh was alluding to. Since the value of an int can change in the middle of reading both bytes, care needs to be taken to disable interrupts while reading or writing the multibyte value.

All the above is true re volatile, need for interrupts etc but does not explain "I get stuck in the interrupt", I'd still like to know how one verifies that given the code posted. As the question has been ignored twice I'm out.


Rob

conconrob:
when a button is pressed it stops saying looping and does nothing. doesnt even ass the 1 to the counter. Any help please

If you want help with your code, you need to post the code that actually demonstrates the problem Posting code that you think is similar and might show the same problem is no use at all. None of your comments quoted above apply to the code you posted.