5 Digit 7-Segment Display Writing Error

Good day, the first digit of the number I sent to the 5 Digit 7-Segment Display via Bluetooth is not visible. The results of different sendings are below. I would be glad if you help.

#include <SPI.h>
#include <SoftwareSerial.h>


int exctDigit(byte input);

void dataWrite(int st);
int latchPin = 5;   // to pin 12
int clockPin = 6;  // to pin 11
int dataPin = 4;  // to pin 14
SoftwareSerial mySerial(7, 8); // RX, TX

boolean sayi[10][8] =
{
{0,1,1,1,1,1,1,1},
{0,1,1,0,0,0,0,1},
{1,0,1,1,0,1,1,1},
{1,1,1,1,0,0,1,1},
{1,1,1,0,1,0,0,1},
{1,1,0,1,1,0,1,1},
{1,1,0,1,1,1,1,1},
{0,1,1,1,0,0,0,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,1,1}
};
boolean dizi[] = {
  1, 0, 0, 0, 0, 0, 0, 1,
  1, 0, 0, 1, 0, 0, 1, 1,
  1, 0, 0, 1, 0, 0, 1, 1,
  1, 0, 0, 1, 0, 0, 1, 1,
  1, 0, 0, 1, 0, 0, 1, 1};  //sadece "1"lerden ve "0"lardan oluşan dizi
byte b1 = 0;        // byte place holders for each shift register
byte b2 = 0;        // extracted from "light"
byte b3 = 0;
byte b4 = 0;
byte b5 = 0;
int x = 0;
unsigned long sa = 2541;

int parseIntArray[5];


void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  for (int i = 0 ; i < 5 ; i++)     
  {
    for(int k = 0;k <8;k++)
    {
      
       x =  sayi[0][k];                    //for dönügü boyunca dizinin i. değeri x'e atanır
    
    digitalWrite(dataPin, x);       //datapin'e x değeri verilir
    digitalWrite(clockPin, 1);      //saat darbesi ile yazma ve kaydırma işlemi yapılır
    digitalWrite(clockPin, 0);
    }
    
  }
  digitalWrite(latchPin, 1);        //8 bitli dizi çıkışa verilir


}

void loop() {
 byte buffer[8];
  // put your main code here, to run repeatedly:

  
   int numberOfData = mySerial.available() ;
   
    if(numberOfData>0)
      {
      unsigned long  k=0;
      
      for(int i=1;i<=numberOfData;i++)
      {
         unsigned long ust =1;
         for(int s =0;s<numberOfData-i;s++)
         {
          ust = ust * 10;
         }
         k +=(unsigned long) exctDigit (mySerial.read())*(unsigned long)ust;
          
      }
     
      dataWrite(k);
      buffer[0] =0;
      buffer[1] =0;
      buffer[2] =0;
      buffer[3] =0;
    

 }
}
void dataWrite(unsigned long st)
{
    digitalWrite(latchPin, 0);        //latchpin "0" konumuna getirilir
    
 parseIntArray[4]= (st%1000)/100;
 parseIntArray[3]= (st%10000)/1000;
 parseIntArray[2]= (st%100000)/10000;
 parseIntArray[1]= (st%1000000)/100000;
 parseIntArray[0]= (st%10000000)/1000000;

 
 for (int i = 0 ; i < 5 ; i++)     
  {
    for(int k = 0;k <8;k++)
    {
      
       x =  sayi[parseIntArray[i]][k];                    //for dönügü boyunca dizinin i. değeri x'e atanır
    
    digitalWrite(dataPin, x);       //datapin'e x değeri verilir
    digitalWrite(clockPin, 1);      //saat darbesi ile yazma ve kaydırma işlemi yapılır
    digitalWrite(clockPin, 0);
    }
    
  }
  digitalWrite(latchPin, 1);        //8 bitli dizi çıkışa verilir

}
int exctDigit(byte input)

