I'm using serial communication to a display board that has 4 7-segmente displays. The board works fine. I used the hardware freeduino serial communication to send through usb to see it working. Using linux command-line and Br@y Terminal on windows.
I read the arduino documentation for the need funstions syntax. The display board has a protocol that I must respect to work. The first few bytes are configuration ones so I can keep that fixed. The last few bytes are data.
If I use directly chars on my char array the display board works fine, as an example:
Display[DATA1] = '1';
Shows the 1 in the appropiate place.
Now if I use a less than 10 converted integer with int(), the things goes wrong.
Example:
Display[DATA] = int(myInteger);
I know i can make this less than 10 number with a swicth as follows:
char conv(int a) {
switch(a){
case 1: return '1'; break;
case 2: return '2'; break;
....
}
}
The value of the expression int(myInteger) is the integer 1, which in C is different than '1'. 1 is 1. '1' is the ASCII encoding for the printable digit 1, or, in decimal terms, the number 49. (See www.asciitable.com.)
If you want to fix your broken expression, turn it to
upps...my bad! Seems working too late makes my shorts in my brain.
What I actually meant to say was "char()" function :S sorry.
I managed to solved that with a nasty solution, i did this:
void build_digit_array(int number){
int i = 0;
int digit;
for(i; i< MAX; i++) {
digit = number;
digit = digit % 10;
number = number /10;
digit_array[MAX-1-i] = digit;
}
}
And then I use the "conv" function as stated before. It's working now. But what I wanted to say was that the "char(myInteger" won't work.
More specific:
If I use in my code the char convertion function, the display do not show what he got to.
Example:
Display[DATA] = char(1);
Don't work for me.
Instead If I do this:
char conv(int a)
{
switch(a){
case 0: return '0'; break;
case 1: return '1'; break;
case 2: return '2'; break;
case 3: return '3'; break;
case 4: return '4'; break;
case 5: return '5'; break;
case 6: return '6'; break;
case 7: return '7'; break;
case 8: return '8'; break;
case 9: return '9'; break;
}
}
int init_digit_array(){
int i = 0;
for ( i ; i < MAX ; i++)
digit_array[i] = 0;
}
int build_digit_array(int number){
int i = 0;
int digit;
for (i = 0; i < MAX; i++) {
digit = number;
digit = digit%10;
number = number/10;
digit_array[MAX-1-i] = digit;
}
}
...
Display[DATA1] = conv(digit_array[0]);
Display[DATA2] = conv(digit_array[1]);
...
Works perfectly.
Sorry, i missed "int()" in the previous post for "char()"
The only difference in C between the "char" type and the "int" type is that char is (usually) smaller. So what I said in my previous post still applies. char(1) is 1, not '1'.
The fix is the same: use the expression char(x) + '0' instead of char(x). Or just rewrite your conv function like this:
Just a side note on the type conversions discussed above. The standard Arduino headers are moving away from the unusual char(x) and int(x) syntax, and using the industry-standard (char)x and (int)x syntax instead. The former variants will not be supported at all in Arduino IDE 0013, from what I understand, and current code and tutorials should not advocate them. Everything mikalhart is saying about how the values work is dead-on correct, though.
The char(x) and int(x) syntax will still work and will still be preferred in the tutorials, etc. We're just removing the explicit #define's for them, since they exist by default in C++.
I don't want to derail the original question in the thread, but I think that's a strategic mistake. You support C++, it should be done in the C++ way. The platform designers should be able to defend every macro that exists in the Arduino platform as a functional or instructional advantage. That syntax is not more functional (reports of broken libraries), and it doesn't make it easier to instruct (they can't apply their training elsewhere without learning of the idiomatic difference).
The int(x) style is called "constructor style." It's not well-thought of in the general C++ community, and for non-class types, not supported in all C++ compilers. While it is valid modern C++, it is not valid in modern or old C code. So without the macro, the int(x) won't compile in .c (plain C) tabs in your sketch. A minor example: C++ cast syntax styles - Stack Overflow