#include <SoftwareSerial.h>
#define DEBUG // Comment this out for release version to get rid of the debug print statements
#define LedDispSoftSerRxPin 11
#define LedDispSoftSerTxPin 12
#define LedDispSoftSerBaudRate 9600
SoftwareSerial LedDispSoftSer(LedDispSoftSerRxPin, LedDispSoftSerTxPin);
#define DE_PIN 10
#define RE_PIN 14
const int16_t interval = 1000; // interval (in milliseconds) between to measurements
// Define the data to send to the LED display
const uint8_t header = 0x5A; // byte 0
const uint8_t address = 0xFA; // byte 1
const uint8_t byte2 = 0x00;
const uint8_t byte3 = 0x00;
// uint8_t byte4 = (data2 / 10000) % 100;
// uint8_t byte5 = (data2 / 100) % 100;
// uint8_t byte6 = data2 % 100;
// uint8_t checksum = header + address + byte2 + byte3 + byte4 + byte5 + byte6; // Keep only the lower 8 bits
#ifdef DEBUG
void printPaddedUnsignedDecimal(int Value)
{
char buffer[6];
sprintf(buffer, "%02u", Value);
Serial.println(buffer);
}
#endif
uint32_t GetData(int whateverNeeded){
// do here what has to be done to get the data
// you want to send to he LEDs
// using the whateverNeeded parameter(s)
int32_t data = 506070;
return data;
}
void SendDataToLEDs(uint32_t data){
// do here what has to be done to send the data
uint8_t byte4 = (data / 10000) % 100;
uint8_t byte5 = (data / 100) % 100;
uint8_t byte6 = data % 100;
uint8_t checksum = header + address + byte2 + byte3 + byte4 + byte5 + byte6;
digitalWrite(DE_PIN, HIGH);
digitalWrite(RE_PIN, HIGH);
LedDispSoftSer.write(header);
LedDispSoftSer.write(address);
LedDispSoftSer.write(byte2);
LedDispSoftSer.write(byte3);
LedDispSoftSer.write(byte4);
LedDispSoftSer.write(byte5);
LedDispSoftSer.write(byte6);
LedDispSoftSer.write(checksum);
// Set the MAX485 to receive mode
digitalWrite(DE_PIN, LOW);
digitalWrite(RE_PIN, LOW);
#ifdef DEBUG
Serial.print("Header : ");
printPaddedUnsignedDecimal(header);
Serial.print("address : ");
printPaddedUnsignedDecimal(address);
Serial.print("byte2 : ");
printPaddedUnsignedDecimal(byte2);
Serial.print("byte3 : ");
printPaddedUnsignedDecimal(byte3);
Serial.print("byte4 : ");
printPaddedUnsignedDecimal(byte4);
Serial.print("byte5 : ");
printPaddedUnsignedDecimal(byte5);
Serial.print("byte6 : ");
printPaddedUnsignedDecimal(byte6);
Serial.print("checksum : ");
printPaddedUnsignedDecimal(checksum);
Serial.println();
#endif
}
uint32_t data = 506070;
void setup() {
Serial.begin(9600);
// Set up the UART interface
LedDispSoftSer.begin(LedDispSoftSerBaudRate);
pinMode(DE_PIN, OUTPUT);
pinMode(RE_PIN, OUTPUT);
digitalWrite(DE_PIN, LOW);
digitalWrite(RE_PIN, LOW);
// Do the other settings here
SendDataToLEDs(data);
}
void loop() {
static uint64_t startTime = 0; // unsigned long "time keeper"
if (millis() - startTime >= interval) {
startTime += interval;
// do relaxed things here
// int whateverNeeded = 1; // represents the parameter(s) required by the GetData function
// uint32_t data = GetData(whateverNeeded);
// SendDataToLEDs(data);
}
if(Serial.available()) //if data available
{
String Message = Serial.readStringUntil('\n');
Message.trim(); // remove any \r \n whitespace at the end of the String
data = Message.toInt();
SendDataToLEDs(data);
}
}