Code for showing data on display via comport

Hello all, I am total newbie when it comes to programming but have been able to get a specific software (Prosim) to show data on a 7-segment display via a Max7219 chip connected to Arduino. To be honest, ChatGPT has done most of it and I have tested it and made some progress. Until now.
The Prosim software can communicate via a serial port and sends and receives data on the syntax [label]=[value]. The label is arbitrary and is defined in the Prosim software by the user. So far so good.
The problem arises when I want to use another data input as a condition for the first. What I want to do is this: when the parameter AC power is on (sends value 1 to the Arduino) I want the code to display the data from the first label as normal. But when Prosim send AC power is off (value 0) then I want the display to show "- - -". When Prosim sends AC Power on again I want to restore the value that was shown before I turned AC off. The program always starts with AC off and that would be nice to reflect on the display, thus showing the "---" on startup. I have spent hours trying to persuade ChatGPT to implement this for me, but without luck. Perhaps someone can point me in the right direction, would be very much appreciated.

Best regards

Oskar

Welcome to the forum

The forum is receiving an ever increasing number of requests for help with code written by AI systems that does not work. This is very annoying and some members, including me, are reluctant to provide such help

Instead of spending hours trying to persuade ChatGPT to write code for you I suggest that it would be better use of your time to learn to write the code for yourself

3 Likes

Thank you HeliBob for your reply. I can respect that. I have a partly working code but I have not been able to produce the last thing which I am after. I'll have to keep trying and see if I can get it to work.

You could also ask ChatGPT to debug its code.

1 Like

Not entirely sure if you are being sarcastic or not, but is this possible?

1 Like

under a pure technical aspect it is possible. I think even some nonsense character-sequence like

"ksjhdkjsfhskjdhfksjhdfkjh" will be transmitted to chatGPT.

It will be very interesting what the reaction of chatGPT will be if you prompt chatGPT with something like

"your code does ........" But I want the code to ........"

where "......." must be the most precise description of the watched function / the wanted functionality that you are able to write

chatGPTs answers are based on probabilities. Though if chatGPT does not find a 100.000% match it takes the 99.9% match or whatever below 100.000% value it finds

But a match below 100.000% means not working code.
Your description that you gave in the initial posting is vague and not really understandable.

If your prompt to chatGPT is of similar quality as the description in post #1 chatGPT will have a very hard time to find a 100.000%-match for such a description.

To simplify that: ChatGPI has no intelligence whatsoever.

That is pretty much the modus I've used so far. The biggest concern is that ChatGPT has no idea of how Prosim works except from what I copied and pasted from its user manual, and the the part regarding generic comports is quite scarce. So theres a a lot of trial and error which has worked fine until now. Even though I tell ChatGPT that the code doesn't work, it doesn't come up with a viable new solution, it just makes small adjustments. What I would like is to create a boolean variable named AConoff and use the arbitrary label 1023. As the gate in Prosim called AC Power outputs sends new data, for example 1023=1, I want this value to change the boolean variable to TRUE, and vice versa for 1023=0. Then I would like the other label (let's call it 1022) to listen to AConoff. If AConoff = TRUE, then it should display its value on the display. If FALSE, I would like it to show "- - -".
So far I have been able to show the "- - -" but unable to show the numbers when the AC Power gate is used. If I (rather ChatGPT) just produce a code just to show the numbers from the label 1022 on the display, it works just fine. The culprit seems to be the when using the 1023 label in combination with 1022.

If you want any help from humans you will have to describe:

what is the character-sequence that is sended from pro-sim to the arduino
quoted as a character for character for character sequence that REALLY contains ALL characters.

This part of your description is an example of beeing too unprecise

I can do assumings but making assumings is prone to errors

you should write the description in a precision like this

pro-sim sends the character-sequence "1023=1"CR-NL to the arduino.
inside the arduino-code if receiving the character-sequence
"1023=1"CR-NL
shall make the code store value true in the variable with name "AConoff"

pro-sim sends the character-sequence "1023=0"CR-NL to the arduino.
Inside the arduino-code if receiving the character-sequence
"1023=1"CR-NL
shall make the code store value false in the variable with name "AConoff"

Do you see the difference?

Each time and if I write each time I mean each and every time all details are named with their real name exactly identifying what it means eliminating the room for speculations down to zero.