{
  switch(input)
        {
                case '0':       return 0;
                case '1':       return 1;
                case '2':       return 2;
                case '3':       return 3;
                case '4':       return 4;
                case '5':       return 5;
                case '6':       return 6;
                case '7':       return 7;
                case '8':       return 8;
                case '9':       return 9;
                
        }
        return 0; 

}








really?

2 Likes

Have you already worked with ModBUS RTU?
Do you have a working Master and a working Slave already?
What function codes and what registers should be used?

1 Like

parseIntArray[0]= st%10;
parseIntArray[1]= (st%100)/10;
parseIntArray[2]= (st%1000)/100;
parseIntArray[3]= (st%10000)/1000;
parseIntArray[4]= (st%100000)/10000;

May be better implemented like this.

parseIntArray[0]= st%10;
parseIntArray[1]= (st/10)%10;
parseIntArray[2]= (st/100)%10;
parseIntArray[3]= (st/1000)%10;
parseIntArray[4]= (st/10000)%10;

1 Like

@burak27471 ,

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Sorry, the first title was about display communication. The second title was about display writing. I didn't know that opening titles on similar topics was forbidden. Have a nice day.

1 Like

If they are related but you feel they should be separate at least include a link to the other topic along with an explanation so people can see what is going on.

1 Like

@burak27471 has the issue been solved?

1 Like

I still haven't been able to solve the problem. I did as you suggested and it didn't work. The first digit of the number I sent to the 5 Digit 7 Segment Display via Bluetooth does not appear. I couldn't understand where the error was in the program. The results of different submissions are above.

If you print the number out out to Serial, what does it say?

1 Like

When I type 1234 it gets 00234,
when I type 12345 it gets 02345,
when I type 123456 it gets 23456
The first digit is always written missing

The issue is most likely somewhere in how you are decoding the data,
I went through most of the code and modified it, hopefully it works without any issues now.
Comments have been added to try and help you understand what is happening.

void loop() {
  // Check to see if the client is connected, if it isn't connected we will reconnect.
  // This loop isn't ideal however....I am not sure if it is even necessary....
  while(!client.connected())
  {
    client.stop();
    delay(1000);
    client.connect(server, 6789);
  }

  // Now that we have a successful connection we will decode the data.
  int numberOfData = client.available();
  
  // Do we have any data?
  if (numberOfData > 0)
  {    
     // Yes, we will now loop until all our data has been received.
     for (byte i=0; i <= numberOfData; i++)
     {
         // Put our data into our array.
         // The ASCII code for '0' is 48, if we subtract 48 it will equal to zero.
         // e.g. '0' - '0' = 0 or '1' - '0' = '1'
         parseIntArray[i] = client.read() - '0'; 
     }
      // Display the data in our array now.
     dataWrite();
  }
}

void dataWrite()
{
    digitalWrite(latchPin, 0);        //latchpin "0" konumuna getirilir

   for (int i = 0 ; i < 5 ; i++)     
   {
      for (int k = 0;k <8;k++)
      {
          x =  sayi[parseIntArray[i]][k];                    //for dönügü boyunca dizinin i. değeri x'e atanır
    
         digitalWrite(dataPin, x);       //datapin'e x değeri verilir
         digitalWrite(clockPin, 1);      //saat darbesi ile yazma ve kaydırma işlemi yapılır
         digitalWrite(clockPin, 0);
      }
   }

  digitalWrite(latchPin, 1);        //8 bitli dizi çıkışa verilir
}
1 Like

Thanks for your help. This is the result.

#include <SPI.h>
#include <SoftwareSerial.h>


int exctDigit(byte input);

void dataWrite(int st);
int latchPin = 5;   // to pin 12
int clockPin = 6;  // to pin 11
int dataPin = 4;  // to pin 14
SoftwareSerial mySerial(7, 8); // RX, TX

