(Help) Im trying to transmit RF with nRF905 but my code isn't making it to the end

Hi all, i'm trying to transmit RF with a nRF905 Radio Transceiver and the nRF905 Transceiver library with Arduino (4.0) but i'm having trouble with the code, I have made the code print "Transmission Complete" when it has finished transmitting but it never seems to get there and "transmission complete" is never printed. Could someone have a look over my code and see whats wrong? P.S. Im not very good at coding so sorry if its messy.

#include <nRF905.h>
#include <SPI.h>

#define mess_Address 0xB54CAB34

nRF905 transceiver = nRF905();


static bool txDone; // NOTE: In polling mode this does not need to be volatile

// Event function for TX completion
void nRF905_onTxComplete(nRF905* device)
{
  txDone = true;
}

byte Message[] = {0b00000000, 0b10000001, 0b10000010, 0b10000011, 0b10000100, 0b10000101, 0b10000110, 0b10000111, 0b10001000, 0b10011000, 0b10101000, 0b10111000, 0b11001000, 0b11011000, 0b11101000, 0b11111001, 0b00101001, 0b00111001, 0b01011001, 0b01101001, 0b01111001, 0b10011010, 0b10011011, 0b10011101, 0b10011110, 0b10011111, 0b10101010, 0b11101011, 0b01101011, 0b11101101, 0b11101110, 0b11111111};


void setup()
{
  Serial.begin(115200);
  Serial.println(F("Transmission is beggining"));
  SPI.begin();

  transceiver.begin(
    SPI,
    10000000,
    6,
    NRF905_PIN_UNUSED, // CE (standby) pin must be connected to VCC (3.3V)
    NRF905_PIN_UNUSED, // TRX (RX/TX mode) pin must be connected to VCC (3.3V) (force TX mode)
    8, // PWR
    NRF905_PIN_UNUSED, // Without the CD pin collision avoidance will be disabled
    NRF905_PIN_UNUSED, // Without the DR pin the library will run in polling mode and poll the status register over SPI. This also means the nRF905 can not wake the MCU up from sleep mode
    NRF905_PIN_UNUSED, // Without the AM pin the library must poll the status register over SPI.
    NULL, // Running in polling mode so no interrupt function
    NULL // Running in polling mode so no interrupt function
  );

  transceiver.events(
    NULL,
    NULL,
    nRF905_onTxComplete,
    NULL
  );

  transceiver.setTransmitPower(NRF905_PWR_n2);



}

void loop() {
  // put your main code here, to run repeatedly:

  //Write data and address to Radio
  transceiver.write(mess_Address, Message, sizeof(Message));

  txDone = false;


  //Transmit the data
  transceiver.TX(NRF905_NEXTMODE_STANDBY, false);
  Serial.println(F("Transmission Started"));

  // Transmission will take approx 6-7ms to complete (smaller paylaod sizes will be faster)
  //Check to see if transmission is complete
  while (!txDone)
    transceiver.poll();



  //power down radio
  transceiver.powerDown();
  Serial.println(F("Transmission Completed"));

  delay(10000);



}

Looks like missing brackets is making your code do something unexpected.
Paul

Right of course, must of missed that, thanks

hi Paul
im having some more trouble with this, is this were im meant to put the brackets?

    while (!txDone) {
      transceiver.poll();
      Serial.println(F("waiting"));
    }

Because it seems the code still doesn’t make it past this point

do you see multiple occurrences of "waiting" on the serial console ?

Look in the example you based your code on and see if you have missed the following line from the loop() (and possibly others too) :

txDone = false;

Yeah waiting is all that comes up, In the example “txDone” gets changed to true when the transmission is finished, is it meant to be set to false or am I meant to remove the “!” From the loop? Unless the transmission is just not finishing for some reason

In this example https://github.com/zkemble/nRF905-arduino/blob/master/examples/lowpwr_node_sensor/lowpwr_node_sensor.ino the events are registered so:


	// Register event functions
	transceiver.events(
		NULL,
		NULL,
		nRF905_onTxComplete,
		NULL
	);

The order will be important and is different in your code.
State which example you have based your code on.

i based my code off the lowpwr node sensor, i also changed my code a bit from the original in my post

/*
   Project: nRF905 Radio Library for Arduino (Low power sensor node example)
   Author: Zak Kemble, contact@zakkemble.net
   Copyright: (C) 2020 by Zak Kemble
   License: GNU GPL v3 (see License.txt)
   Web: https://blog.zakkemble.net/transceiver-avrarduino-librarydriver/
*/

// This examples requires the low power library from https://github.com/rocketscream/Low-Power

// This examples configures the nRF905 library to only use 5 connections:
// MOSI
// MISO
// SCK
// SS -> 6
// PWR -> 8

// The following pins on the nRF905 must be connected to VCC (3.3V) or GND:
// CE (TRX_EN) -> VCC
// TXE (TX_EN) -> VCC

