Lähettimen lähdekoodi V2: Difference between revisions
From Pessin randon wiki
Created page with "=== 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.<syntaxhighlight lang="c#"> #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..." |
No edit summary |
||
| Line 2: | Line 2: | ||
==== 2.0 ==== | ==== 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.<syntaxhighlight lang="c#"> | |||
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<syntaxhighlight lang="c#"> | |||
#include <CRC16.h> // include CRC16 library | #include <CRC16.h> // include CRC16 library | ||
#include <RH_ASK.h> // include RadioHead library | #include <RH_ASK.h> // include RadioHead library | ||
| Line 28: | Line 33: | ||
checksumValue = crc.calc(); // calculate CRC-16 checksum value for the DIP switch values | checksumValue = crc.calc(); // calculate CRC-16 checksum value for the DIP switch values | ||
rf_driver.init(); // initialize the radio driver | |||
} | } | ||
| Line 54: | Line 40: | ||
rf_driver.send((uint8_t*)&checksumValue, sizeof(checksumValue)); // send the checksum value first | 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.waitPacketSent(); // wait for the packet to be sent | ||
rf_driver.send(data, sizeof(data)); // then send the DIP switch values | rf_driver.send(data, sizeof(data)); // then send the DIP switch values | ||
rf_driver.waitPacketSent(); // wait for the packet to be sent | 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 | delay(random(4000, 10000)); // add random delay of between 4-10 seconds before transmitting again | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 14:53, 3 October 2025
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
}