Seems to be working great,
Thanks for all of the help Crossroads,
3 questions
1, How can i set the CAN code up to look at only a single base CAN id, at the moment, regardless of CAN id, it simple recieves any message and uses the data in the melevant bits to run the lights,
2, How can i clear the message buffers at the beginning of each time through the loop? at the moment, once the CAN message is removed from the bus, the led's remain in the last message state,
3, Any way of getting the code to run faster, as i believe i can display multiple colours at a time in software as the CAN u[pdate rate is fast enough for visual deception,
#include "mcp_can.h"
#include <SPI.h>
#include <stdio.h>
#define INT8U unsigned char
INT8U Flag_Recv = 0;
INT8U len = 0;
INT8U buf[8];
char str[20];
////////////////////////////////////////// CAN Setup Above
int x = 0;
int pinsArray[] = {4,7,8,9,14,15,16,17}; // could be whatever group you select
int maskBit = 1;
int i = 0;
///////////////////////////////////////////////////// LED Setup Above
void setup()
{
for(i=0; i<8; i++)
pinMode(pinsArray[i],OUTPUT);
pinMode(3,OUTPUT); // Red PWM
pinMode(5,OUTPUT); // Green PWM
pinMode(6,OUTPUT); // Blue PWM
//////////////////////////////////////////////////////////////// LED Outputs Above
CAN.begin(CAN_500KBPS); // init can bus : baudrate = 500k
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
Serial.begin(115200);
////////////////////////////////////////////////////////CAN Begin Above
}
void MCP2515_ISR()
{
Flag_Recv = 1;
}
/////////////////////////////////////////////CAN Calling code
void loop ()
{
if(Flag_Recv) // check if get data
{
Flag_Recv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.println("CAN_BUS GET DATA!");
Serial.print("data len = ");Serial.println(len);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);Serial.print("\t");
}
Serial.println();
}
////////////////////////////////////////////////////////////////////////////////Recieve CAN Message
analogWrite(3,buf[1]);
analogWrite(5,buf[2]);
analogWrite(6,buf[3]);
////////////////////////////////////////////////////////Set RGB Colour With CAN Message
maskBit = 1; // start with B00000001
for (x=0; x<8; x=x+1){
if ((buf[0] & maskBit) >0){ // makes all bits but 1 low: 0000000C, next pass 000000C0, etc
digitalWrite (pinsArray[x], HIGH);
}
else {
digitalWrite (pinsArray[x], LOW);
}
maskBit = maskBit << 1; // next bit - B00000010, B00000100, B00001000, etc up to B10000000
}
////////////////////////////////////////////////////////////////////////////////////////////////Write LED Pattern
}// next x