کد کنترل دو LED با NRF و آردوینو
کد فرستنده :
کد:
#include <RH_NRF24.h>
RH_NRF24 nrf24(9, 10);
void setup()
{
Serial.begin(9600);
if (!nrf24.init())
Serial.println("init failed");
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
}
void loop()
{
if (Serial.available() > 0) {
Serial.println(c);
if (c == '1') {
Serial.println("Sending to nrf24_server");
uint8_t data[] = "1";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
}
else if (c == '2') {
Serial.println("Sending to nrf24_server");
uint8_t data[] = "2";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
}
else {
Serial.println("unknown character");
}
}
}
کد گیرنده :
کد:
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24(9, 10); // For RFM73 on Anarduino Mini
void setup()
{
Serial.begin(9600);
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
if (nrf24.available())
{
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len))
{
String str = (char*)buf;
if (str == "1") {
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
}
if (str == "2") {
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}
}
}
}