Arduino Serial help?

Hi I have a 8x8 LED Matrix and wanted to control it from C# over serial, I’m having some trouble with getting the correct data over serial.

What I want to achieve is this…

Send 8 numbers for example 1 2 3 4 5 6 7 8 over serial (each number being a separate character)

Once the arduino receives its next 8 characters then it should output them in 8 bit binary.

//1
00000001
//2
00000010
//3
00000011
//4
00000100
//5
00000101
//6
00000110
//7
00000111
//8
00001000

This is what I have so far it kind of works but numbers are showing as 6 bits

So far my code will output this if I enter in 1 2 3 4 5 6 7 8

110001
110010
110011
110100
110101
110110
110111
111000

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

void loop()
{
//Wait for next 8 bytes
if(Serial.available() == 8 )
{
//Print the next 8 bytes in the buffer to screen
for(int i = 0; i < 8; i++)
{
char b = Serial.read();
Serial.println(b, BIN);
}
}
//Clear buffer
Serial.flush();
}

At first glance it seems you aren't taking into account delimiters - are you sending "1 2 3 4 5 6 7 8" or "12345678"? How exactly are you sending the characters?

Hi you can send them 1 character by 1 or just together works the same, at the moment im testing all this using the built in serial monitor. but in C# ill be sending 8 bytes over serial so 1 byte that = 1 should show up as 00000001 on the arduino once it receives it.

Edit I have been sending them just as 12345678 without spaces :)

I understand what you’re trying to achieve, I’m just wondering if extra data is getting in the way. For debugging purposes change your loop to -

for(int i = 0; i < 8; i++)
{
char b = Serial.read();
Serial.print(b, BIN); //drop the \n
Serial.println(""); //newline
Serial.print(b); //ascii character
Serial.println(""); //newline
Serial.println(b, BIN);
}

And see what the output is like

The output shows this

110001 1 110001 110010 2 110010 110011 3 110011 110100 4 110100 110101 5 110101 110110 6 110110 110111 7 110111 111000 8 111000

Ah I think I know what's happening.

Print the DEC, I bet it's converting the ascii character code for the number, which is not the same as the number of bits in the byte

DEC shows this from 12345678

49 50 51 52 53 54 55 56

How would I convert the numbers I send to 8 bit binary I tried Serial.println(b - '0', BIN); which works but doesn't convert it to 8 bit.

This is what I get

1 10 11 100 101 110 111 1000

You've got 8 bit, the serial monitor is cutting off the leading zeroes.

Exactly, just round out the number when sending or parsing - (not on a compiler so you may need to tweak this, mybitstring is an example just make a function that takes a string argument. )

string mybitstring = 110;
while(bitstring.length < 8)
{
mybitstring = 0+mybitstring;
}
return mybitstring;

There are more efficient ways to accomplish this btw, this is just an example

Thanks for the reply's :) its helped a bit, ill try and play around with it and see if I can get it all to work lol, at least now I understand where I'm going wrong :)

Thanks for all you help :D

Hi just thought I would let you know, I got it all working now, thanks for your help I added an attachment if it shows to show it lol

Thanks :smiley: