setting a value using the serial monitor

Hello

I want to be able to change a value using the serial monitor

for instance: when I type "VAL = 100" in the serial monitor the value of VAL will be 100

Ive been looking on multiple websites/ and even looked at a few posts on this forum but I can't find an
answer that works.

This is currently what I have

int value = 0;

void setup() 
{
 Serial.begin(9600);
 pinMode(value, OUTPUT);

}

void loop()
{
  Serial.println(value);
  delay(500);

  if(Serial.available()>0)
  {
   value=Serial.parseInt();
  }
}

Thank you

This is currently what I have

And. . . ?

Are you saying you actually type "VALUE = 100", or just "100"?

TheMemberFormerlyKnownAsAWOL:
And. . . ?

Are you saying you actually type "VALUE = 100", or just "100"?

Yes, I want to be able to type "value = 100" in the serial monitor to change its value.
When I type it the value changes to 49 for a few seconds, after that it goes to 10 and stays there.

But you can only ever change the value of the variable "value", because you're not parsing the string "value =".

Change the serial monitor line ending setting to "no line ending", or handle the line ending in your code.

TheMemberFormerlyKnownAsAWOL:
But you can only ever change the value of the variable "value", because you're not parsing the string "value =".

Change the serial monitor line ending setting to "no line ending", or handle the line ending in your code.

"Change the serial monitor line ending setting to "no line ending", or handle the line ending in your code."

What do you mean with this? Ive never heard of "no line ending"

Across the bottom of the serial monitor window should be a number of controls (left to right):
"Autoscroll", "Show timestamp", then a drop-down. Select "no line ending" from the drop-down.
Retest and report.

1. This is the layout (Fig-1) of Serial Monitor.


Figure-1:

2. If we open the "Line ending tab", we will see the following options:
(1) No line ending
(2) Newline
(3) Carriage return
(4) Both NL & CR

3. If we choose Newline option and then enter 5 in the InputBox and then click on the Send button, then the following two bytes data (known as ASCII Code, see Fig-2) will be transmitted to UNO one after another. (Note that there are START bit (LOW) and STOP bit (HIGH) with every ASCII byte, which are not shown purposely. Actual bit transmission sequence for 5 is: (LOW LSBit . . . . . . MSBit STOP = 0101011001)

00110101 00001010 //space is shown for clarity

It means that 0011 0101 (0x35; 0x says that 35 is a hexadecimal number) will be transmitted first for 5 and then 0000 1010 (0x0A) will be transmitted for Newline (a non-printable character).


Figure-2:

3. If we choose No line ending option and then enter 5 in the InputBox and then click on the Send button, then only the ASCII code of 5 (00110101) will be transmitted to UNO.

4. If you choose Newline option and then enter Value = 100 in the InputBox and then click on the Send button, then the following ASCII Codes will be transmitted to UNO. We are writing the ASCII code in hex notation for convenience; but, the actual transmission occurs in bit form. Also, not that that there are START bit (Low) and STOP bit (HIGH) with each ASCII code, which are not shown.

0x56 for V
0x61 for a
0x6C for l
0x75 for u
0x65 for e
0x20 for space
0x3D for =
0x20 for space
0x31 for 1
0x30 for 0
0x30 for 0
0x0A for Newline character whos C-Language notation is: '\n'

5. The following sketch (tested) at the UNO side will receive the string (Value = 100) coming from the InputBox of Serial Monitor and will be written back to the OutputBox of Serial Monitor. The numerical value 100 will also be extracted from the received string and would be saved in variable z. The value of z will also be shown on Serial Monitor.

char myData[20];   //array to hold received string

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

void loop() 
{
  byte y = Serial.available(); //check if a character has arrived
  if(y != 0)
  {
    byte m = Serial.readBytesUntil('\n', myData, 20); //receive charcaters until Newline is detected
    myData[m] = '\0'; //insert null-character at end of character array - char myData[]
    Serial.println(myData);  //show the received string
    //------extract 100 from Value = 100 ---------------------------
    memset(myData, 0x30, 8); //replace first 8 characters of myData[] array by ASCII codes of 0s
    int z = atoi(myData);   //use atoi() (ASCII to Integer) function; z = 0x64 = 100
    Serial.println(z, DEC);  //Serial Monitor shows: 100
    //---------------------------------------------------------------
    memset(myData, 0x00, 20);     //reset the whole arry to 0s. 
  }  
}

6. Upload sketch of Step-5.
7. Choose Newline option for the Line ending tab. Enter Value = 100 in the InputBox and then click on the Send button. Check that the following message has appeared on Serial Monitor.

Value = 100
100

8. Repeat Step-7 with Value = 365. Check that the following message has appeared on Serial Monitor.

Value = 365
365

Some interesting read: Robin's updated Serial Input basics

You might / will have to mix and match some parts of the various examples if you decide to base your code on it.

"no line ending",

"No line ending"

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