Assigning 2 variables to a single operation

Hello, I'm trying to combine two variables to be assigned to a single operation. Below are the two variables that I want to combine.

Incoming_value = Serial.read();
command = Serial.read();

Basically what I want to happen is something like this:

Incoming_value and command = Serial.read();

I hope I can get an answer soon.

What kind of data you want to combine String,numbers what is it?
could you share your desired out put like you want 2+2=4 or "2"+"2"="22"

1 Like
Incoming_value = command = Serial.read();

I don't know what you exactly mean (and why you're asking that), you better explain your target or requirement, possibly with a sample code to let us better understand, because the question seems to be quite trivial (or wrong).

Anyway, I'll give it a try.
If you just need to assign the same value to both variables there are (at least) two ways. One is copying the value from one to the other:

Incoming_value = Serial.read();
command = Incoming_value;

But another approach is chaining assignments, like:

x = y = 10;

This because in C any assignment "returns" the value, so the "y = 10" will evaluate the rightmost expression (here just the constant value 10) and assigns it to the variable "y", so "y = 10" evaluates to 10: it lets the second assignment to get the same value and at the end both "x" and "y" variables have the same value.
This means, in your case:

command = Incoming_value = Serial.read();
2 Likes

Doesn't really matter if you use a single statement, or two statements, the actual code produced by the compiler will move the data into the variables as two operations. The main thing is that you do not want to call Serial.read() twice.

1 Like

some possibilities

  • the input could be either an ascii char command or a value
char cmd = Serial.read ();
int  val = cmd - '0';
  • the entire multi-char command could be a single char cmd followed by a # or # followed by a char cmd
  • could be above with a space in between
  • sscanf() could be used handle the cmd/val approach
void loop ()
{
    char cmd;
    int  val;
    if (Serial.available ())  {
        char buf [90];
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';     // terminate c-string

        sscanf (buf, "%c%d", &cmd, &val);

        Serial.print   (" cmd ");
        Serial.print   (cmd);
        Serial.print   (", val ");
        Serial.println (val);
        
    }
}

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

Hi @lorenz029,

Incoming_value = command = Serial.read(); //The expression is evaluated from right to left

or

command = Serial.read();
Incoming_value = command;

will have the same results

Good Luck.
Gaby.//

The underlying question is why do you need two variables with the same value? :slight_smile:

1 Like

Yeah, I quote myself hoping the OP will give us more details: :wink:

you better explain your target or requirement, possibly with a sample code to let us better understand, because the question seems to be quite trivial (or wrong).

2 Likes

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