#include <nRF905.h>
#include <SPI.h>

byte mess_Address = {0xB54CAB34};
byte Message[] = {0b00000000, 0b10000001, 0b10000010, 0b10000011, 0b10000100, 0b10000101, 0b10000110, 0b10000111, 0b10001000, 0b10011000, 0b10101000, 0b10111000, 0b11001000, 0b11011000, 0b11101000, 0b11111001, 0b00101001, 0b00111001, 0b01011001, 0b01101001, 0b01111001, 0b10011010, 0b10011011, 0b10011101, 0b10011110, 0b10011111, 0b10101010, 0b11101011, 0b01101011, 0b11101101, 0b11101110, 0b11111111};
char Button = 0;


nRF905 transceiver = nRF905();

static bool txDone; // NOTE: In polling mode this does not need to be volatile

// Event function for TX completion
void nRF905_onTxComplete(nRF905* device)
{
  txDone = true;
}

void setup()
{
  Serial.begin(115200);
  Serial.println(F("Getting Transmission ready"));

  txDone = false;

  // standby off TODO
  //pinMode(7, OUTPUT);
  //digitalWrite(7, HIGH);
  // pwr
  //pinMode(8, OUTPUT);
  //digitalWrite(8, HIGH);
  // trx
  //pinMode(9, OUTPUT);
  //digitalWrite(9, HIGH);


  // This must be called first
  SPI.begin();

  // Minimal wires (polling mode)
  // Up to 5 wires can be disconnected, however this will reduce functionalliy and will put the library into polling mode instead of interrupt mode.
  // In polling mode the .poll() method must be called as often as possible. If .poll() is not called often enough then events may be missed.
  transceiver.begin(
    SPI,
    10000000,
    10,
    7, // CE (standby) pin must be connected to VCC (3.3V)
    9, // TRX (RX/TX mode) pin must be connected to VCC (3.3V) (force TX mode)
    8, // PWR
    4, // Without the CD pin collision avoidance will be disabled
    3, // Without the DR pin the library will run in polling mode and poll the status register over SPI. This also means the nRF905 can not wake the MCU up from sleep mode
    2, // Without the AM pin the library must poll the status register over SPI.
    NULL, // Running in polling mode so no interrupt function
    NULL // Running in polling mode so no interrupt function
  );

  transceiver.events(
    nRF905_onTxComplete,
    NULL,
    NULL,
    NULL
  );

  // Low-mid transmit level -2dBm (631uW)
  transceiver.setTransmitPower(NRF905_PWR_10);
  transceiver.setChannel(106);
  transceiver.setBand(NRF905_BAND_433);
  transceiver.setAutoRetransmit(false);
  transceiver.setCRC(NRF905_CRC_16);
  transceiver.setPayloadSize(32, 0); // Will transmit 5 byte payloads, receive 32 byte payloads
  transceiver.setAddressSize(4, 4);
  transceiver.write(mess_Address, Message, sizeof(Message));

  Serial.println(F("Transciever Started"));
}

void loop()
{

  // Write data to radio
  transceiver.write(mess_Address, Message, sizeof(Message));



  Serial.println(F("Waiting for input"));

  while (Serial.available() == 0) {

  }

  Button = Serial.parseInt();

  if ((Button >= 0) && (Button <= 9)) {

    // This will power-up the radio and send the data
    transceiver.TX(NRF905_NEXTMODE_TX, false);
    // Transmission will take approx 6-7ms to complete (smaller paylaod sizes will be faster)
    while (!txDone) {
      transceiver.poll();
      Serial.println(F("waiting"));
    }
    Serial.println(F("Transmission Completed"));
    transceiver.powerDown();

    // NOTE:
    // After the payload has been sent the radio will continue to transmit an empty carrier wave until .powerDown() is called. Since this is a sensor node that doesn't need to receive data, only transmit and go into low power mode, this example hard wires the radio into TX mode (TXE connected to VCC) to reduce number of connections to the Arduino.


    // TODO
    // Sleep for 64 seconds
    //uint8_t sleepCounter = 8;
    //while(sleepCounter--)
    // Sleep for 1 second
    delay(10000);
  }



  // Transmission done, power-down the radio

}

This appears still to be incorrect.

Am I meant have ontxcomplete on the third line?

Am I meant to have ontxcomplete on the third line?

Yes. In principle, if you are following an example and don’t fully understand it, it is unwise to make arbitrary changes to it.

isn't the while (!txDone) {} going to repeat forever because if txDone is not re-set inside the while loop it will keep repeating. Or can the arduino run multiple processes simultaneously?

I actually have no idea, I’ll try have a look

I just had a look online and it seems to suggest that it can only run 1 process at a time, but I think that the “transceiver.poll” is what checks for events like ontxcomplete, I’ll have a look though