boolean sayi[10][8] =
{
{0,1,1,1,1,1,1,1},
{0,1,1,0,0,0,0,1},
{1,0,1,1,0,1,1,1},
{1,1,1,1,0,0,1,1},
{1,1,1,0,1,0,0,1},
{1,1,0,1,1,0,1,1},
{1,1,0,1,1,1,1,1},
{0,1,1,1,0,0,0,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,1,1}
};
boolean dizi[] = {
  1, 0, 0, 0, 0, 0, 0, 1,
  1, 0, 0, 1, 0, 0, 1, 1,
  1, 0, 0, 1, 0, 0, 1, 1,
  1, 0, 0, 1, 0, 0, 1, 1,
  1, 0, 0, 1, 0, 0, 1, 1};  //sadece "1"lerden ve "0"lardan oluşan dizi
byte b1 = 0;        // byte place holders for each shift register
byte b2 = 0;        // extracted from "light"
byte b3 = 0;
byte b4 = 0;
byte b5 = 0;
int x = 0;
unsigned long sa = 2541;

int parseIntArray[5];


void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  for (int i = 0 ; i < 5 ; i++)     
  {
    for(int k = 0;k <8;k++)
    {
      
       x =  sayi[0][k];                    //for dönügü boyunca dizinin i. değeri x'e atanır
    
    digitalWrite(dataPin, x);       //datapin'e x değeri verilir
    digitalWrite(clockPin, 1);      //saat darbesi ile yazma ve kaydırma işlemi yapılır
    digitalWrite(clockPin, 0);
    }
    
  }
  digitalWrite(latchPin, 1);        //8 bitli dizi çıkışa verilir


}

void loop() {
 byte buffer[8];
  // put your main code here, to run repeatedly:

  
    int numberOfData = mySerial.available();
  
  // Do we have any data?
  if (numberOfData > 0)
  {    
     // Yes, we will now loop until all our data has been received.
     for (byte i=0; i <= numberOfData; i++)
     {
         // Put our data into our array.
         // The ASCII code for '0' is 48, if we subtract 48 it will equal to zero.
         // e.g. '0' - '0' = 0 or '1' - '0' = '1'
         parseIntArray[i] = mySerial.read() - '0'; 
     }
      // Display the data in our array now.
     dataWrite();
  }
}

void dataWrite()
{
    digitalWrite(latchPin, 0);        //latchpin "0" konumuna getirilir

   for (int i = 0 ; i < 5 ; i++)     
   {
      for (int k = 0;k <8;k++)
      {
          x =  sayi[parseIntArray[i]][k];                    //for dönügü boyunca dizinin i. değeri x'e atanır
    
         digitalWrite(dataPin, x);       //datapin'e x değeri verilir
         digitalWrite(clockPin, 1);      //saat darbesi ile yazma ve kaydırma işlemi yapılır
         digitalWrite(clockPin, 0);
      }
   }

  digitalWrite(latchPin, 1);        //8 bitli dizi çıkışa verilir
}

int exctDigit(byte input)

{
  switch(input)
        {
                case '0':       return 0;
                case '1':       return 1;
                case '2':       return 2;
                case '3':       return 3;
                case '4':       return 4;
                case '5':       return 5;
                case '6':       return 6;
                case '7':       return 7;
                case '8':       return 8;
                case '9':       return 9;
                
        }
        return 0; 

}






Looks like there is still an issue with receiving the data....I will try to find what is going on....

1 Like

try

//#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8); // RX, TX

#define dataPin  4
#define latchPin 5
#define clockPin 6
unsigned long sa = 98765UL;

const uint8_t digit[] = 
{
  //  PGFEDCBA  Segments      7-segment map:
  //  0b00111111, // 0   "0"          AAA
  //  0b00000110, // 1   "1"         F   B
  //  0b01011011, // 2   "2"         F   B
  //  0b01001111, // 3   "3"          GGG
  //  0b01100110, // 4   "4"         E   C
  //  0b01101101, // 5   "5"         E   C
  //  0b01111101, // 6   "6"          DDD
  //  0b00000111, // 7   "7"
  //  0b01111111, // 8   "8"
  //  0b01101111  // 9   "9"
//gcbafedp
  B01111111,
  B01100001,
  B10110111,
  B11110011,
  B11101001,
  B11011011,
  B11011111,
  B01110001,
  B11111111,
  B11111011
};

