LIN BUS Master Arduino Nano connect to TLE7259-3GE

#include <LIN_master.h>

// Define LIN ID for power window control
#define LIN_ID_POWER_WINDOW 0x15

// Define data values for power window control
#define POWER_WINDOW_UP_CMD 0x01
#define POWER_WINDOW_DOWN_CMD 0x02

// Define pin numbers for buttons
#define BUTTON_FR_LEFT_PIN 2
#define BUTTON_FR_RIGHT_PIN 3
#define BUTTON_RR_LEFT_PIN 4
#define BUTTON_RR_RIGHT_PIN 5

// Instantiate LIN_Master object
LIN_Master linMaster;

// Variable to store the previous state of buttons
bool prevButtonStateFrLeft = HIGH;
bool prevButtonStateFrRight = HIGH;
bool prevButtonStateRrLeft = HIGH;
bool prevButtonStateRrRight = HIGH;

void setup() {
  // Begin serial communication
  Serial.begin(9600);

  // Initialize LIN communication
  linMaster.begin(19200, LIN_V2, false); // Baudrate 19200, LIN version 2, no background operation

  // Initialize button pins as inputs
  pinMode(BUTTON_FR_LEFT_PIN, INPUT_PULLUP);
  pinMode(BUTTON_FR_RIGHT_PIN, INPUT_PULLUP);
  pinMode(BUTTON_RR_LEFT_PIN, INPUT_PULLUP);
  pinMode(BUTTON_RR_RIGHT_PIN, INPUT_PULLUP);
}

void loop() {
  // Read current state of buttons
  bool currentButtonStateFrLeft = digitalRead(BUTTON_FR_LEFT_PIN);
  bool currentButtonStateFrRight = digitalRead(BUTTON_FR_RIGHT_PIN);
  bool currentButtonStateRrLeft = digitalRead(BUTTON_RR_LEFT_PIN);
  bool currentButtonStateRrRight = digitalRead(BUTTON_RR_RIGHT_PIN);

  // Check if button FR_LEFT is pressed (active high)
  if (currentButtonStateFrLeft == HIGH && prevButtonStateFrLeft == LOW) {
    sendPowerWindowCommand(0, POWER_WINDOW_UP_CMD); // Start bit: 0
  } else if (currentButtonStateFrLeft == LOW && prevButtonStateFrLeft == HIGH) {
    sendPowerWindowCommand(0, POWER_WINDOW_DOWN_CMD); // Start bit: 0
  }

  // Check if button FR_RIGHT is pressed (active high)
  if (currentButtonStateFrRight == HIGH && prevButtonStateFrRight == LOW) {
    sendPowerWindowCommand(5, POWER_WINDOW_UP_CMD); // Start bit: 5
  } else if (currentButtonStateFrRight == LOW && prevButtonStateFrRight == HIGH) {
    sendPowerWindowCommand(5, POWER_WINDOW_DOWN_CMD); // Start bit: 5
  }

  // Check if button RR_LEFT is pressed (active high)
  if (currentButtonStateRrLeft == HIGH && prevButtonStateRrLeft == LOW) {
    sendPowerWindowCommand(10, POWER_WINDOW_UP_CMD); // Start bit: 10
  } else if (currentButtonStateRrLeft == LOW && prevButtonStateRrLeft == HIGH) {
    sendPowerWindowCommand(10, POWER_WINDOW_DOWN_CMD); // Start bit: 10
  }

  // Check if button RR_RIGHT is pressed (active high)
  if (currentButtonStateRrRight == HIGH && prevButtonStateRrRight == LOW) {
    sendPowerWindowCommand(15, POWER_WINDOW_UP_CMD); // Start bit: 15
  } else if (currentButtonStateRrRight == LOW && prevButtonStateRrRight == HIGH) {
    sendPowerWindowCommand(15, POWER_WINDOW_DOWN_CMD); // Start bit: 15
  }

  // Update previous button states
  prevButtonStateFrLeft = currentButtonStateFrLeft;
  prevButtonStateFrRight = currentButtonStateFrRight;
  prevButtonStateRrLeft = currentButtonStateRrLeft;
  prevButtonStateRrRight = currentButtonStateRrRight;
}

