How do I change digitalRead value into format for serial transmission

Hi,

I am using the attached script to transmit data from one Arduino Uno to another using 433 MHz transmit and receive modules, using radiohead library.

It is working to send a 'canned message' "Hello, my name is Scott!" just fine however, I want to take the value of a digitalRead (1 or 0) and send that to the receiver to be read and used in it's script. I would have thought this much easier than I've found it to be.

I'm new to serial communication and am not sure what's being sent; I know, highs and lows but is it ASCII or what?

// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module

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


int id_1 = 2;
int id_2 = 3;
int id_3 = 4;
int id_0 = 5;

int buzzer_id_0 = 0;
int buzzer_id_1 = 0;
int buzzer_id_2 = 0;
int buzzer_id_3 = 0;


int pin6 = 6;

RH_ASK driver(2000, 11, 12, 5); // ESP8266 or ESP32: do not use pin 11

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

pinMode(pin6,INPUT);
pinMode(id_1, INPUT);
pinMode(id_2, INPUT);
pinMode(id_3, INPUT);
pinMode(id_0, INPUT);

}

void loop()
{
 

 buzzer_id_0 = digitalRead(id_0);
 buzzer_id_1 = digitalRead(id_1);
 buzzer_id_2 = digitalRead(id_2);
 buzzer_id_3 = digitalRead(id_3);
 
 if(digitalRead(pin6) == LOW){
     

    
    
    const char *msg = "Hello, my name is Scott!";

    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(200);
 }


    
}

Post your own efforts, and explain what went wrong.

Hint: "msg" is just 1 or more bytes, with any value(s) you choose, between 0 and 255.

    driver.send((uint8_t *)msg, strlen(msg));

There are many ways.

You can put the four values in an array:


uint8_t values[4];  // just a global variable here

// later

// in the loop fill the array

    values[0] = buzzer_id_0;  // and do the same for 1, 2 and 3

// then send the whole thing

    driver.send(values, 4);

On the other side, catch those in an array, use them however.

You could use an array directly instead of the four variables whose name differ only by an appended digit - a sure sign that array variables might be useful.

a7

Hello meltapudly

Either send an 'A' for digitalRead()==HIGH or an 'a' for digitalRead()==LOW.
Up to you.
You can design and programme your own character-driven software protocol.

1 Like

Thanks. I need to send the 4 buzzer_id values.

I tried putting the 4 digital read values in a char array, get weird box like chars on receiving end. So, in a nutshell, how can I get the 4 digital read values into the msg variable as text (I assume the "Hello, my name is Scott!" is simply text and whatever I change that text to sends and receives perfectly).

Thanks. I did try that and got box-like chars on receiving end.

That is because you are reading and printing the data as if it were text. A slightly different approach is required for reception of binary data.

Another hint: the printable ASCII text character '1' can be used to represent a digital value of 1.

const char *msg = "1";
driver.send((uint8_t *)msg, 2);  //including the terminating zero of the C-string

Let us not that int y = digitalRead(DPin); returns HIGH or LOW of type int; where, HIGH has been defined as 1 (0x01) or 0 (0x00). You can transmit this value to the receiver as binary as it is or in ASCII format like 0x31 (0x30 + HIGH = 0x30 + 0x01 = 0x31) for HIGH and 0x30 (0x30 + LOW = 0x30 + 0x00) for LOW. For example:

int y = digitalRead(4);
if(y == HIGH)
{ 
    SUART.print(y + 0x30);  // or Serial.print('1'); sending ASCII for 1 = HIGH
}
else
{
    SUART.print(y + 0x30);  //or Serial.print('0'); sending ASCII for 0 = LOW
}
1 Like

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