LoginSignup
2
1

More than 1 year has passed since last update.

arduinoでインスツルメント クラスター その2

Last updated at Posted at 2021-09-05

概要

arduino unoでcanからインスツルメント クラスターを操作した。
シリアルから、コマンドで操作してみた。

写真

CIMG2968.JPG

サンプルコード

コマンド rpm speed
a 1000 20
b 3000
c 5000
d 40
e 100
#include <SPI.h>
#include "mcp_can.h"

MCP_CAN CAN(10);
unsigned char stmp[8] = {
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0
};

void setup() {
    Serial.begin(115200);
START_INIT:
    if (CAN_OK == CAN.begin(CAN_1000KBPS))
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
    Serial.print("\n>");
}
int rpm = 3000;
int speed = 40;
void loop() {
    if (Serial.available() > 0) 
    {
        char c = Serial.read();
        if (c == 13) 
            return;
        Serial.print(c);
        switch (c) 
        {
        case 'a':
            rpm = 1000;
            speed = 20;
        break;
        case 'b':
            rpm = 3000;
        break;
        case 'c':
            rpm = 5000;
        break;
        case 'd':
            speed = 40;
        break;
        case 'e':
            speed = 100;
        break;
        default:
            rpm = 7000;
            speed = 180;
        break;
        }
        Serial.print("\n>");
    }
    stmp[0] = (rpm * 4) / 256;
    stmp[1] = (rpm * 4) % 256;
    stmp[4] = (speed * 100 + 10000) / 256;
    stmp[5] = (speed * 100 + 10000) % 256;
    CAN.sendMsgBuf(0x201, 0, 8, stmp);
    delay(100);
}





以上。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1