how to get string value after while looping

hi, before im asking, im sorry for my bad english, i've searching for my solution and still don't get solution.

this is my example code for my training

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 10); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  while (!Serial) 
    ; // wait for serial port to connect. Needed for native USB port only

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);


void loop() 
  int x = 0;
  String var_baca;
  String var_ahir;

    var_baca = mySerial.readStringUntil('\r');

  while(digitalRead(2) == LOW)
    Serial.print("Mengirim : ");

  if(digitalRead(3) == LOW)
  Serial.print("Hasil Akhir : ");

this is my description :

  1. i'm using 2 arduino for simulation 2 device serial communication,and using push button for every command.
  2. if digitalread 2 = low, my arduino send a value (x) to other arduino. On the other hand, if my arduino get a value from serial pin, my arduino will print it.

My problem is, how to get string value "var_baca" after while looping complete. because, if i'm running that code and digitalread 3 = low, "var_baca" not showing any value.

thank you for your tim and helping :smiley:

Do you see any problem with putting a separate copy of the if(digitalRead(3) == LOW) loop inside the while loop to handle that case?

Change this

while(digitalRead(2) == LOW)


if (digitalRead(2) == LOW)

And have a look at the examples in Serial Input Basics - simple reliable ways to receive data.


thank you, i'll try to learn from that tutorial :smiley: