-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver-Motor
122 lines (104 loc) · 2.54 KB
/
Receiver-Motor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Chat GPT code for motor controller UNO
*/
#include <SPI.h>
#include <mcp_can.h>
#define CAN0_INT 2 // Set INT pin for CAN shield to pin 2
MCP_CAN CAN0(10); // Set CS pin for CAN shield to pin 10
// Define the pin numbers
const int pin1 = 1;
const int pin2 = 2;
const int pin3 = 3;
const int pin4 = 4;
const int pin5 = 5;
const int pin6 = 6;
const int inputPin = 7;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize CAN bus communication
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
Serial.println("CAN BUS Shield init OK!");
} else {
Serial.println("CAN BUS Shield init FAIL!");
while (1);
}
// Set CAN interrupt pin
pinMode(CAN0_INT, INPUT);
// Set pin modes for digital pins
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(inputPin, INPUT);
// Enable CAN bus communication
CAN0.setMode(MCP_NORMAL);
}
void loop() {
unsigned long canId;
uint8_t len = 0;
uint8_t buf[8];
// Check for CAN bus message
if (!digitalRead(CAN0_INT)) {
CAN0.readMsgBuf(&canId, &len, buf);
handleCANMessage(canId, buf, len);
}
// Check input pin status
if (digitalRead(inputPin) == HIGH) {
sendCANMessage();
}
}
void handleCANMessage(unsigned long canId, uint8_t *buf, uint8_t len) {
// Process the received CAN message
switch (canId) {
case 0x501:
if (buf[0] == 0x00) {
digitalWrite(pin1, HIGH);
} else if (buf[0] == 0x01) {
digitalWrite(pin1, LOW);
}
break;
case 0x502:
if (buf[0] == 0x10) {
digitalWrite(pin2, HIGH);
} else if (buf[0] == 0x20) {
digitalWrite(pin2, LOW);
}
break;
case 0x503:
if (buf[0] == 0x10) {
digitalWrite(pin3, HIGH);
} else if (buf[0] == 0x20) {
digitalWrite(pin3, LOW);
}
break;
case 0x510:
if (buf[0] == 0x10) {
digitalWrite(pin4, HIGH);
} else if (buf[0] == 0x20) {
digitalWrite(pin4, LOW);
}
break;
case 0x511:
if (buf[0] == 0x10) {
digitalWrite(pin5, HIGH);
} else if (buf[0] == 0x20) {
digitalWrite(pin5, LOW);
}
break;
case 0x512:
if (buf[0] == 0x10) {
digitalWrite(pin6, HIGH);
} else if (buf[0] == 0x20) {
digitalWrite(pin6, LOW);
}
break;
}
}
void sendCANMessage() {
// Send CAN message with ID 0x30
unsigned char msg[1] = {0x30};
CAN0.sendMsgBuf(0x30, 0, 1, msg);
}