-
Notifications
You must be signed in to change notification settings - Fork 1
/
virtual_ECU_mcp2515.ino
59 lines (46 loc) · 1.26 KB
/
virtual_ECU_mcp2515.ino
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
#include <SPI.h>
#include <mcp2515.h>
#include <stdlib.h>
#define potPin A0
int potvalue=0;
int new_potvalue=0;
int LED = 3;
int LED1 = 4;
struct can_frame canMsg;
struct can_frame canMsg1;
MCP2515 mcp2515(10);
void setup() {
canMsg1.can_id = 0x0F6;
canMsg1.can_dlc = 2;
//canMsg1.data[0] = 0x8E;
canMsg1.data[1] = 0x87;
while (!Serial);
Serial.begin(115200);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS);
mcp2515.setNormalMode();
//Serial.println("Example: Write to CAN");
//Serial.println("------- CAN Read ----------");
//Serial.println("ID DLC DATA");
}
void loop() {
potvalue = analogRead(potPin);
potvalue = map(potvalue,0,2047,0,511);
analogWrite (LED, potvalue);
//Serial.println(potvalue);
canMsg1.data[0] = potvalue;
//Serial.println("Messages sent");
mcp2515.sendMessage(&canMsg1);
delay(100);
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
//Serial.print(canMsg.can_id, HEX); // print ID
//Serial.print(" ");
//Serial.print(canMsg.can_dlc, HEX); // print DLC
//Serial.print(" ");
if(canMsg.can_id==0x0F6){
int x = canMsg.data[0];
//Serial.print(x);
analogWrite (LED1, x);
}
}
}