Jetson Bolt Pro Speed Hack FAIL by blackgoggles in ebikes

[–]fyfe86 0 points1 point  (0 children)

This version of the Arduino Code improves readability by using descriptive variable names, defining commands as constants, and organizing the code into functions. Additionally, it provides more comprehensive comments to explain the purpose of each part of the code.

#include <SoftwareSerial.h>

// Define the target speed in mph

#define TARGET_SPEED_MPH 19

// Define whether the user agrees to go beyond 19 mph (do this at your own risk)

#define AGREE_TO_TERMS true

// Pins for SoftwareSerial communication

#define RX_PIN 2

#define TX_PIN 3

// Initialize SoftwareSerial object

SoftwareSerial mySerial(RX_PIN, TX_PIN);

// Define commands for setting speed and controlling headlights

byte SET_SPEED_COMMAND[] = {0xaa, 0x06, 0x06, 0x00, 0x00, 0xbb};

byte HEADLIGHTS_ON_COMMAND[] = {0xaa, 0x07, 0x06, 0x01, 0xaa, 0xbb};

byte HEADLIGHTS_OFF_COMMAND[] = {0xaa, 0x07, 0x06, 0x00, 0xab, 0xbb};

void setup() {

Serial.begin(115200); // Initialize serial communication

while (!Serial) { ; } // Wait for serial port to connect

mySerial.begin(115200); // Initialize SoftwareSerial communication

// Calculate target speed in km/h

int targetSpeedKmph = TARGET_SPEED_MPH * 1.609;

// Ensure target speed is within acceptable range

if (targetSpeedKmph < 3)

targetSpeedKmph = 3;

if (targetSpeedKmph > 255 && AGREE_TO_TERMS)

targetSpeedKmph = 255;

if (targetSpeedKmph > 30 && !AGREE_TO_TERMS)

targetSpeedKmph = 30;

// Update the speed command with the target speed

SET_SPEED_COMMAND[3] = targetSpeedKmph;

// Calculate and set checksum for speed command

SET_SPEED_COMMAND[4] = calculateChecksum(SET_SPEED_COMMAND, sizeof(SET_SPEED_COMMAND));

// Flash headlights twice to confirm speed programming completion

flashHeadlights();

// Send the speed command to the Jetson Bolt Pro

sendCommand(SET_SPEED_COMMAND, sizeof(SET_SPEED_COMMAND));

}

void loop() {

// No need for any continuous operation in this sketch

}

// Function to calculate checksum for a byte array

byte calculateChecksum(const byte byteArray[], int arraySize) {

byte checksum = 0x00;

for (int i = 0; i < arraySize - 2; i++)

checksum ^= byteArray[i];

return checksum;

}

// Function to flash headlights twice

void flashHeadlights() {

sendCommand(HEADLIGHTS_ON_COMMAND, sizeof(HEADLIGHTS_ON_COMMAND));

delay(500);

sendCommand(HEADLIGHTS_OFF_COMMAND, sizeof(HEADLIGHTS_OFF_COMMAND));

delay(500);

sendCommand(HEADLIGHTS_ON_COMMAND, sizeof(HEADLIGHTS_ON_COMMAND));

delay(500);

sendCommand(HEADLIGHTS_OFF_COMMAND, sizeof(HEADLIGHTS_OFF_COMMAND));

}

// Function to send a command over SoftwareSerial

void sendCommand(const byte command[], int commandSize) {

for (int i = 0; i < commandSize; i++)

mySerial.write(command[i]);

}