Skip to content

Instantly share code, notes, and snippets.

@matt448
Last active May 18, 2019 16:24
Show Gist options
  • Save matt448/dfe563623ef277a86d57 to your computer and use it in GitHub Desktop.
Save matt448/dfe563623ef277a86d57 to your computer and use it in GitHub Desktop.

Revisions

  1. matt448 revised this gist Feb 10, 2015. 1 changed file with 0 additions and 37 deletions.
    37 changes: 0 additions & 37 deletions CAN_Sender_500k_29bit.ino
    Original file line number Diff line number Diff line change
    @@ -1,37 +0,0 @@
    /////////////////////////////////////////////////////////////////////
    // This is a simple example of sending CAN messages with 29bit id's
    //
    // https://matthewcmcmillan.blogspot.com
    // Twitter: @matthewmcmillan
    //

    #include "mcp_can.h"
    #include <SPI.h>

    MCP_CAN CAN(10); //CAN CS pin

    int sensorPin = A0; //Pot for adusting value
    int sensorValue = 0;
    int cantxValue = 0;

    void setup()
    {
    Serial.begin(115200);
    // init can bus, baudrate: 500k
    if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("CAN init ok\r\n");
    else Serial.print("CAN init failed\r\n");
    }

    void loop()
    {
    sensorValue = analogRead(sensorPin); //Read the value of the pot
    cantxValue = sensorValue / 4; //Divide by 4 to get us in the 0-255 range
    Serial.print("cantxValue: ");
    Serial.print(cantxValue);
    Serial.println();
    //Create data packet for CAN message
    unsigned char canMsg[8] = {cantxValue, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    // format: (id, id_type, dlc, data buf)
    CAN.sendMsgBuf(0x17F8140E, 1, 8, canMsg);
    delay(100);
    }
  2. matt448 created this gist Feb 10, 2015.
    37 changes: 37 additions & 0 deletions CAN_Sender_500k_11bit.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    /////////////////////////////////////////////////////////////////////
    // This is a simple example of sending CAN messages with 11bit id's
    //
    // https://matthewcmcmillan.blogspot.com
    // Twitter: @matthewmcmillan
    //

    #include "mcp_can.h"
    #include <SPI.h>

    MCP_CAN CAN(10); //CAN CS pin

    int sensorPin = A0; //Pot for adusting value
    int sensorValue = 0;
    int cantxValue = 0;

    void setup()
    {
    Serial.begin(115200);
    // init can bus, baudrate: 500k
    if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("CAN init ok\r\n");
    else Serial.print("CAN init failed\r\n");
    }

    void loop()
    {
    sensorValue = analogRead(sensorPin); //Read the value of the pot
    cantxValue = sensorValue / 4; //Divide by 4 to get us in the 0-255 range
    Serial.print("cantxValue: ");
    Serial.print(cantxValue);
    Serial.println();
    //Create data packet for CAN message
    unsigned char canMsg[8] = {cantxValue, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    // format: (id, id_type, dlc, data buf)
    CAN.sendMsgBuf(0x07B, 0, 8, canMsg);
    delay(100);
    }
    37 changes: 37 additions & 0 deletions CAN_Sender_500k_29bit.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    /////////////////////////////////////////////////////////////////////
    // This is a simple example of sending CAN messages with 29bit id's
    //
    // https://matthewcmcmillan.blogspot.com
    // Twitter: @matthewmcmillan
    //

    #include "mcp_can.h"
    #include <SPI.h>

    MCP_CAN CAN(10); //CAN CS pin

    int sensorPin = A0; //Pot for adusting value
    int sensorValue = 0;
    int cantxValue = 0;

    void setup()
    {
    Serial.begin(115200);
    // init can bus, baudrate: 500k
    if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("CAN init ok\r\n");
    else Serial.print("CAN init failed\r\n");
    }

    void loop()
    {
    sensorValue = analogRead(sensorPin); //Read the value of the pot
    cantxValue = sensorValue / 4; //Divide by 4 to get us in the 0-255 range
    Serial.print("cantxValue: ");
    Serial.print(cantxValue);
    Serial.println();
    //Create data packet for CAN message
    unsigned char canMsg[8] = {cantxValue, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    // format: (id, id_type, dlc, data buf)
    CAN.sendMsgBuf(0x17F8140E, 1, 8, canMsg);
    delay(100);
    }