I have been working on some code where my desired outcome is to to trap an error condition from CAN Bus messages and light an LED.
Specifically, I want to light an LED when CAN-Bus message ID 2DE has the hex data FF in the 4th byte.
I am utilising a SparkFun RedBoard in conjunction with the SparkFun CAN-Bus shield and I have been connecting the LED between pin 13 and ground.
I have some code which seems to work, in that it will write "Error detected" to the serial monitor and turn on the LED when message ID 2DE with FF in byte four is received.
My trouble started with the recognition that the LED was not very bright which I deduced is probably caused by it being turned on and off repeatedly with very narrow pulses. I decided to relocate the LED to pin 11 and try some different approaches when I ran into a brick wall. Just as soon as I change the configuration at line 10 (const int ledPin = X;) from pin 13 to pin 11, the LED is "hard on" constantly. It is now very bright, but it doesn't switch off when the error condition is absent.
I am still seeing "Error detected" on the serial monitor, so the code is mostly working OK, but the LED is not.... Something else has priority over this pin it seems.
I am a complete newbie to Arduino and software development, though I have fiddled with both analogue and digital electronics for many years. I have a gaping hole in my understanding here though. I've tried researching this with the aid of Google and FAQs to no avail and I don't know whether I have a bad coding problem, or I am misunderstanding how the Arduino hardware functions.
One possibility is that the CAN-Bus shield is already using the pin, but I cannot find anything which might confirm or deny this notion.
Here is my sketch:
/****************************************************************************
Used for development of Nissan EPAS for Fraser deJoux
*************************************************************************/
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
const int ledPin = 13; //Led connected to pin 13
//********************************Setup Loop*********************************//
void setup() {
Serial.begin(9600); // For debug use
Serial.println("CAN Read - Testing receipt of CAN Bus message");
delay(1000);
pinMode(ledPin, OUTPUT); //Set digital pin 13 to OUTPUT
if(Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok");
else
Serial.println("Can't init CAN");
delay(1000);
}
//********************************Main Loop*********************************//
void loop(){
tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
if(message.id == 0x2DE and message.data[4] == 0xFF) //Test for PID 2DE with data FF in the 4th byte
{
Serial.println("Error detected");
digitalWrite(ledPin, HIGH); //Set LED on
}
else
{
digitalWrite(ledPin, LOW); //Set LED off
}
}
}
}
Any assistance or suggestions would be welcome