0

I'm an Arduino newbie having problems with 4 digit 7 segments display. I want to display some thousands to 4 digit 7 segments display but it display only 0000. I want to receive data from serial with string. And display the data to 4 digit 7 segments display... So test make String = '2575'... but it's not work. What's wrong in my codes? Any suggestions here will be a great help. thanks

int segments[] = {A0, A1, A2, A3};

byte digits[10][7] =
{
  { 1,1,1,1,1,1,0 }, // 0
  { 0,1,1,0,0,0,0 }, // 1
  { 1,1,0,1,1,0,1 }, // 2
  { 1,1,1,1,0,0,1 }, // 3
  { 0,1,1,0,0,1,1 }, // 4
  { 1,0,1,1,0,1,1 }, // 5
  { 1,0,1,1,1,1,1 }, // 6
  { 1,1,1,0,0,0,0 }, // 7
  { 1,1,1,1,1,1,1 }, // 8
  { 1,1,1,0,0,1,1 }  // 9
};

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

  for(int i=2;i<10; i++) {
    pinMode(i, OUTPUT);
  }

  for(int i=0; i<4; i++) {
    pinMode(segments[i], OUTPUT);
  }
}

void loop() {
  String IncomingData = "2575";

  for(int i=0; i<4; i++) {
    digitalWrite(segments[i], LOW);
    int digit = atoi(IncomingData[i]);
    displayDigit(digit);
    delay(5);
    digitalWrite(segments[i], HIGH);
  }   
}

void displayDigit(int num){
  int pin = 2;
  for(int i=0;i<7;i++){
    digitalWrite(pin+i, digits[num][i]);
  }
}
  • Are you sure you have a common cathode display? Do you have current limit resistors on each segment pin? Also, why do you have int for everything, when byte is all that is needed for a 0,1 or 0 to 9? Try adding a Serial.print after this line int digit = atoi(IncomingData[i]); to see that digit is breaking out what you think it is. – CrossRoads Dec 7 '18 at 17:42
  • use common anode display. and int number display very well like displayDigit(5) but the code did not work... thanks – 김태정 Dec 7 '18 at 17:44
  • Code is setup for common cathode; low on the cathode to select the digit, high on the segments to turn them on. If you are using common anode, then reverse the logic; high on the digit to select one, low on the segments to turn them on. – CrossRoads Dec 7 '18 at 17:48
  • 1
    Change displayDigit(digit); to displayDigit(3); to see if the problem is in the string parsing, or in the led-displaying part. – Gerben Dec 7 '18 at 19:21
  • 1
    displayDigit(3); works... string parsing problem... thanks – 김태정 Dec 7 '18 at 19:24
1

You call atoi() with a char. Parameter of atoi is const char*. To convert ASCII code of digit to the digit use

int digit = IncomingData[i] - '0'

  • i can't understand that... anyway apply IncomingData[i] - '0' ... it's not works... thanks – 김태정 Dec 7 '18 at 19:39
  • @김태정, int digit = IncomingData[i] - '0' – Juraj Dec 7 '18 at 19:42
  • you do not know what is ASCII code? how characters are encoded? or you don't know why IncomingData[i] is char and can't be used with itoa? – Juraj Dec 7 '18 at 19:45
  • woww@.@ it works! thanks a lot! How can I understand the theory...! Have a nice day! – 김태정 Dec 7 '18 at 19:46
  • how can i accept the answer?... – 김태정 Dec 7 '18 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.