PerryBebbington:
Hello jwallis,
Almost the same question came up a day or 2 ago
Why are you using an interrupt anyway? It is a common mistake to use interrupts for inputs when they are not the correct tool to use.
I have convinced myself that it is not the printing that is my problem. As noted above, my issue was the AC mains.
For my actual project, I am simply sampling the D2 pin, and I am seeing the value of the pin generally be TRUE (INPUT_PULLUP is enabled), but randomly, avergage of every .2 sec, it goes to FALSE
I am using an interrupt because I am doing a lot of work in my main loop, it will take between 2s and 60s to complete each loop, depending on if it needs to process incoming messages or enable GPS. For this reason I cannot sample the input fast enough to detect when D2 changes.
All I am doing in my ISR is setting 1 var to TRUE so that the main loop will know to take action.
hammy:
Using the Arduino in this environment what happens if it falsely triggers a “kill” signal when you are driving ? Could be dangerous and could well invalidate insurance after the resulting accident.
Absolutely true, and thank you for the concern. I was originally using software to control the relay that controls the car wiring but have switched to using hardware for this reason. The relay can ONLY open when both the arduino has a pin set to TRUE and the signal from the starter wire (directly off the steering column) is energized. The latter part of the circuit is not related to the arduino at all, it is directly connected to the relay. The user would have to try to start their car while moving for the relay to open. I do intend to sell this project, to a Very niche classic car market, like 1 specific model.
Grumpy_Mike:
make the 10K resistor into a 1K and also add another 1K resistor from the base of the transistor to ground.
I do not think the internal pull up resistors are strong enough for this environment. Make it an external pull up about 510R.
But I would also strong agree with what hammy and peter said. You shouldn't be using interrupts unless the rest of the code is poorly written and has lots of delays in it.
This sounds like the right track. In my second message above I admitted the issue was the mains, 60Hz, all that good stuff, so my example was very poor compared to what I'm really after. I hoped no one would respond after that. Unit A of my project, in the car, when all the components are wired together and a few inches apart from each other works great. Unit B of my project, soldered to the custom PCB I had made, where all the components are mm from each other, has this issue of the interrupt firing over and over. I think it's due to the proximity, so I think this is the right track. I just changed the pullup resistor to a 5k, and that didn't do it. I will try changing the values and adding the resistor from base to ground as you advised. I genuinely abhor this part of this discipline. Software seems so much cut and dried in a lot of ways. I don't know how you all do it, it makes me want to pull my hair out.
I am surprised that you say (it sounds like you're saying this is true in general) that one shouldn't be using interrupts. As I said above, my main loop can take on the order of minutes to complete, so without multi-threading, how can I sample a pin that will be signaled arbitrarily? Maybe I could build something like a flip flop in hardware, but my interrupt solution seems to be the most obvious. As I said above, my ISR is very short and simply sets a var to true so that the main loop will know to take action.
Paul__B:
The term "interrupt" in the subject title strongly implies a mistake!
I also find this an interesting comment that if you were someone else I'd ignore, but I am really curious what you mean by that. Is there a different word that would have more clearly described my problem? To me, in my ignorance, interrupt is a neutral word that is simply part of the system just like register, pin, etc...?
Oh... maybe you, like some others, generally consider using interrupts to be bad practice...? I'm so surprised to hear this from all of you. I will do some research on why this might be.