From 54cb35b7a220c3ce9a763a82635381a001bb9770 Mon Sep 17 00:00:00 2001 From: smayzy Date: Sun, 1 Mar 2026 19:22:10 +0100 Subject: [PATCH] initial commit --- can.ino | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 can.ino diff --git a/can.ino b/can.ino new file mode 100644 index 0000000..a71c0a0 --- /dev/null +++ b/can.ino @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +LiquidCrystal_I2C lcd(0x27, 20, 4); + +#define CAN_CS 10 +MCP_CAN CAN(CAN_CS); + +const byte NODE_ID = 0x01; // Change to your Sevcon node ID + +void setup() { + lcd.init(); + lcd.backlight(); + + lcd.setCursor(0, 0); + lcd.print("vitesse = "); + lcd.print(" "); + lcd.print(" km/h "); + + lcd.setCursor(0, 1); + lcd.print("ubat = "); + lcd.print(" "); + lcd.print(" V "); + + Serial.begin(115200); + + while (CAN_OK != CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ)) { + Serial.println("CAN init failed, retrying..."); + delay(100); + } + + CAN.setMode(MCP_NORMAL); + Serial.println("CAN init OK"); + +} + +void loop() { + + if (CAN.checkReceive() == CAN_MSGAVAIL) { + + unsigned long rxId; + byte len = 0; + byte buf[8]; + + CAN.readMsgBuf(&rxId, &len, buf); + + switch (rxId) { + case 0x382 : { + int16_t rpm = buf[0] | (buf[1] << 8); + int v = rpm * 0.017; + Serial.print(v); + Serial.println(); + lcd.setCursor(0, 0); + lcd.print("vitesse = "); + lcd.print(v); + lcd.print(" km/h "); + delay(400); + } + //case 0x209 : { + // int16_t ubat = buf[0] | (buf[1] << 8); + // int ubatr = ubat / 16; + // Serial.print(ubat); + // Serial.println(); + // lcd.setCursor(0, 1); + // lcd.print("ubat = "); + // lcd.print(ubatr); + // lcd.print(" V "); + // delay(400); + //} + } + } +} \ No newline at end of file