I used two arduino's interface with can module,input as 4 switch ,output as 3 led's.But not working.No error for code.But i think ,circuit in proteus is not correct.if i pressed 1&2 switch's the first,second led should be ON.if i pressed 3,4 the second,third led's should be On.why my circuit not working properly?
tSENDER
#include <SPI.h>
#include "mcp2515_can.h"
// CAN bus pins
const int CS_PIN = 10;
mcp2515_can CAN(CS_PIN);
int buttonState1= 0;
int buttonState2= 0;
int buttonState3= 0;
int buttonState4= 0;
const int ID1 = 0x01; // Right front door
const int ID2 = 0x02; // Right back door
const int ID3 = 0x03; // Left front door
const int ID4 = 0x04; // Left back door
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Sender");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void sendCanMessage(int id);
void loop() {
buttonState1 = digitalRead(2);
buttonState2 = digitalRead(3);
// Read the state of the push buttons and send CAN messages if necessary
if (buttonState1 == HIGH || buttonState2 == HIGH) {
// digitalWrite(LED_1, HIGH); // Right doors indicator
sendCanMessage(ID1);
sendCanMessage(ID2);
}
else{}
buttonState3= digitalRead(4);
buttonState4= digitalRead(5);
if (buttonState3 == HIGH || buttonState4 == HIGH) {
sendCanMessage(ID3);
sendCanMessage(ID4);
} else {
}
if (buttonState1 == HIGH || buttonState2 == HIGH || buttonState3 == HIGH || buttonState4 == HIGH) {
sendCanMessage(ID1);
sendCanMessage(ID2);
sendCanMessage(ID3);
sendCanMessage(ID4);
} else {
}
delay(100);
}
void sendCanMessage(int id) {
unsigned char data[] = {0x01};
if (CAN.sendMsgBuf(id, 0, 1, data) == CAN_OK) {
Serial.print("CAN message sent with ID ");
Serial.println(id);
} else {
Serial.println("Error sending CAN message");
}
}
RECEIVER
#include <SPI.h>
#include "mcp2515_can.h"
#define CAN_CS_PIN 10
mcp2515_can CAN(CAN_CS_PIN);
const int LED1_PIN = 6; // Right doors indicator
const int LED2_PIN = 7; // Left door indicator
const int LED3_PIN = 8; // Interior light indicator
//const int R_TURN_PIN = 9; // Right turn indicator
//const int L_TURN_PIN = 11; // Left turn indicator
const int ID1 = 0x01; // Right front door
const int ID2 = 0x02; // Right back door
const int ID3 = 0x03; // Left front door
const int ID4 = 0x04; // Left back door
void setup() {
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
// pinMode(R_TURN_PIN, OUTPUT);
//pinMode(L_TURN_PIN, OUTPUT);
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Sender");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void loop() {
// Check for incoming CAN messages
if (CAN_MSGAVAIL == CAN.checkReceive()) {
// Read the incoming message and check the ID
unsigned long msgId = 0;
unsigned char len = 0;
unsigned char buf[8];
if (CAN.readMsgBuf(&len, buf) == CAN_OK) {
msgId = CAN.getCanId();
if (msgId == ID1 || msgId ==ID2) {
// Right front door message
digitalWrite(LED1_PIN, buf[0]);
} else {
// Right back door message
digitalWrite(LED1_PIN, buf[0]);
} if ( msgId ==ID3|| msgId ==ID4) {
// Left front door message
digitalWrite(LED2_PIN, buf[0]);
} else {
// Left back door message
digitalWrite(LED2_PIN, buf[0]);
}
if ( msgId ==ID1|| msgId ==ID2|| msgId ==ID3|| msgId ==ID4) {
digitalWrite(LED3_PIN, buf[0]);
}
else{
digitalWrite(LED3_PIN, buf[0]);
}}
}}
ype here
i'm expecting,to get basic info about proteus and how to CAN module in proteus and what's wrong in circuit.