// Function to send power window control command via LIN
void sendPowerWindowCommand(uint8_t startBit, uint8_t command) {
  // Data to be sent
  uint8_t data[] = {startBit, 0x01, command}; // Start bit, Data length, Command

  // Send a master request frame with LIN header
  linMaster.sendMasterRequest(LIN_ID_POWER_WINDOW, sizeof(data), data);

  // Print the data being sent
  Serial.print("Sent LIN frame: ");
  for (int i = 0; i < sizeof(data); i++) {
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
}

can anyone have idea how to trial or simulation to receive the output of my program?Use code tags to format code for the forum

I moved your topic to an appropriate forum category @ckardhi.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Posting your schematic would help.

#include <lin_bus.h>

// Define LIN ID for power window control
#define LIN_ID_POWER_WINDOW 0x15

// Define data values for power window control
#define POWER_WINDOW_UP_CMD 0x01
#define POWER_WINDOW_DOWN_CMD 0x02

// Define pin numbers for LEDs
#define LED_FRONT_LEFT_PIN 10
#define LED_FRONT_RIGHT_PIN 9
#define LED_REAR_LEFT_PIN 12
#define LED_REAR_RIGHT_PIN 11

// Instantiate LIN_Master object
lin_bus linBus(Serial, LIN_V2, 2, 3, 4); // Menggunakan Serial untuk komunikasi LIN

void setup() {
  // Initialize LIN communication
  linBus.slave(19200, LIN_ID_POWER_WINDOW); // Baudrate 19200, LIN ID 0x15
  // Initialize LED pins as outputs
  pinMode(LED_FRONT_LEFT_PIN, OUTPUT);
  pinMode(LED_FRONT_RIGHT_PIN, OUTPUT);
  pinMode(LED_REAR_LEFT_PIN, OUTPUT);
  pinMode(LED_REAR_RIGHT_PIN, OUTPUT);
}

void loop() {
  // Read data from LIN bus if available
  uint8_t data[8];
  int bytesRead = linBus.listen(LIN_ID_POWER_WINDOW, data, sizeof(data));

  // If data is received successfully
  if (bytesRead > 0) {
    // Process received data
    processLINData(data, bytesRead);
  }
}

// Function to process received LIN data
void processLINData(uint8_t *data, int length) {
  // Check if the received data corresponds to a power window control command
  if (length == 3) {
    // Extract the start bit from the first byte of data
    uint8_t startBit = data[0];
    uint8_t command = data[2];
    // Process the command based on the start bit
    switch (startBit) {
      case 0:
        // Front Left
        digitalWrite(LED_FRONT_LEFT_PIN, (command == POWER_WINDOW_UP_CMD) ? HIGH : LOW);
        break;
      case 5:
        // Front Right
        digitalWrite(LED_FRONT_RIGHT_PIN, (command == POWER_WINDOW_UP_CMD) ? HIGH : LOW);
        break;
      case 10:
        // Rear Left
        digitalWrite(LED_REAR_LEFT_PIN, (command == POWER_WINDOW_UP_CMD) ? HIGH : LOW);
        break;
      case 15:
        // Rear Right
        digitalWrite(LED_REAR_RIGHT_PIN, (command == POWER_WINDOW_UP_CMD) ? HIGH : LOW);
        break;
      default:
        // Invalid start bit
        break;
    }
  }
}

I want to connect two Arduino Nano boards using LINBUS @gilshultz

If you do not want to post the schematic fine then you have my answer at this point. There are several ways to accomplish what way are you are trying to do.

can you tell me, what should i do, can i use PROTEUS ? @gilshultz

why i can't monitoring my data on terminal?

Part of the schematic is missing and no power source.

You can monitor data on the monitor, just remember it behaves like a TTY, no formatting capability.

I think not because of that

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.