not really clear what that means.
If you want to show different numbers than "1" or "0" these numbers have to be transmitted by pro-sim.
So far you have only described pro-sim sends either
"1023=1"CR-NL
or
"1023=0"CR-NL

So from where the heck shall the arduino know to show what different number?
It might be that you have this running and it works.
But then you have totally missed to add the description to your posting how it is done.

How is the label (1022) - that is defined inside Pro-Sim - able to
"listen"

to a variable (AConoff) that is defined inside the arduino-code?????

Do you see now that each and every detail matters?

best regards Stefan

Well, I suppose this is what differs us, you are a pro and I am not.
But all in all your assumptions are correct, though I am not sure what the CR-NL means.

I'm sorry if I was unclear. Prosim just sends an ASCII string in the format of label [β€˜=’ ]. For the label 1022, this is Cruise altitude (I have chosen the label 1022 myself, can be virtually anything) and can for example be 25000. This value is what I want to show on my display.

The label 1023 is controlling the AC Power. When AC power is on (1023=1), I want 1022=[value] to be displayed. When AC power is off and Arduino receives 1023=0, I want the display to be "off" by showing "- - -". I hope this makes it clearer.

This is a semantic error on my behalf, of course a label can't listen to anything. What I want to achieve is that the display shows the value or not based on the condition above.

Hi @apeshaft,

it would be of great assistance if you could give some more information

Please put the code between the code tags, see the gray marked symbol in the editor window:

image

Correct, that's the software.

Prosim is able to receive and send data through a generic serial comport, using the syntax described above.

Using the Generic driver
ProSim737 contains a β€œGeneric” driver that communicates in clear text over a COM port
or over a network through TCP port. It allows you to read and write states of switches and
indicators quickly and efficiently. The generic driver is available in the β€œDrivers” tab of the
configuration screen of ProSim737 as β€œGeneric COM port/TCP driver”.
After enabling the driver, Switches, Indicators, Analog values and encoders can be
configured to work with driver. In the first drop down menu of the item, select either a COM
port or β€œGeneric driver TCP”. Next, use the text field next to the dropdown box to enter a
label for this IO Element. The name must be unique.
Communication with the driver is either through the configured COM port or through the TCP
port. The driver listens on TCP port 8091 for incoming connections. The protocol is straight
ASCII and consists of reports separated by the newline character. Each report is in this
format:
[β€˜=’ ]
Where is the label that was configured in the configuration screen and is the
value of the item. If β€˜=’ is omitted, the value is assumed to be β€˜1’.
Specific instructions per IO element type:
Switches Configure each switch state. To change a switch, send the name of
the switch state that is currently active.
Indicators Indicators are reported with value β€˜0’ for off, β€˜1’ for on and β€˜2’ for
bright.
Gates: A β€˜0’ is sent for off and a β€˜1’ is sent for on.
Analog elements: Send the value of the element to change it.
Numerical outputs: The output is reported with its current value.
Encoders: Send a β€˜1’ for a clockwise rotation and a β€˜-1’ for counter clockwise.
Send this value each time the encoder is rotated.

#include "LedControl.h"
#include <Arduino.h>

// Define the pins connected to MAX7219
const int dataInPin = 33;   // Data pin connected to MAX7219
const int loadPin = 22;     // Load (CS) pin connected to MAX7219
const int clockPin = 23;    // Clock pin connected to MAX7219
const int numDevices = 1;   // Number of MAX7219 devices connected

LedControl lc = LedControl(dataInPin, clockPin, loadPin, numDevices);
const String hardcodedLabel = "1022"; // Hardcoded label

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  lc.shutdown(0, false); // Wake up MAX7219 (0 for device 0)
  lc.setIntensity(0, 8); // Set brightness level (0-15)
  lc.clearDisplay(0);    // Clear the display (0 for device 0)
}

void loop() {
  if (Serial.available() > 0) {
    String receivedData = Serial.readStringUntil('\n'); // Read data until newline character
    
    if (receivedData.length() > 0) {
      int separatorIndex = receivedData.indexOf('=');
      if (separatorIndex != -1) {
        String label = receivedData.substring(0, separatorIndex);
        String value = receivedData.substring(separatorIndex + 1);

        if (label.equals(hardcodedLabel)) { // Check if received label matches the hardcoded label
          if (value.toInt() == -1) {
            lc.clearDisplay(0); // Turn off the display if value is -1
          } else {
            // Display the value on the MAX7219 display (assuming it's within 8 characters)
            displayTextReverse(value); // Using the function for reversed display
          }
        } else {
          lc.clearDisplay(0); // Turn off the display if label doesn't match
        }
      }
    }
  }
}