void setup() {
  //  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  Display(sa);
}

void loop() {
  if (mySerial.available() > 0) // Do we have any data?
  {
    delay(10);
    sa = mySerial.parseInt();
    while (mySerial.available() > 0)mySerial.read();
    Display(sa);
    delay(1000);
  }
}

void Display(uint32_t num) {
  byte seq[5];
  num %= 100000UL; // number bigger as 99999 show only last 5 digits
  for (byte i = 0 ; i < 5 ; i++)  { // reverse digit order
    seq[5 - i] = num % 10;
    num /= 10;
  }
  // Display the data in our array now.
  digitalWrite(latchPin, 0);
  for (byte i = 0 ; i < 5 ; i++)
    shiftOut(dataPin, clockPin, MSBFIRST, digit[seq[i]]);
  digitalWrite(latchPin, 1);
}

1 Like

The problem I see is it won't pad the number out.
i.e. 125 is sent, the display will say 12500.

I have tested the code below and it works as you want it to I think.

@kolaha's code should work too, however I am not sure whether is has been tested or not.

// Move these variables if you desire.
byte data = 0;
byte dataLength = 0;

void loop() {
  byte buffer[8];
  // put your main code here, to run repeatedly:

  
  // Do we have any data?
  if (mySerial.available() > 0)
  {
    // Read out data from the Serial buffer.
    data = mySerial.read();
    
    // Put our data into our array. But only if it is a valid number.
    if (data >= '0' and data <= '9')
    {
      // The ASCII code for '0' is 48, if we subtract 48 it will equal to zero.
      // e.g. '0' - '0' = 0 or '1' - '0' = '1'
      parseIntArray[dataLength] = data - '0';

      // Increment our data counter to prepare for more data.
      dataLength++;
    }

    // Check to make sure our data counter isn't greater then five.
    // If it is then reset it back to zero and recount.
    // Also reset the data counter if the data is a carriage return or newline.
    if (dataLength >= 5 or data == 10 or data == 13)
    {
      // We have got the complete display full, so display it.
      dataWrite();

      // Reset the data counter.
      dataLength = 0;
    }
  }
}

void dataWrite()
{
  // We need this to figure out the padding amount.
  byte padding = 5 - dataLength;
  
  digitalWrite(latchPin, 0);        //latchpin "0" konumuna getirilir

  for (int i = 0 ; i < 5 ; i++)     
  {          
    for (int k = 0; k < 8; k++)
    {
      // Not sure how your screen works...
      // I am padding out the first numbers with zero if our data counter is smaller than five.
      if (dataLength < 5) 
      {
        // Automatically assign the data to zero.
        x = sayi[0][k];
      }
      else
      {
        // This should automatically align the data.
        x = sayi[parseIntArray[i - padding]][k];    //for dönügü boyunca dizinin i. değeri x'e atanır
      }
    
      digitalWrite(dataPin, x);       //datapin'e x değeri verilir
      digitalWrite(clockPin, 1);      //saat darbesi ile yazma ve kaydırma işlemi yapılır
      digitalWrite(clockPin, 0);
    }

    // Here we increment the data counter, until we have completed "padding".
    if (dataLength < 5) 
    {
      // Increment our data counter till it equals to five.
      dataLength++; 
    } 
    else 
    {
      // Reset the number back to zero, that way we won't retain old data.
      parseIntArray[i - padding] = 0;
    }
  }

  digitalWrite(latchPin, 1);        //8 bitli dizi çıkışa verilir
}
1 Like

Thanks for your answer. I got the following results with different data. After the data is gone, it always returns to the same result.

00000

11111


22222


33333


55555


12345


Thanks for your answer. The display did not respond to the data. It was at 00000 when opened.

@burak27471 and again i made change, bc bit order on this panel is different
so try now

1 Like