Parallel to serial conversion and vice versa

Hi,
I am very new to Arduino and want some help for conversion of 10 Parallel Digital/Push Buttons Inputs (at 10 DI pins) into one serial output (at one Output Pin) of an Arduino UNO at Trans end.

And at Receiving end, reverse of the same i.e. conversion of that serial data (bytes) input into corresponding 10 parallel Digital outputs at another Arduino UNO.Basic Circuit Idea attached herewith.

How to do this, can anybody help & provide code to me? Thanks in Advance.
With Warm Regards.

mindmate11@gmail.com

Image from Original Post so we don't have to download it. See this Simple Image Guide.

...R

The simplest thing IMHO is to create a cstring to hold the settings for the switches and populate it like this

char swSettings[11] = "0000000000"; // need 11 to leave room for terminating NULL
byte swPins[10] = {2,3,4,5,6,7,8,9,10}; // replace with the actual pins your switches are connected to.

for (byte n = 0; n < 10; n++) {
   swSettings[n] = digitalRead(swPins[n]) + 48; // the 48 converts a 0 or 1 to the characters '0' or '1'
}

Then you can send that cstring with

Serial.println(swSettings);

Then just reverse the process on the receiving end. Have a look at the second example in Serial Input Basics

...R

SPI or I2C allow to transmit any number of bits in a serial stream. The transmitted bytes can be read from or written into the controller ports which connect each bit to a digital I/O pin.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The second example will be suitable for your data.

Assuming the data is in the array recvdChars[] then you can switch all the individual LEDs like this (also assuming you have a byte array called ledPins[] that holds the pin numbers for the different LEDs

for (byte n = 0; n < numLeds; n++;) {
  byte ledState = recvdChars[n] - '0';
  digitalWrite(ledPin[n], ledState);
}

...R

Mindmate:
as these were not used in the sketch at Transmitting/sender End.

In the code in your Reply #5 you have

 val = Serial.readStringUntil('\n');

which is relying on a linefeed character ('\n') as the end-marker. My second example does the same.

...R

Mindmate:
{D1,D2,D3,A0,A1}. It is understood that value of Digital Inputs will remain either 0 or 1, but the value of Analog inputs will be vary from Decimal 0 to 9 depending upon instantaneous value of voltage/frequency Or A/D converter.

I presume you realize that you will need to make the array sendData[] bigger.

analogRead() returns an int value between 0 and 1023. An int comprises 2 bytes.

How are you planning to send the array sendData[] using serial - you have not included that code? And what is receiving the data - the receiving code is just as important.

The simplest thing may be to send the analog value separately with a Serial.print()

...R

Mindmate:
I am simply planning to dividing the Analog Pin (A0,A1) analogRead value by a divider (SAY by 100) before adding it in to serial byte/string. In this way I will get only number/levels from 0 to 9 only.

If that is sufficient, it will work. Dividing by 128 (right shift 7 times) is much faster than dividing by 100. For example

int myAdcVal = analogRead(analogPin);
int scaledVal = myAdcVal >> 7;
sendData[3] = ((byte) scaledVal) + 48;

...R

Mindmate:
Please guide, How to add second analog Pin A1 to form the serial data like as mentioned above:
{D1,D2,D3,A0,A1} ?

I think it is now time for you to do a little thinking.

What do you think might be necessary to extend the programs for this?

Please post the programs (sender and receiver) that represent your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

...R