Why does a low input on pin 12 freeze my Arduino?

I'm fairly new to Arduino's so please talk slowly.
This code works fine until I add the interrupt portion. When I do and ground pin 12, it appears to freeze and I have to power the board down to get it to start responding. I'm guessing it has something to do with the PWM portion because I tried moving that input to A2 and it still freezes. What's going on, and how do I fix this?

Pin12_help.ino (8.87 KB)

I'm not sure if it's a problem, but throw some parenthesis around digitalRead(PIN_SW_TAIL_LIGHT) == LOW. I'm worried that it does the assignment first, then the comparison. In the first line of the function Lights().

(I don't know the precedence of = vs ==)

Second only to ',' , '=' is the lowest precedence, so that's not the problem.

I would focus on the first statement in your ISR. You're attempting a digitalRead on a pin and then immediately testing to see if it's non-zero. In all likelihood, if the switch is not closed the state of input will float. Without an external pull-down resistor, this test will always be true.

Thanks for the replies. The timer code works by itself, and the light code works by itself. When I put them together everything works except when I ground pin 12. Then things seem to freeze. I'm thinking it has something to do with the PWM pins and timer 1. I think I read that the timer uses those pins and messes up the PWM. I may just have to dim the LEDs with a resistor instead.

What Arduino are you using? This is important information which belongs in a posting.

On the Mega, pin 12 is a Timer1 pwm pin.

Sorry. It's an UNO clone.

  u8g.drawStr(20, ypos, "*****HELLO*****");

Liberal application of the F-macro is always a good choice. A choice you should consider.

djreiswig:
I'm guessing it has something to do with the PWM portion...

Let's find out...

PB1 and PB2 are the pins of interest. Those are pins 9 and 10 in Arduino vernacular.

...
#define PIN_LED_RR 9 // RR LED PWM
...
      analogWrite(PIN_LED_RR, HZ_State); // R REAR BLINK HAZ
...

Yup. You are shooting yourself in the foot.

But, the solution is fairly trivial. Get rid of all of that timer 1 stuff. The important part can, fairly easily, be converted to blink-without-delay.

The timer stuff is for a speedometer/odometer readout. Is there a better way to do that?

The ideal method is to eliminate all interrupt service routines. That requires the time through loop to be fast enough that transitions of the reed switch can be reliably captured. I suspect this snippet will get in the way...

  u8g.firstPage();
  do {
    draw_main(); 
  } while ( u8g.nextPage() );

But, if individual u8g method calls are typically fast (in comparison to reed switch transitions) then riddling your code with reed switch checks is a potential solution.

The next best method is to capture reed switch transitions using an external interrupt or a pin change interrupt.

I am thinking of using digital outputs for the lights and running a single pwm for all of the dim lights. Throw in some diodes to keep it from back feeding and it should work. I think.

Well, I guess my lack of electronic knowledge is showing. It doesn't appear that pin 12 is going to be useful for anything. I switched some outputs around and switched the LEDs to all digitalWrite. I figured I'd put the tail lights on a single PWM pin (3) and then run it through 4 diodes to each of the 4 LEDs. I guess I thought that would make the LEDs stay on dim until they were overridden by one of the digital outputs. It works with one LED and one diode, but when I add another diode I get no dim light. Do diodes mess with the PWM signal?

I'm planning on using the outputs to drive MOSFETs and run the actual LEDs off of 12v. Maybe things will work differently with this setup. I don't have the MOSFETs yet, so I can't try it out.

You are turning what is a fairly trivial fix into a morass. Focus on just turning the interrupt service routine (speed determination) into blink-without-delay. Post that code. Get advice. The rest will essentially be what you have now.

I'm listening.