Indice
Descrizione
In questo video scoprirai come usare dei moduli wireless nRF24L01+ con Arduino.
Tramite degli esempi imparerai come inviare i messaggi e come effettuare la comunicazione bidirezionale senza fili tra due o più schede Arduino.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 wireless(10, 9);
const byte address[6] = "00001";
const char msg[] = "Tutorial di Arduino";
void setup() {
Serial.begin(9600);
wireless.begin();
wireless.setChannel(125);
wireless.openWritingPipe(address);
wireless.setPALevel(RF24_PA_LOW);
wireless.stopListening();
}
void loop() {
bool result = wireless.write(&msg, sizeof(msg));
if (result) {
Serial.println("Comunicazione inviata correttamente");
} else {
Serial.println("Errore: messaggio non ricevuto");
}
delay(1000);
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 wireless(48, 49);
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
wireless.begin();
wireless.setChannel(125);
wireless.openReadingPipe(1, address);
wireless.setPALevel(RF24_PA_LOW);
wireless.startListening();
}
void loop() {
char msg[32] = {0};
if (wireless.available()) {
wireless.read(&msg, sizeof(msg));
Serial.println(msg);
}
delay(10);
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define POTENTIOMETER_PIN A0
#define LED_PIN 3
RF24 wireless(10, 9);
const byte addresses [][6] = {"node1", "node2"};
int ledCount = 0;
bool isDark = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
wireless.begin();
wireless.setChannel(125);
wireless.openWritingPipe(addresses[0]);
wireless.openReadingPipe(1, addresses[1]);
wireless.setPALevel(RF24_PA_MIN);
wireless.startListening();
}
void loop() {
int value = analogRead(POTENTIOMETER_PIN) / 171;
if (value != ledCount) {
ledCount = value;
wireless.stopListening();
wireless.write(&ledCount, sizeof(ledCount));
delay(100);
wireless.startListening();
}
if (wireless.available()) {
wireless.read(&isDark, sizeof(isDark));
digitalWrite(LED_PIN, isDark);
}
delay(10);
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define PHOTORESISTOR_PIN A0
#define THRESHOLD 550
RF24 wireless(48, 49);
const byte addresses [][6] = {"node1", "node2"};
int ledPins[] = {6, 5, 4, 3, 2};
int ledCount = 0;
bool isDark = false;
void setup() {
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
wireless.begin();
wireless.setChannel(125);
wireless.openWritingPipe(addresses[1]);
wireless.openReadingPipe(1, addresses[0]);
wireless.setPALevel(RF24_PA_MIN);
wireless.startListening();
}
void loop() {
bool value = analogRead(PHOTORESISTOR_PIN) < THRESHOLD;
if (value != isDark) {
isDark = value;
wireless.stopListening();
wireless.write(&isDark, sizeof(isDark));
delay(100);
wireless.startListening();
}
if (wireless.available()) {
wireless.read(&ledCount, sizeof(ledCount));
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], i < ledCount);
}
}
delay(10);
}