I have the transmitter
#include <SPI.h>
#include <mcp_can.h>
const int spiCS = 10; // CS pin connected to CAN module
const int potPin = A0; // Analog pin connected to potentiometer
MCP_CAN CAN(spiCS); // Set CS pin
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Initializing MCP2515...");
if (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
Serial.println("MCP2515 Initialized Successfully!");
} else {
Serial.println("Error Initializing MCP2515 Module");
while (1);
}
}
void loop() {
int potValue = analogRead(potPin); // Read potentiometer value
byte buf[8] = {highByte(potValue), lowByte(potValue)}; // Create message buffer with potentiometer value
Serial.print("Potentiometer Value: ");
Serial.println(potValue);
// Send message with ID 0x123 and potentiometer value
if (CAN.sendMsgBuf(0x123, 0, 2, buf) == CAN_OK) {
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message");
}
delay(1000); // Adjust delay as needed
}
and the seriel output of it is smthn like that
Initializing MCP2515...
Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Potentiometer Value: 606
Message Sent Successfully!
and the receiving code is =
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <mcp_can.h>
const int spiCS = 10; // CS pin connected to CAN module
MCP_CAN CAN(spiCS); // Set CS pin
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address to 0x27
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Initializing LCD...");
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on backlight
Serial.println("LCD Initialized!");
Serial.println("Entering Configuration Mode...");
if (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
Serial.println("CAN BUS Initialized Successfully!");
} else {
Serial.println("Error Initializing MCP2515 Module");
while (1);
}
Serial.println("Starting to Set Mask...");
CAN.init_Mask(0, 0, 0x7FF);
Serial.println("Mask Set Successfully!");
Serial.println("Starting to Set Filter...");
CAN.init_Filt(0, 0, 0x123); // Setting filter to accept messages with ID 0x123
Serial.println("Filter Set Successfully!");
}
void loop() {
unsigned char len = 0;
unsigned char buf[8];
unsigned long id;
int canReceiveStatus = CAN.checkReceive(); // Check if message available
Serial.print("CAN Receive Status: ");
Serial.println(canReceiveStatus);
if (canReceiveStatus == CAN_MSGAVAIL) { // Message available
CAN.readMsgBuf(&id, &len, buf); // Read message into buffer
Serial.print("Received Message ID: ");
Serial.println(id, HEX);
if (id == 0x123) { // Check if message ID is correct
int potValue = (buf[0] << 8) | buf[1]; // Reconstruct analog value from received data
Serial.print("Received Potentiometer Value: ");
Serial.println(potValue);
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0);
lcd.print("Pot Value: ");
lcd.setCursor(0, 1);
lcd.print(potValue); // Display potentiometer value
Serial.println("Potentiometer Value Displayed on LCD!");
}
} else {
Serial.println("No Message Received");
}
delay(100000); // Adjust delay as needed
}
output of it is
CAN Receive Status: 4
No Message Received
Can you see any problem here Use code tags to format code for the forum