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]);
 }
}
jsotola
1,5342 gold badges12 silver badges20 bronze badges
asked Dec 7, 2018 at 17:36
7
  • 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. Commented Dec 7, 2018 at 17:42
  • use common anode display. and int number display very well like displayDigit(5) but the code did not work... thanks Commented Dec 7, 2018 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. Commented Dec 7, 2018 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. Commented Dec 7, 2018 at 19:21
  • 1
    displayDigit(3); works... string parsing problem... thanks Commented Dec 7, 2018 at 19:24

1 Answer 1

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'

answered Dec 7, 2018 at 19:28
5
  • i can't understand that... anyway apply IncomingData[i] - '0' ... it's not works... thanks Commented Dec 7, 2018 at 19:39
  • @김태정, int digit = IncomingData[i] - '0' Commented Dec 7, 2018 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? Commented Dec 7, 2018 at 19:45
  • woww@.@ it works! thanks a lot! How can I understand the theory...! Have a nice day! Commented Dec 7, 2018 at 19:46
  • how can i accept the answer?... Commented Dec 7, 2018 at 19:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.