Hi guys. Before the explaining problem I want to explain my project :
There is a plane with arduino uno and Nrf24L01, and a remote control with uno and nrf24l01.I send the pot values to plane from remote. The plane get the values but I want a feedback like " I recieve your message". How can I do that? Here is my codes :
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2);
// Verici Servoları
int Gaz ;
int SagSol;
int Yukariasagi;
int Gazs;
int SagSols;
int Yukariasagis;
int Connected = 0;
int msg[3];
RF24 radio(9,10);
const uint64_t pipe = (0xF0F0F0F0E1LL);
const uint64_t pipe2 = (0xF0F0F0F0E2LL);
void setup() {
radio.begin();
radio.enableAckPayload();
Serial.begin(57600);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.write("asdf");
radio.openWritingPipe(pipe);
radio.openReadingPipe(1,pipe2);
}
void loop() {
Gaz = analogRead(0);
SagSol = analogRead(1);
Yukariasagi = analogRead(2);
Gazs = map(Gaz,23,175,0,255);
SagSols = map(SagSol,39,228,60,120);
Yukariasagis = map(Yukariasagi,40,226,55,125);
msg[0] = Gazs;
msg[1] = SagSols;
msg[2] = Yukariasagis;
radio.write(msg,sizeof(msg));
radio.startListening();
if(radio.available()){
radio.stopListening();
lcd.print("ok");
}
radio.stopListening();
}
** edit **
And can we send message while we are recieving a message? For example I want to add other sensors into plane and can we send values while we getting other values?``
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
I think I solve it. Everything works well. Thank you for reply. But I have a question. I set the "data send per second" to 1 milisecond. Can it damage/crash arduino or anything else?
LuckeRu:
I set the "data send per second" to 1 milisecond. Can it damage/crash arduino or anything else?
I don't think an nRF24 can send a message every millisec. I think a round-trip message and acknowledgement takes 3 to 5 millisecs. Trying to send too often might mean that messages get mixed up.
You also need to consider whether the receiving Arduino has time to do anything useful with the data it receives.
I find it hard to believe that a message rate of more than 10 per second is actually needed, and maybe 5 per second would be sufficient.
LuckeRu:
I think I solve it. Everything works well. Thank you for reply. But I have a question. I set the "data send per second" to 1 milisecond. Can it damage/crash arduino or anything else?
Can you post your updated code please?
I will help others who use this thread.
Good to hear you got things working... Tom...
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LiquidCrystal_I2C.h>
#define CE_PIN 9
#define CSN_PIN 10
LiquidCrystal_I2C lcd(0x3f, 16, 2);
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int dataToSend[3];
char txNum = '0';
int GazE;
int SagSolE;
int YukariAsagiE;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1; // send once per second
void setup() {
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
GazE = map(analogRead(0),23,200,0,255);
SagSolE = map(analogRead(2),37,260,70,120);
YukariAsagiE = map(analogRead(1),35,230,65,125);
if (GazE > 255) // My pot is too unbalanced. ( Sometimes it can show up to "200" value)
GazE = 255;
if (GazE < 0)
GazE = 0; //( "Gaz" Means Throttle. "GazE" Means Throttle Edited)
if (SagSolE > 120) // "Sag Sol" Means Left Right. "SagSolE" Means Left Right Edited)
SagSolE = 120;
if (SagSolE < 70)
SagSolE = 70;
if (YukariAsagiE > 125) // "Yukari Asagi" Means Up Down. "YukariAsagiE" Means Up Down Edited)
YukariAsagiE = 125;
if (YukariAsagiE < 65)
YukariAsagiE = 65;
dataToSend[0] = GazE;
dataToSend[1] = YukariAsagiE;
dataToSend[2] = SagSolE;
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) { // I dont understand anything here
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
if (rslt) {
lcd.print("Connected"); // If its connected , it prints to lcd connected :p
lcd.setCursor(0,0);
}
else {
lcd.print("Connection Lost"); // If connection lost , it prints connection lost and clear it because if it connect again it writes "Connectedn Lost"
delay(1000);
lcd.clear();
}
}
//================
And the Reciever
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
int dataReceived[3]; // this must match dataToSend in the TX
bool newData = false;
int int1;
int int2;
int int3;
Servo UpDown;
Servo LeftRight;
int Throttle;
void setup() {
UpDown.attach(6);
LeftRight.attach(5);
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop() {
getData();
showData();
UpDown.write(int2);
LeftRight.write(int3);
digitalWrite(3,int1);
}
//==============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
int1 = dataReceived[0];
int2 = dataReceived[1];
int3 = dataReceived[2];
}
}
void showData() {
if (newData == true) {
Serial.println(int1);
Serial.println(int2);
Serial.println(int3);
newData = false;
}
}
You say you want to send a message every millisecond (which, as i said earlier, is much too fast) but then you have delay(1000) in your code which means the maximum rate of sending cannot be more than one per second.