0

My whole code for master. Please head to void read() where I've used parseInt()

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

RF24 radio(7, 8); //Set up nRF24L01 on CE pin and CSN pin respectively

const uint64_t pipe = 0x0ABCDEF; //Address for communication between master and slave
uint16_t data[7];
const uint16_t buffer_size = sizeof(data); //Size of the payload for the RF
int i, flag;
int tx_counter = 0;


void read();
void setup() {

  Serial.begin(115200);

  radio.begin();
  radio.setChannel(2);
  radio.setDataRate(RF24_1MBPS);
  radio.setPALevel(RF24_PA_MIN);
  //radio.powerUp();
  radio.openWritingPipe(pipe);
  //radio.setPayloadSize(7);
  radio.stopListening();


  //Default values range from 100 to 700
  for (i = 0; i < 7; i++) {
    data[i] = 100 + (100 * i);
  }
  flag = 0;
  Serial.println("Enter 7 values");
}

void loop() {

  read();

  if (flag) {
    bool tx_ok = radio.write(data, buffer_size);
    tx_counter++;
    Serial.print("Transmission count:\t");
    Serial.println(tx_counter);
    print();
    if (tx_ok) {
      Serial.println("Transmitted Successfully");
      Serial.println("\nEnter Next set of 7 numbers");
    }
    else Serial.println("Transmission failed");

  }
}

void print() {
  for (i = 0; i < 7; i++) {
    Serial.println(data[i]);
  }
  flag = 0;
}

void read() {
  while (Serial.available()) {
    for (i = 0; i < 7; i++) {
      data[i] = Serial.parseInt();      
    }
    flag = 1;
  }
}

My problem is that Serial.parseInt() takes some time. The code prints out the values to the serial monitor after sometime. I have timed it and it appears to be around 7 seconds. The arduino page on the function says default timeout is 1 sec. So I think 7 elements in the array takes 1 sec each. If I use Serial.setTimeout(2000), the same thing takes 14 seconds.

This has got to have a simpler way. I want it to behave like when we use Serial.read. Please help me on this.

  • i think that Serial.available() has a timeout of several seconds before it decides that there is no more data – jsotola Mar 28 at 16:26
  • 1
    I am confused. you send a 7 bytes long binary buffer over radio. and then you print the content of the buffer as numbers to local Serial Monitor and you parse data from Serial Monitor. do you enter numbers in Serial Monitor? every Serial.parseInt waits a second for a number sent from Serial Monitor – Juraj Mar 28 at 17:55
  • @jsotola the Serial.available returns how many bytes have been received (and are in the Serial receive buffer, waiting to be read). That's all, nothing else. – Jot Mar 28 at 18:13
  • @Juraj Yes I enter numbers in the Serial monitor. I am printing it to just keep a track of the values in the array so that it should match at the receiver's end. If I enter the number '2' and wait for some seconds, it automatically timeouts and the rest all becomes zeroes. I want it to wait for the user's input infinitely. – Kishore Mar 28 at 18:51
0

this is what you want:

void read() {
  for (i = 0; i < 7; i++) {
    while (!Serial.available()); // wait forever for some input
    data[i] = Serial.parseInt();      
  }
  flag = 1;
}
  • Thanks for this. Works except with a small issue. The first value transmitted alone is 0. And after each transmission, if I enter next set of integers quick enough this doesn't occur. A sample received value showing this 0 8 84 51 162 21 651 – Kishore Apr 5 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.