Hello everyone.
I am making a project that uses 2 arduino to transfer data (one asks and one answers). Right now i can make them work as i aspected. However there is a problem that i need to fix.
When i use the UNO (ask) to receive answer from the MEGA (answer) and send it on Serial, there is a blank line then my answer i want. I haved tried some value for delay() but they don't work. I want to remove the blank line so that i can use LabVIEW to read and process data correctly.
Code for UNO (Ask)
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#include "nRF24L01.h"
RF24 radio(9, 10);
uint8_t diachi[][6] = {"Kenh0","Kenh1"};
//const byte diachi[6]="12345";
void setup() {
Serial.begin(9600); //
Serial.println("Serial port is ready");
while (!radio.begin())
{ Serial.println("Hardware is error");
}
radio.openWritingPipe(diachi[0]);
radio.openReadingPipe(1,diachi[1]);
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(120);
radio.setDataRate(RF24_250KBPS);
if (!radio.available())
{
Serial.println("Haven't connected yet");
Serial.println("CONNECTING...");
}
}
void loop() {
radio.stopListening();
if (Serial.available() > 0) {
int command = Serial.parseInt();
int abc1=1;int abc2=2;int abc3=3;
if (command == 1) {
Serial.println("1");
radio.write(&abc1,sizeof(abc1));
} else if (command == 2) {
Serial.println("2");
radio.write(&abc2,sizeof(abc2));
} else if (command == 3) {
Serial.println("3");
radio.write(&abc3,sizeof(abc3));
}
delay(50);
radio.startListening();
byte nhan[20];
radio.read(&nhan, sizeof(nhan));
for (int i=0;i<20;i++){
Serial.print(nhan[i]);}
Serial.println();
delay(100);
}
}
Code for MEGA (answer)
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#include "nRF24L01.h"
RF24 radio(9, 53);
uint8_t diachi[][6] = {"Kenh0","Kenh1"};
void setup() {
pinMode(A0, INPUT);
Serial.begin(9600);
while (!radio.begin())
{ Serial.println("Hardware is error");
}
radio.openWritingPipe(diachi[1]);
radio.openReadingPipe(1,diachi[0]);
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(120);
radio.setDataRate(RF24_250KBPS);
if (!radio.available())
{
Serial.println("Haven't connected yet");
Serial.println("CONNECTING...");
}
}
void loop() {
radio.startListening();
while (!radio.available()) {}
int nhan=0;
radio.read(&nhan, sizeof(nhan));
//nhan[0]=int(nhan[0]);
Serial.println(nhan);
delay(100);
radio.stopListening();
byte abc1[20]={1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};
byte abc2[12]={1,2,3,4,5,6,7,8,9,0,1,2};
byte abc3[2]={1,1};
if (nhan==1)
{radio.write(&abc1, sizeof(abc1));
for (int i=0;i<20;i++){
Serial.print(abc1[i]);
}
Serial.println();}
else if (nhan==2)
{
radio.write(&abc2, sizeof(abc2));
for (int i=0;i<12;i++){
Serial.print(abc2[i]);}
Serial.println();}
else if (nhan==3)
{
radio.write(&abc3, sizeof(abc3));
for (int i=0;i<2;i++){
Serial.print(abc3[i]);}
Serial.println();}
delay(100);
}
This is what i received on Serial mornitor
Thank you very much!