Here is a code made by Artificial Intelligence.
#include <Arduino.h>
byte data[] = {
0x20, 0xDF, 0xFF, 0x61, 0x60, 0xFF, 0x69, 0x00,
0xFF, 0x61, 0x60, 0x00, 0x68, 0x00, 0xA6, 0xEF,
0x21, 0xDE, 0xFF, 0x61, 0x61, 0x57, 0x6A, 0x00,
0xFF, 0x61, 0x61, 0xD5, 0x6A, 0xFF, 0xE8, 0x60,
0x22, 0xDD, 0xFF, 0x61, 0x62, 0x00, 0x68, 0x00,
0xFF, 0x00, 0x61, 0x6E, 0x48, 0x00, 0x25, 0x5F,
0x23, 0xDC, 0xFF, 0x5E, 0x61, 0x64, 0x6A, 0x00,
0xFF, 0x60, 0x5F, 0x00, 0x68, 0x9A, 0xA6, 0xEB,
0x24, 0xDB, 0xFF, 0x61, 0x5E, 0x3E, 0x6A, 0x00,
0xFF, 0x61, 0x5E, 0x26, 0x6A, 0x26, 0xA6, 0x79,
0x25, 0xDA, 0xFF, 0x62, 0x60, 0x3E, 0x6A, 0x00,
0xFF, 0x00, 0x62, 0x00, 0x48, 0x00, 0x25, 0x31
};
const int RSE_PIN = 10;
const unsigned long FRAME_DELAY_US = 1019; // 573 (frame) + 446 (desired delay)
const unsigned long LOOP_DELAY_MS = 9;
const byte firstByteCycle[] = { 0x20, 0x26, 0x2C, 0x32, 0x38 };
byte cycleIndex = 0;
void setup() {
pinMode(RSE_PIN, OUTPUT);
digitalWrite(RSE_PIN, HIGH); // Always in transmit mode
Serial1.begin(19200, SERIAL_8O1); // 8 data bits, Odd parity, 1 stop bit
}
void loop() {
// Update the first byte before each transmission loop
data[0] = firstByteCycle[cycleIndex];
cycleIndex = (cycleIndex + 1) % 5;
for (byte b : data) {
unsigned long t_start = micros();
Serial1.write(b);
// Wait until full frame duration (573 us) + 446 us inter-frame gap
while (micros() - t_start < FRAME_DELAY_US) {
// Busy wait to ensure spacing
}
}
delay(LOOP_DELAY_MS); // Wait before repeating the whole transmission
}
This code works as intended but FRAME_DELAY_US has slight deviation from what is in the code. It is alternating between X us after or Y us before set value. Is there any way to make it more precise? What is the hardware limit on timing?
Second problem is that I need more accurate delay than MS for "const unsigned long LOOP_DELAY_MS". But when i change it to "const unsigned long LOOP_DELAY_US = 9000" and at the end "delay(LOOP_DELAY_US);"; Program runs as if there was no loop delay at all. Googling "FRAME_DELAY" Doesn't show any documentation or instructions about this. I'd like to understand more words used in this particular code.
Thank you for advices in Advance