How do I send multiple inputs through radiohead library

Hello, I am currently taking a meccano car and using an arduino uno R3 to turn it into an RC car. I am using an H-Bridge circuit to switch the car forwards, backwards and to get it to stop. you can see the code here

/*------------------------------------------------------------
   L9110S-Stepper-DC-motor-Driver-Module
  made on 28 oct 2020
  by Amir Mohammad Shojaee @ Electropeak

  edited by Evan Moore

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

#define A1 5  // Motor A pins
#define A2 6
byte Speed = 255; // Speed of motor 255 is 100% used to vary speed of PWM

typedef struct
{
    uint16_t   dataitem1;
    uint16_t   dataitem2;
} MyDataStruct;

RH_ASK driver;

int incomingByte = 0; // for incoming serial data

void setup() {

  pinMode(A1, OUTPUT);  //defines pin 5 to go to H-Bridge pin A1
  pinMode(A2, OUTPUT);  //defines pin 6 to go to H-Bridge pin A2
  
//Sets pins to low to override past variables so the transistors don't fry themselves

  digitalWrite(A1, LOW);
  digitalWrite(A2, LOW);
  
 Serial.begin(9600);  // Debugging only
    if (!driver.init())
         Serial.println("init failed");

}

void forward() {          //function of forward 
  analogWrite(A1, Speed); //sets Pin A1 to use PWM with a duty cycle of Speed*100/255% in order to modulate the current through the motor
  analogWrite(A2, 0);     //sets Pin A2 to 0 so current does not pass through the transistors
}

void backward() {         //function of backward. the same as forward but switch which is activated and zero in order to flip direction as current
  analogWrite(A1, 0);
  analogWrite(A2, Speed);
}

void Stop() {              //function of stop. no current passes to the motor as all transistors are off
  digitalWrite(A1, LOW);
  digitalWrite(A2, LOW);
}

int  input = 0;
void loop() {

// send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    input = incomingByte - 48; //convert ASCII code of numbers to 1,2,3
    
  switch (input) { 
    case 1:         // if input=1 ....... motors turn forward
      forward();
      break;
    case 2:         // if input=2 ....... motors turn backward
      backward();
      break;
    case 3:         // if input=1 ....... motors turn stop
      Stop();
      break;
  }
  //wait a bit before resetting the input I don't know exactly why this is here but someone smarter than me made it so I am not touching it.
  delay(200);
  input=0;
} 
}

and I have gotten a cheap 433MHz. transmitter receiver to (hopefully) send what is currently being inputted into the serial monitor, and the speed of the car. however I cannot seem to get radiohead to work with me for the life of me.
here is the code for the receiver, I got it online, and tried to modify it for my purposes

// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h>
 
#define LED 13

// Create Amplitude Shift Keying Object
RH_ASK rf_driver;

//Define packet for the direction (X axis and Y axis)
int data[2];

void setup() {
   if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    
  rf_driver.init();
  pinMode(LED, OUTPUT);

}

void loop() {
// Set buffer to size of expected message
    int receivedData[2] = {0};
    uint8_t buflen = sizeof(receivedData);
    if (rf_driver.recv((uint8_t*)receivedData, &buflen)) {
      //if data is not an array, use &receivedData
    for (byte i = 0; i < 3; i++) {
      Serial.print(receivedData[i]);
      Serial.print('\t');
      Serial.println(receivedData[i],HEX);
    }
  }               
}

currently I want to take one part of the data array and let that be either 1, 2 or 3. as that is what tells the code I made for the motor to go forwards, reverse or to stop. I want to be able to also use PWM to be able to modify the speed of the car, so the other part of the array will have a number from zero to 255.
right now I just don't know what I should replace the for loop in the receiver code to output those numbers.
finally I have my transmitter code which I also copied somewhere on the internet.

// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 


//Define packet for the 
int data[2];

// Create Amplitude Shift Keying Object
RH_ASK rf_driver;

int incomingByte = 0; // for incoming serial data

void setup() {
  Serial.begin(9600);

  // Initialize ASK Object
  rf_driver.init();

}

int input = 0;
void loop() {
    incomingByte = Serial.read();
    input = incomingByte - 48;

  

  data[0] = input;
  data[1] = 255;

  
  rf_driver.send((const uint8_t*)data, sizeof(data));
  rf_driver.waitPacketSent();
  delay(200);  
}

the code looks like it should work. I don't have much to say about it because I copied it from the same place as the receiver code

Update 1:03 PM 12/2/21 added the arduino I am using and the radio parts. also changed the amount of bytes the receiver asks for.

Welcome to the forum!

You forgot to mention which Arduino you are using, what you have in the way of radios, and how the setup is wired.

The transmitter code is sending two integers, and the receive code expects three integers. That won't work. Neither will this, because you don't check whether characters are actually available to read.

incomingByte = Serial.read();

The first thing to do with any new library and hardware is to get the simplest library example working, before making any modifications (except possibly pin number changes, appropriate to the wiring). The radiohead library provides simple receive and transmit examples.

sorry I am using an arduino uno, with a 433Mhz transmitter and receiver that I got on amazon. but the radiohead library examples send text, and also only one message. I want to send two integers. isn't that different?

The point is to get the unmodified library example working, to test your wiring and components. Also, your coding skill will improve by looking at other people's work. I have already pointed out fatal errors in your first attempt.

Until the library example works, there is no point in moving on to a more complicated application.

ok, I will try that then. thank you.

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