Minimal transmitting code

I was wondering what the most barebones code for transmitting a command if a condition is met can be over the CAN BUS could be. Hypothetically I would want to send something along the lines of: “3D 11 04 00 C3” on the CAN BUS if the speedometer reads 20 km/h. I found this:
#include “carloop/carloop.h”
Carloop carloop;

void setup()

void loop()
CANMessage message;
if(carloop.can().receive(message)) {
// Do awesome things with message!

Velocity: 0x3CA
Expressed in:[km] h
Frequency: 6.7 Hz
Minimum Timestamp: 100
But I think that I am missing something. Ideally I would want this to be standalone (so no serial printing required). What could I do?

How does one turn 8 bit messages in hex numbers?

Thank you very much

Hi and welcome!

I’m out at Maker Faire in New York for a busy weekend. I’ll write and test a working example for you as soon as possible. Stay tuned.

Here is an example of a simple transmit.

#include "application.h"
#include "carloop/carloop.h"

// Don't block the main program while connecting to WiFi/cellular.
// This way the main program runs on the Carloop even outside of WiFi range.

// Tell the program which revision of Carloop you are using.
Carloop<CarloopRevision2> carloop;

void setup() {
  // Configure the CAN bus speed for 500 kbps, the standard speed for the OBD-II port.
  // Other common speeds are 250 kbps and 1 Mbps.
  // If you don't call setCANSpeed, 500 kbps is used.

  // Connect to the CAN bus

unsigned long lastTransmitTime = 0;
const unsigned long transmitInterval = 100; /* ms */
void loop() {
  // Send a message at a regular time interval
  unsigned long now = millis();
  if (now - lastTransmitTime > transmitInterval) {
    CANMessage message;

    // A CAN message has an ID that identifies the content inside = 0x123;

    // It can have from 0 to 8 data bytes
    message.len = 8;

    // Pass the data to be transmitted in the data array[0] = 10;[1] = 20;[2] = 30;[3] = 40;

    // You can of course pass dynamic data[4] = (uint8_t)(now >> 24);[5] = (uint8_t)(now >> 16);[6] = (uint8_t)(now >> 8);[7] = (uint8_t)(now);

    // Send the message on the bus!

    lastTransmitTime = now;

Find the latest version and instructions on how to flash it to your Carloop on GitHub.

1 Like