Lähettimen lähdekoodi v1

From Pessin randon wiki
Revision as of 14:35, 3 October 2025 by Exf (talk | contribs) (Created page with "Lähettimen lähdekoodi V1<syntaxhighlight lang="c#"> #include <RH_ASK.h> #include <SPI.h> RH_ASK driver(1000); #define TX_PIN 12 #define DIP1 2 #define DIP2 3 #define DIP3 4 #define DIP4 5 byte crc8_dallas(const String &data) { byte crc = 0x00; for (int i = 0; i < data.length(); i++) { crc ^= data[i]; for (int j = 0; j < 8; j++) { crc = (crc & 0x80) ? (crc << 1) ^ 0x31 : (crc << 1); } } return crc; } void setup() { driver.init(); pinMod...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Lähettimen lähdekoodi V1

#include <RH_ASK.h>
#include <SPI.h>

RH_ASK driver(1000);
#define TX_PIN 12

#define DIP1 2
#define DIP2 3
#define DIP3 4
#define DIP4 5

byte crc8_dallas(const String &data) {
  byte crc = 0x00;
  for (int i = 0; i < data.length(); i++) {
    crc ^= data[i];
    for (int j = 0; j < 8; j++) {
      crc = (crc & 0x80) ? (crc << 1) ^ 0x31 : (crc << 1);
    }
  }
  return crc;
}

void setup() {
  driver.init();
  pinMode(DIP1, INPUT_PULLUP);
  pinMode(DIP2, INPUT_PULLUP);
  pinMode(DIP3, INPUT_PULLUP);
  pinMode(DIP4, INPUT_PULLUP);
  pinMode(TX_PIN, OUTPUT);
  randomSeed(analogRead(0));
}

String readDipCode() {
  String code = "";
  code += (digitalRead(DIP1) == LOW) ? '1' : '0';
  code += (digitalRead(DIP2) == LOW) ? '1' : '0';
  code += (digitalRead(DIP3) == LOW) ? '1' : '0';
  code += (digitalRead(DIP4) == LOW) ? '1' : '0';
  return code;
}

void loop() {
  String code = readDipCode();
  String core = "S" + code + "E";
  byte crc = crc8_dallas(core);
  String message = core + String(crc, HEX);

  driver.send((uint8_t*)message.c_str(), message.length());
  driver.waitPacketSent();

  delay(random(800, 1800));  // satunnainen lähetysväli
}