Extracting serial data and display to 5digit 7segment

You need to have a refresh at the end of loop()
sevseg.refreshDisplay();

Ok, between us, we have given @raffy0991 the answer completely, there is nothing left for them to work out for themselves.

Here's suggestion to improve the code, and remove use of String: there is no need to convert the received characters to a number, and then have SevSeg convert it back into separate digits to display it. You can simply use

sevseg.setChars(receivedData);

If you need to ignore the first character because it is a space or whatever, you can use

sevseg.setChars(&receivedData[1]);

Hi,

Thanks for the help i have actually revised my program. As shown below and this works. However, when I include softwareserial in the program it will just display 88888. Is there a way where I can connect the GSE460 to the serial com? is it the pin 0 and 1 of arduino? I am using softwareSerial because of the GSE460 it has an rs232 connector so I bought a rs232 to TTL converter and connected it to the A0 and A1.

Now, for some reason this program with sevseg does not work with softwareserial. Is there a way I can connect it to my arduino directly?

/* SevSeg Counter Example
 
 Copyright 2020 Dean Reading
 
 This example demonstrates a very simple use of the SevSeg library with a 4
 digit display. It displays a counter that counts up, showing deci-seconds.
 */

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
String inString = "";
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(A0, A1);

void setup() {
  Serial.begin(9600);
  //mySerial.begin(9600);
  byte hardwareConfig = N_TRANSISTORS;
  byte numDigits = 5;
  byte digitPins[] = {9, 10, 11, 12, 13};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(90);
   while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }

  // send an intro:
  Serial.println("\n\nString toInt():");
  Serial.println();
}

void loop() {
  // Read serial input:
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string, then the string's value:
    if (inChar == '\n') {
      sevseg.setNumber(inString.toInt(), 0);
      Serial.print("Value:");
      Serial.println(inString.toInt());
      Serial.print("String: ");
      Serial.println(inString);
      // clear the string for new input:
      inString = "";
    }
  }

  sevseg.refreshDisplay(); // Must run repeatedly
}

/// END ///

Have you validated that the Uno can receive data from the sensor with software serial and echo it to serial monitor successfully? (Without the display connected, and not using your sketch above).

Hi Guys,

I have generally solved the problem in my led driver circuit now my problem is with the program. The equipment send the data for example " 00.." every one second. This is received by my rs232 to TTL converter and then go to my arduino uno. My problem is that the display is not constant. What I mean when the equipment sends " 12345". The 7 segment display will be "1" then disappear then after a second display "2" then disappear then after a second displays "3" then disppears then after a second displays "4" and then disappears then "5". The display should simultaneously display the 12345 instead of one by one. Please help me check my code.

#include <SoftwareSerial.h>

SevSeg sevseg;        // Initialize the SevSeg object
SoftwareSerial mySerial(A0, A1);
String receivedData = "";   // String to store the received data
long numberValue = 0;       // Variable to store the extracted number as a long

void setup() {
  mySerial.begin(9600);       // Initialize Serial communication
  byte hardwareConfig = N_TRANSISTORS;
  byte numDigits = 5;
  byte digitPins[] = {9, 10, 11, 12, 13};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = true;
  bool updateWithDelaysIn = false;
  bool disableDecPoint = true;
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelaysIn, disableDecPoint);
  sevseg.setBrightness(10);
  //byte numDigits = 5;       // Set the number of digits in your 7-segment display
  //byte digitPins[] = {2, 3, 4, 5, 6};  // Specify the digital pins connected to the display digits (A, B, C, D, E)
  //byte segmentPins[] = {7, 8, 9, 10, 11, 12, 13};  // Specify the digital pins connected to the display segments (a, b, c, d, e, f, g)

  // The following settings can be adjusted as needed
  //bool resistorsOnSegments = false;  // Set to 'true' if resistors are on the segment pins, otherwise 'false'
  //bool updateWithDelays = false;     // Set to 'true' to enable update with delays, otherwise 'false'
  //bool leadingZeros = true;         // Set to 'true' to enable leading zeros, otherwise 'false'
  //bool disableDecPoint = true;      // Set to 'true' to disable the decimal point, otherwise 'false'

  //sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments,
  //             updateWithDelays, leadingZeros, disableDecPoint);
  //sevseg.setBrightness(90);   // Set the brightness of the display (adjust as needed)
}

void loop() {
  if (mySerial.available()) {
    char receivedChar = mySerial.read();    // Read a character from the Serial port

    // Check if the received character is a valid numerical value ('0'-'9') or a space
    if ((receivedChar >= '0' && receivedChar <= '9') || receivedChar == ' ') {
      // Skip leading spaces
      if (receivedData.length() > 0 || receivedChar != ' ') {
        receivedData += receivedChar;   // Add the character to the received data String
      }
    }
    // If the received character is a newline '\n', then convert the received data String
    // to a long integer and display it on the 7-segment display
    else if (receivedChar == '\n' && receivedData.length() > 0) {
      numberValue = extractNumberFromData(receivedData); // Convert String to long integer
      displayNumberOn7Segment(numberValue); // Display the extracted number on the 7-segment display
      receivedData = "";                 // Clear the receivedData String for the next number
    }
  }
}

// Function to extract the number from the String and convert it to a long integer
long extractNumberFromData(String data) {
  long extractedNumber = 0;
  bool isNegative = false;
  int startIndex = 0;

  // Check if the first character is a minus sign '-'
  if (data.charAt(0) == '-') {
    isNegative = true;
    startIndex = 1; // Start extracting the number from the second character
  }

  // Convert the numeric characters into a long integer
  for (int i = startIndex; i < data.length(); i++) {
    if (data.charAt(i) >= '0' && data.charAt(i) <= '9') {
      extractedNumber = extractedNumber * 10 + (data.charAt(i) - '0');
    }
  }

  // Apply the negative sign if applicable
  if (isNegative) {
    extractedNumber *= -1;
  }

  return extractedNumber;
}

// Function to display the number on the 7-segment display
void displayNumberOn7Segment(long number) {
  sevseg.setNumber(number);   // Display the extracted number on the 7-segment display
  sevseg.refreshDisplay();    // Refresh the display to show the number
  }

@raffy0991,

I have added your hijack duplicate topic to the end of this one. Please do not add your questions to the end of other people's topics and please only create one instance of your topic, having carefully selected the appropriate forum category.

Thank you.

Hi,

Thank you I have already solved it here is the code. I don't know why but removing the code for the serial.begin and serial println solved it.

/* SevSeg Counter Example
 
 Copyright 2020 Dean Reading
 
 This example demonstrates a very simple use of the SevSeg library with a 4
 digit display. It displays a counter that counts up, showing deci-seconds.
 */

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
String inString = "";
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A0, A1);

void setup() {
  
  mySerial.begin(9600);
  byte hardwareConfig = N_TRANSISTORS;
  byte numDigits = 5;
  byte digitPins[] = {9, 10, 11, 12, 13};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(90);


}

void loop() {
  // Read serial input:
  while (mySerial.available() > 0) {
    int inChar = mySerial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string, then the string's value:
    if (inChar == '\n') {
      sevseg.setNumber(inString.toInt(), 0);
      // clear the string for new input:
      inString = "";
    }
  }

  sevseg.refreshDisplay(); // Must run repeatedly
}

/// END ///

It could be that because you were using such a slow speed:

and printing to serial monitor a little too often, combined with SevSeg multiplexing the display continuously, that software serial was missing some bits of the serial data coming from the GSE460.

It might be best to switch to a different Arduino model for your final build. Arduino Leonardo, Micro and Pro Micro have a spare hardware serial port, which you could use to avoid use of software serial.