Get variables from serial

How can I include (read and write) in the sketch a variable that is being sent through the serial USB into Arduino ? I need to use them in the sketch, for example a value of an airplane current heading displayed on a flight simulator that I need to change for some degrees and then write back to the simulator.

Welcome to the forum

Your topic was MOVED to its current forum category as it is more suitable than the original as it is not an Introductory Tutorial

From the title it appears that you want to send something to UNO from the InputBox of the Serial Monitor. What data do you want send and grab it at the UNO for subsequent use?

I would suggest to study Serial Input Basics to handle this

I´ll try to clarify better what I need. I have a flight simulator (Prepar3d) that has an internal value (aircraft heading) that is appearing in the serial monitor (like 180) and I suppose is on UNO memory somewhere. What I need is to access this variable (an analog value) make calculations inside the sketch or vary it using a rotary decoder and then send back to the simulator, and so the aircraft will change heading, or any other value, like flap position, altitude, etc. Thanks for the response.

What sketch? I don't see any.

The values that appear on Serial Monitor are being sent to Serial in an Arduino sketch. What is the sketch?

So you get the value that is sent somehow from the Prepar3d software to a (unknown) sketch and that's printed on the Serial monitor.

The true question for your goal though is : Do you have an API for Prepar3d where you can send commands through a Serial interface or another way back into the Prepar3d software ?

Yes, that is a software called NunaLink, and my sketch has nothing special, I could send it to you if I knew how to do it. I can see no options to attach files to this message. If I had a mean to identify and assign the value to a variable, I could manipulate it and then send back to the simulator. It would be good if we could talk in a video sessio of facetime or whatsapp. What I can tell you is that I see the message going into UNO.

//Rotary Encoder Inputs
#define inputCLK 4
#define inputDT 5

//LED Outputs
#define ledCW 8
#define ledCCW 9

int counter=0;
int currentStateCLK;
int previousStateCLK;
int delayTime=100;
int d;

//Test read write current heading
int currentHeading;

String encdir="";

void setup() {
  // Set encoder pins as inputs
  pinMode (inputCLK,INPUT);
  pinMode (inputDT,INPUT);
  
  // Set led pins as outputs
  pinMode (ledCW,OUTPUT);
  pinMode (ledCCW,OUTPUT);

  //Setup serial monitor
  Serial.begin(115200);

  //Read the initial state of inputCLK
  //Assign to previousStateCLK variable
  previousStateCLK = digitalRead(inputCLK);
}

void loop() {

 //delay(delayTime);
  
  // Read the current state of inputCLK
  currentStateCLK = digitalRead(inputCLK);

  //Pulse has occurred ?
  if (currentStateCLK != previousStateCLK)  {
  //inputDT state is different from inputCLK state -> Clockwise
  if (digitalRead(inputDT)!=currentStateCLK)   {
     counter ++;
     encdir = "CW";
//     digitalWrite(ledCW,LOW);
//     digitalWrite(ledCCW,HIGH);
//     delay(delayTime);
//     digitalWrite(ledCCW,LOW);
   
  // clockwise
  }  else  {
    counter --;
    encdir = "CCW";
//    digitalWrite(ledCW,HIGH);
//    digitalWrite(ledCCW,LOW);
//    delay(delayTime);
//    digitalWrite(ledCW,LOW);

  }
  //Serial.print("Direction: ");
  //Serial.print(encdir);
  //Serial.print(" Value: ");
  Serial.println(counter);

  //Test read write current heading
  currentHeading=d;
  Serial.println(currentHeading);
  delay(delayTime);
   
  }
  //update previousStateCLK with the current state
  previousStateCLK = currentStateCLK;

}

this icon in the tool bar is to upload something

images can be dropped directly into the text field

but before you post more stuff, do yourself a favour and please read How to get the best out of this forum

I see you managed to post with code tags. Good.

there is nothing in that code that seems to listen to incoming data from the Serial line...

usuarios novos não podem mandar anexos...

sorry, new users can not send attachements, I receveid

ah - Eu entendo :slight_smile:

code in code tags is much better than attachments anyway. So you did it right.

Still the code just seems to deal with a rotary encoder. Nothing else

no, but as a newcomer to UNO, that´s what I don´t know how to do...

and prints out some numbers that appears on the monitor

are you sure the software sends stuff through the Serial link? if you open the Serial monitor that makes a mess if the software is using also this Serial connexion...

I can disable serial comms in the interface with simulator, so serial monitor has the port

but then the simulator does not send anything to the Arduino and the Arduino cannot talk back to the the simulator.... pretty useless...