Analog Read & Write between two Arduino's

Hi all,

Ok, so I have 2 Arduino Nano's. Both with HC-12 RF 433mhz serial wireless boards. I have a basic sketch on both using software serial in which I can transmit and recieve string messages between the two.

My goal is:

Arduino 1:
Read 2 x potentiometer readings using AnalogRead (One pot on each analog pin).
En-code the corrosponding values and send via wifi.

Arduino 2:
Decode and read analog values sent by Arduino 1.
Control 2 seperate FET gates via PWM (AnalogWrite) using the values sent from Arduino 1.

I'm fairly new to the RF HC-12 side of things, but I have this circuit working using just one Arduino without any issues. I just need help with coding to intergrate the transmission of the values from one Arduino to the other.

Ideally, latency rates need to be minimul as to provide rapid responce of the FET's.

Any help grately appreciated.

Many thanks,
Jim

Ok, I've managed to get Arduino 1 transmitting integer values read from a POT to the PC serial port and the software serial which then feeds to the HC-12. Code below:

//Transmit
 
#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(2, 3); //RX, TX
const int analogInPin = A0;
 
void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}
 
void loop() {
 
    mySerial.println(analogRead(analogInPin));
    Serial.println(analogRead(analogInPin));
 
}

I'm using the following code on Arduino 2:

Code (Text):
//Recieve
 
#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(2, 3); //RX, TX
 
void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}
 
void loop() {
 
    int input = mySerial.read();
    Serial.println(input);  
 
}

When Arduino 1 is not transmitting Arduino 2 displays -1 per line in the serial window. When Arduino 1 is transmitting the serial monitor on Arduino 2 throws out random numbers between 10 and 60 on each line.

Can anyone help?
Thanks

You're sending multiple ASCII characters with values in the range 48 to 57, plus newline, but you're receiving individual characters, and printing them as integers.

hmmmmm ok.
Whats the best solution to get around this issue?
I'm unsure as to how to format the data, transmit and then receive it as valid values I can use to then output to a PWM pin.

Read Serial Input Basics - updated to get ideas how to receive. Next write the sender's code to send what the receiver expects.

Well, you can continue sending ASCII characters, but you'll have to change the receiver code to parse the numbers.
Robin2 has a serial handling tutorial thread.

Ok thanks for the help guys.

I've amended my Arduino 1 transmit code to:

//Transmit

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX
const int analogInPin = A0;

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

void loop() {

    int input(analogRead(analogInPin));
    mySerial.write(input);
    Serial.println(input);

}

Thus sending the POT value as an integer.

Arduino 2 (receive) code is:

//Recieve

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

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

void loop() {
  
    int input = mySerial.read();
    Serial.println(input);    

}

The receiver (Arduino 2) serial window now shows the corresponding values of the POT on Arduino 1 (transmitter) up to 255 bits. But then as the POT value increases beyond 255 on the transmitter, the receiver reaches 255 and then starts counting from 0 again up to 255, then 0 again.

I'm sure I'm missing a little something here.

I'l have a read.

Thanks

You're missing the fact that you're writing a single byte on the transmitter.
A single byte can only represent the values 0 to 255.

You'd be better off printing the value, not writing it.

@Groove I have a question with regards to the single byte you mentioned.
Isn't an integer value between -32,xxx and 32,xxx?
So the int input value should allow for values 0 to 1023 to be read no?
Where are you getting the single byte on transmitter concept from? I'm unclear. Thanks.

Isn't an integer value between -32,xxx and 32,xxx?

Don't confuse "int" with "integer".
A "char" (8 bits) is an integer.
A "long long" (64 bits) is an integer

Groove:
You're missing the fact that you're writing a single byte on the transmitter.
A single byte can only represent the values 0 to 255.

You'd be better off printing the value, not writing it.

@AWOL do you know what he means by printing the value?
should we be using the char command?

what I don't get is how is a single byte being written on the transmitter?

I imagine s/he means printing the value, as an ASCII string, with some simple delimiter, like a carriage return.
That's what I'd do, then I could very simply verify the result.

char is a datatype, not a command.

knightridar:
what I don't get is how is a single byte being written on the transmitter?

The answer to that question is in the documentation of the write() function.

    int input(analogRead(analogInPin));
    mySerial.write(input);

write used that way will only send 1 byte of the memory pointed by input (which is likely 2 bytes if you are on a standard arduino UNO for example)

if you were to do

    mySerial.write(&input,sizeof(input));

then you would actually ask to send 2 bytes (or whatever is an int) to the receiver

if you just want to send an ascii text and parse it on the other side, then a

    mySerial.println(input);

would do and use the '\r\n' end of line sequence to help you find where the number terminates