Hi!
It's working!!
Now I have 3 Arduino. 1 master and 2 slaves and all is working find but I generate a random number and I want to stop the generation of numbers. I try with "while" ut I don't know why doesn't stop.
Master:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte slaveAddressVerde[5] = {'R','x','A','A','A'};
const byte slaveAddressRojo[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
char txNum = '0';
int ackData = 0;
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 2000; // send once per second
int randNumber = 0;
//===============
void setup() {
Serial.begin(115200);
Serial.println(F("Source File /mnt/sdb1/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/SimpleTxAckPayload.ino"));
Serial.println("SimpleTxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(5,5); // delay, count
// 5 gives a 1500 µsec delay which is needed for a 32 byte ackPayload
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
}
showData();
}
//================
void send() {
randNumber = random(2,4);
Serial.print("RANDOM: ");
Serial.println(randNumber);
while (randNumber == 2){
radio.openWritingPipe(slaveAddressVerde);
int luz = 2;
bool rslt;
rslt = radio.write( &luz, sizeof(luz) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(luz);
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&ackData, sizeof(ackData));
newData = true;
}
else {
Serial.println(" Acknowledge but no data ");
}
if (ackData = 1){
randNumber = 0;
}
}
else {
Serial.println(" Tx failed");
}
}
while (randNumber == 3){
radio.openWritingPipe(slaveAddressRojo);
int luz = 3;
bool rslt;
rslt = radio.write( &luz, sizeof(luz) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(luz);
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&ackData, sizeof(ackData));
newData = true;
//randNumber = 0;
}
else {
Serial.println(" Acknowledge but no data ");
}
if (ackData = 1){
randNumber = 0;
}
}
else {
Serial.println(" Tx failed");
}
}
prevMillis = millis();
}
//=================
void showData() {
if (newData == true) {
Serial.print(" Acknowledge data ");
Serial.print(ackData);
Serial.print(", ");
// Serial.println(ackData[1]);
Serial.println();
newData = false;
}
}
Slave "Verde"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddressVerde[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
int ackData = 0;
bool newData = false;
int luz = 0;
int LED = 2;
#define echoPin 6
#define trigPin 7
long duracion, distancia;
//==============
void setup() {
Serial.begin(115200);
Serial.println("SimpleRxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddressVerde);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
pinMode(LED, OUTPUT);
//Sensor de proximidad
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
//==========
void loop() {
getData();
showData();
}
//============
void getData() {
if ( radio.available() ) {
radio.read( &luz, sizeof(luz) );
updateReplyData();
newData = true;
digitalWrite(luz, HIGH);
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(luz);
Serial.print(" ackPayload sent ");
Serial.print(ackData);
Serial.print(", ");
newData = false;
}
}
//================
void updateReplyData() {
while (luz == 2) {
digitalWrite(luz, HIGH);
//Serial.println(luz);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duracion = pulseIn(echoPin, HIGH);
distancia = (duracion/2)/29;
Serial.print(double(distancia));
Serial.println(" cm.");
if (distancia <=15) {
digitalWrite(luz, LOW);
luz = 0;
Serial.println(luz);
ackData = 1;
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}
}
}
Slave "Rojo"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddressRojo[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
int ackData = 0;
bool newData = false;
int luz = 0;
int LED = 2;
#define echoPin 6
#define trigPin 7
long duracion, distancia;
//==============
void setup() {
Serial.begin(115200);
Serial.println("SimpleRxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddressRojo);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
pinMode(LED, OUTPUT);
//Sensor de proximidad
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
//==========
void loop() {
getData();
showData();
}
//============
void getData() {
if ( radio.available() ) {
radio.read( &luz, sizeof(luz) );
updateReplyData();
newData = true;
digitalWrite(luz, HIGH);
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(luz);
Serial.print(" ackPayload sent ");
Serial.print(ackData);
Serial.print(", ");
newData = false;
}
}
//================
void updateReplyData() {
while (luz == 3) {
digitalWrite(luz, HIGH);
//Serial.println(luz);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duracion = pulseIn(echoPin, HIGH);
distancia = (duracion/2)/29;
Serial.print(double(distancia));
Serial.println(" cm.");
if (distancia <=15) {
digitalWrite(luz, LOW);
luz = 0;
Serial.println(luz);
ackData = 1;
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}
}
}
In other sketch without nRF24L01 when I use random number and "while" the program generate only one number till while end