working with CAN interface and interrupts - advice please?

Hello everyone :slight_smile:

This is my first post here although have been lurking for some time lol! I've been trying to understand how to code arduino devices, mainly through creating some devices for use in cars to monitor for certain CAN IDs and switching a relay or similar and have gone through some other code that people have written to try and teach myself how and why things work as they are - so far so good....

I'm now trying to write some new code which i'm not sure how to approach for the best - i'm hoping you guys might be able to give me some advice!

I have a single Nano connected to a standard CANbus shield (2515), which i want to monitor and send from .

The device should remain sleeping until it hears a specific CAN ID to say ignition on.
Once this is heard (and for as long as this is heard), it then needs to do the following:

Check for another CAN ID and check bytes 4 and 5 for specific values to confirm car is at a set speed.
If it isn't, then send a specific CAN code out.
If it is, then send a different specific CAN code out, together with another one.
This needs to be repeated approx every 500ms initially, although this might change depending on how it performs.

I was going to use an interrupt to monitor for the Ingition Can Id, however I'm still struggling to understand how i could do this when it needs to listen for two different CAN IDs - essentially it won't listen for the second Can ID for speed unless it has heard the First Can ID for ignition on. I know i can add 2 separate filters for listening for specific Can IDs but will they interfere with each other? I suspect not because the second speed ID won't be received unless the car is running but i don't know if this would be the best approach.

previous code i've done has used an interrupt to keep the device in low power mode, but this sent out on one shield and listened on the other.

Sorry for the complicated first post, would appreciate any advice anyone can offer me!

I always worry when I see newbie posts talk about interrupts.

If you are seriously concerned about sleeping to minimise power consumption - which is the only reason interrupts would ever be involved here - then you will not be using a Nano but a Pro Mini which does not have the USB interface drawing current.

Your requirement to sense "ignition" status from the bus implies that cars no longer have an actual ignition switch and "ignition" and "accessories" power lines, otherwise you would simply use them to switch your system.

Thanks for the Reply Paul__B. Hopefully I can explain why i'm doing what i am!

Basically the units are permanently connected to the car to a permanent feed; one was to enable a reverse camera feature that wasn't available in earlier models and so didn't send the correct CAN messages. The second one was for ignition sense to turn on other devices in a car when the ignition is turned on or running and turns everything off when the engine is stopped and the car is locked. So these devices needed to stay permanently powered in the car and therefore need to consume as little power as possible!

In regards to the car - yes it is a keyless entry/keyless start with no keys to turn for accessory/on/start - and because the stereo doesn't have the old style memory and ignition on lives; it has a main power feed and uses CAN IDs to turn the stereo on. not useful when you wants an aftermarket head unit :slight_smile:

Finally, with regards to using the nano - as these units are in a car, they need to be cased with a shunt regulator and hidden away somewhere out of site - hence while a Pro Mini would be better to use it would be more awkward to sort out updating code!

hence while a Pro Mini would be better to use it would be more awkward to sort out updating code!

Not at all. You just bring out the five lines used for the programming adaptor - 5V, ground, TX, RX and RTS - to a suitable connector. Instead of using the USB jack. You can choose your own connector.

Otherwise the USB chip draws its own current.

You do have a valid point - definitely something to think about. To be honest if i achieve what i ultimately want to and integrate all these small units into a single one running combined code I suspect I will need something more powerful to do the job!

Based on what i have now said, would you have any recommendations/suggestions on how best to implement what i'm trying to do? I guess i can set the CAN filter to listen for both Can IDs for speed and ignition as the former should be present without the latter!

Sorry, interrupts I know, CAN I do not I am afraid. :astonished:

Thanks Paul__B!

tbh more interested in working out the best way of doing it so i guess it doesn't matter if it's CAN or on some other condition really? so while point A is true, then point B needs to be checked and if true do x else do y. if A is false then deep sleep.