// Function to display text in reverse order on the MAX7219
void displayTextReverse(const String& text) {
  int textLength = text.length();

  // Calculate the number of characters to display based on the length of the received value
  int displayLength = min(textLength, 8); // Maximum 8 segments available

  for (int i = 0; i < displayLength; i++) {
    char character = text.charAt(i);
    if (isAlpha(character)) { // Check if the character is a letter
      lc.setRow(0, 7 - i, charToSegment(character)); // Reverse order while writing
    } else {
      lc.setChar(0, 7 - i, character, false); // Reverse order while writing
    }
  }

  // Clear any remaining segments if the text was less than 8 characters
  for (int i = displayLength; i < 8; i++) {
    lc.setChar(0, 7 - i, ' ', false); // Reverse order while writing
  }
}

// Function to convert a letter to its corresponding segment data
byte charToSegment(char character) {
  byte segmentData;
  switch (character) {
    case 'H':
    case 'h':
      segmentData = B01110110; // Segment data for letter 'H' (or 'h')
      break;
    case 'S':
    case 's':
      segmentData = B01011011; // Segment data for letter 'S' (or 's')
      break;
    case 'N':
    case 'n':
      segmentData = B1110110; // Segment data for letter 'N' (or 'n')
      break;
    case 'E':
    case 'e':
      segmentData = B01011110; // Segment data for letter 'E' (or 'e')
      break;
    case 'W':
    case 'w':
      segmentData = B0111110; // Segment data for letter 'W' (or 'w') displayed as 'U'
      break;
    case '6':
      segmentData = B10111111; // Segment data for number '6'
      break;
    case '-':
      segmentData = B00000001; // Segment data for sign '-'
      break;
    default:
      segmentData = 0; // If the letter is not supported, display nothing
      break;
  }
  return segmentData;
}

I am repeating myself:

As long as you are unable to post

REAL

example-data that shows

character for character

how the pro-sim data looks like

it is impossible to modify the code in a way that makes the code work

I'm sorry but I can't be more specific. The data is transferred from Prosim to the Arduino in clear text; if the display is supposed to show 25000 for label 1022 the format is 1022=25000. What more information are you looking for?

An example that shows the real data.

So does this mean your pro-sim sends some time
1023=1
1022=25000

some other time
1023=1
1022=24000

and that's all?

What is so hard about posting such a character-sequence that you insist on posting verbal descriptions?
Start your fingers aching if you type these character-sequence: 1022=23000
Do get a heavy headache from it?
I guess not.

Or is there are range of values that can be beyond the "1022="
Then it is important to know this range

I am sorry if I upset you. The values differ because the displays in a cockpit can represent everything from present GPS position to height and Volts. The cruise altitude will shift depending on how high the cruise will be but the operational ceiling of a 737 is 41000 feet.

Are you using an Arduino MEGA?

Yes!

@StefanL38 Would a table presentation be helpful? If I read the OP's words right, he has the following sort of relationship:
(@apeshaft This example is completely made up, you've got to fill it in with pertinent information)
Variable Name Variable key# Variable min Value Variable max Value
Tail light 1023 0 1
Elevator 1024 -15 15

That's what I'm getting, anyway. Prosim seems to use a key#, rather than a variable label, for transmitting data.

HTH

From what I read the relevant information is as follows:

Each report is in this format: [β€˜label=’value ]

  • The label that was configured in the configuration screen on the left hand side of '='
  • The value of the specific label may be printed to the right (of the '=')
  • If β€˜=’ is omitted, the value is assumed to be β€˜1’.

Specific instructions per IO element type are:

  • Switches: To change a switch, send the name of the switch state that is currently active
  • Indicators: Are reported with value β€˜0’ for off, β€˜1’ for on and β€˜2’ for bright.
  • Gates: A β€˜0’ is sent for off and a β€˜1’ is sent for on.
  • Analog elements: Send the value of the element to change it.
  • Numerical outputs: The output is reported with its current value.
  • Encoders: Send a β€˜1’ for a clockwise rotation and a β€˜-1’ for counter clockwise. Send this value each time the encoder is rotated.