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]);
}
}
1 Answer 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김태정– 김태정2018年12月07日 19:39:43 +00:00Commented Dec 7, 2018 at 19:39
-
@김태정,
int digit = IncomingData[i] - '0'
2018年12月07日 19:42:55 +00:00Commented 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
?2018年12月07日 19:45:30 +00:00Commented Dec 7, 2018 at 19:45 -
woww@.@ it works! thanks a lot! How can I understand the theory...! Have a nice day!김태정– 김태정2018年12月07日 19:46:31 +00:00Commented Dec 7, 2018 at 19:46
-
how can i accept the answer?...김태정– 김태정2018年12月07日 19:56:50 +00:00Commented Dec 7, 2018 at 19:56
displayDigit(digit);
todisplayDigit(3);
to see if the problem is in the string parsing, or in the led-displaying part.