How to send multiple bits of data

I'm trying to send multiple bits of data from the inputs from a joystick through
Radio Head 433Mhz Tx Rx. I still don't really understand the code and need help.

Transmitter

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

RH_ASK driver;

const int analogYpin = A7;
const int analogXpin = A6;
const int buttonPin = A5;

void setup()
{
    Serial.begin(9600);   // Debugging only
    if (!driver.init())
    Serial.println("init failed");
    
    pinMode(buttonPin, INPUT);
    pinMode(analogYpin, INPUT);
    pinMode(analogXpin, INPUT);
}

uint8_t data = 0;

void loop()
{
    data = (uint8_t) (analogRead(buttonPin)/4); //Map from max of 1023 to 255
    driver.send(&data, 1);
    driver.waitPacketSent();
}
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

RH_ASK driver;

const int analogYpin = A7;
const int analogXpin = A6;
const int buttonPin = A5;

void setup()
{
    Serial.begin(9600);   // Debugging only
    if (!driver.init())
    Serial.println("init failed");
    
    pinMode(buttonPin, INPUT);
    pinMode(analogYpin, INPUT);
    pinMode(analogXpin, INPUT);
}

uint8_t data = 0;

void loop()
{
    data = (uint8_t) (analogRead(buttonPin)/4); //Map from max of 1023 to 255
    driver.send(&data, 1);
    driver.waitPacketSent();
}

Receiver

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

RH_ASK driver;

void setup()
{
    Serial.begin(9600); // Debugging only
    if (!driver.init())
    Serial.println("init failed");
}

void loop()
{
    uint8_t buflen = 3;
    uint8_t buf[buflen];
    if (driver.recv(buf, &buflen)) // Non-blocking
    {
      int i;
      // Message with a good checksum received, dump it.
      Serial.print("Button Value: ");
      Serial.println(*buf);         
    }
}

Thanks!

See the examples for that library.

As @DrDiettrich recommends, get the unmodified examples in the library to function as a simple transmit/receive pair.

You can eliminate an ASK radio set for testing - just wire the transmitter output of the one Arduino board to the receiver input on the other. Hook up a common ground.

Wired wireless.

When you have some confidence with that, replace the wire with your TX and RX modules, and at this time separate the two Arduino boards by a few meters - too close can be, um, too close for the radio set to work well. In thjs part, there are no direct electrical connections between the transmitter and the receiver.

Study the examlples you got working. Ask questions about any code lines you do not understand.

In the transmitter you posted, this

    data = (uint8_t) (analogRead(buttonPin)/4); //Map from max of 1023 to 255
    driver.send(&data, 1);

is you calculating one byte of data (data) and sending it out by address (pointer). The address-of operator & gets the address of the byte, and the length is set to 1.

You could use an array here, and send many bytes at once. Think of it like a little train you assemble on the transmitting side:

    uint8_t lotsaData[16];

// here put anything you want in the 16 elements of the array
// 

// and then send it out:
    driver.send(lotsaData, 16);

An array is already an address that can be used by name without the address-of operator.

The receiver now has this array for handling the received data

    uint8_t buflen = 3;
    uint8_t buf[buflen];

Obvsly, I hope, the idea on the receiver side woukd be to accommodate whatever you expect to send

    uint8_t buflen = 16;
    uint8_t buf[buflen];

so

   if (driver.recv(buf, &buflen)) // Non-blocking

gets the entire train and returns true so your if statement handles new data arrival.

Besides an array, a popular way to send many unrelated pieces of information is by using a struct structured variable. These are sent and received by address and length in the same manner.

In either case assembling the packet (data train) takes some thinking and some minimal coding skills. Try first just sending some fake data.

Use the serial monitor and lots more serial printing to keep track of the flow in your code and the values of key variables.

You will know more C/C++ than when you started this. :expressionless:

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.