Lähettimen lähdekoodi V2

From Pessin randon wiki
Revision as of 14:53, 3 October 2025 by Exf (talk | contribs)

Versiohistoria

2.0

Lisätty CRC-16 tarkistussummafunktio. Lähettää singalin 4-10 sekunnin välein, jolla tarkoitus pienentää paristojen kulutusta. Voisi implementoida suoraan virransäästöominaisuudet.

2.1

Poistettu ylimääräiset kommentit

#include <CRC16.h>  // include CRC16 library
#include <RH_ASK.h>  // include RadioHead library

RH_ASK rf_driver(1000, 11, 12, 10); 	// speed = 1000 bits/sec, tx pin = 12

byte data[4];                     // create byte array to store DIP switch values
uint16_t checksumValue;           // variable to store the checksum value

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A0));   // seed the random number generator with noise from an unconnected analog pin
  pinMode(2, INPUT_PULLUP);     // set DIP switch pins as input pull-up
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

  data[0] = digitalRead(2);      // read DIP switches and store in the array
  data[1] = digitalRead(3);
  data[2] = digitalRead(4);
  data[3] = digitalRead(5);

  CRC16 crc;                     // create an instance of CRC16 class with default parameters (you can customize them if needed)
  crc.add(data, sizeof(data));    // add the DIP switch values to the CRC calculation
  checksumValue = crc.calc();    // calculate CRC-16 checksum value for the DIP switch values

  rf_driver.init();             // initialize the radio driver
}

void loop() {

  rf_driver.send((uint8_t*)&checksumValue, sizeof(checksumValue)); // send the checksum value first
  rf_driver.waitPacketSent();                                     // wait for the packet to be sent
  rf_driver.send(data, sizeof(data)); // then send the DIP switch values
  rf_driver.waitPacketSent();         // wait for the packet to be sent
  delay(random(4000, 10000));          // add random delay of between 4-10 seconds before transmitting again
}