FOrming a line in serial.print

So, i have an array of boolean, and i have to form a line looking like (01101001) and send it all at one time through serial port with println. Then repeat. So output should look like

00111010
01110100
10101010
01100010

and so on. But they have to be sent as one line, not one sumbol at a time and then go to other line

If each of those 1s and 0s are a character then they will be sent one at a time. If they are the bits of a byte then you can send them all at once as a byte.

Which is it ?

Here is the code:

const int inps = 2; //number of inputs

int pins[inps] = {A0, A1};//this array stores input values from piezos
int curlev = 0;//current level of processed piezo
int trs = 10;//threshold
boolean lstate[inps];/* This array stores data whether input
                     was higher than threshold or not
                     */

void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
Serial.begin(9600);

//fill output aray with zeros
for (int i=0; i <= inps-1; i++)
    {
      lstate[i] = 0;
    }
    
}

void loop() {
//for every input, read its state and compare it to threshold
//then store the result in lstate
for (int i=0; i <= inps-1; i++)
   {
    curlev = digitalRead(pins[i]);
    if (curlev > trs)
    {
     lstate[i] = 1;
    }
    else
    {
     lstate[i] = 0;
    }
   }
   //HERE println lstate array, at one line at the same time
   //then start all over again
}

Do you know the difference between print and println?

print just sends something to serial monitor and println goes to a new line after doing that

So, what's the problem?

GrooveFlotilla:
So, what's the problem?

I have an array, an i have to print it on one line at the same time. Not element by element and then go to another line. i have to convert array into a line of 1's and 0's and then print the whole line

You know how to print something without a newline, and you know how to print a newline.
I don't see your problem.

Running a cycle which prints an element of array each time, and in the end moves to another line is not what i want. I want to take an array, convert it to a line of 1's and 0's, or maybe turn it into decimal number, or somehow else compact it, then send it

Why does it matter if you print "10100101" or '1' '0' '1' '0' '0' '1' '0' '1'?

I'm trying not to be a smart arse*, but I really don't see the difference. Serial is slow, and the receiver won't know the difference.

  • Probably failing, though

GrooveFlotilla:
Why does it matter if you print "10100101" or '1' '0' '1' '0' '0' '1' '0' '1'?

It will be recieved by a program that reads that as states of 16 inputs. So it will recieve 16 1's or 0's, and then know whether corresponding input was enabled, and play associated sound, it will be a drum machine. Kind of like a MIDI, but with serial. Also, i tried MIDI through serial program, but i though that this will suit me better.

OK, I give up.

So it will recieve 16 1's or 0's

Not if you're printing like that where you see 0's and 1's on the screen. If that's the case then the receiver would be getting bytes equal to 48 or 49. If you want to combine your booleans into one byte then that's something someone can help you with. If you just want to make it print to a screen in one line the that's ridiculous becaus ebehind the scenes it will be broken up and sent one at a time anyway.

Why don't you explain what you want to do instead of how you (erroneously) think you should go about doing it.

Topsoil:
I have an array, an i have to print it on one line at the same time. Not element by element and then go to another line. i have to convert array into a line of 1's and 0's and then print the whole line

Serial communication of the arduino sends exactly one bit at the time. It cannot send more than one bit at the same time.

The fastest way to send 16 bits over serial is:

  1. compact them into 2 bytes
  2. use the highest reliable baudrate.
  3. use hardware serial

If and only if there allways changes only 1 bit of the 16 bits between two sends, you can send the index of the changed bit instead. That could be done in theory with 4 bits Serial communication. You need to rewrite hardware serial but it can be done.

Topsoil:
I have an array, an i have to print it on one line at the same time. Not element by element and then go to another line. i have to convert array into a line of 1's and 0's and then print the whole line

Serial always sends one character at a time (and that char is sent one bit at a time - hence "serial")

In fact, since your code is almost certainly running faster than the serial can output it, the behavior of Serial.println("01001100") is probably indistinguishable to the receiver from:

Serial.print('0');
Serial.print('1');
...
Serial.println('0');

since in the second case, before the first character has finished sending, the next Serial.print will have been reached to stick the second character into the buffer, so there will be no pause between the characters.

What you need to do is the have the receiving software wait until some sort of separator character (like a linebreak), and use that to determine the start and end of each set of values.

That said, you can totally create a char array ( c string - as opposed to String) - make it 1 longer than the total number of characters you're sending, and set each charachter in that array to the value you want, then send it all at once with println.

You can even build a String (the string class) - String is not recommended due to how it dynamically allocates memory, but you'd likely be able to get away with it in a simple case like this.

Either of those last two solutions have the same problem as above though - the characters still come through one at a time, so you need to have your receiving software look for a marker character (like the newlines) to know when each set of characters starts and ends

Seems OP is MIA

Hi,
I think this code will help demonstrate using serial print.

int val = B10001000;
int loop1 = 0;
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (loop1 == 0 )
  {
    Serial.println ("Serial.print examples \n");
    Serial.println ("Decimal");
    Serial.println (val, DEC);
    Serial.println ("Hexadecimal");
    Serial.println (val, HEX);
    Serial.println ("Octal");
    Serial.println (val, OCT);
    Serial.println ("Binary");
    Serial.println (val, BIN);
    loop1 = 2;
  }

}

Tom... :slight_smile:

Hmm. Or i can just have it count up to 16 and then reset. Ok, thanks!

Topsoil:
Hmm. Or i can just have it count up to 16 and then reset. Ok, thanks!

I am not sure what you mean by your comment or whether you want/need to receive a series of chars, each with a '0' or '1' in them representing the state of the booleans or whether you want to receive a series of bytes where each bit of the byte represents the state of the booleans in a line.

Come to think of it, how are the original booleans held in the array. Is it a 1 dimensional or 2 dimensional array ? Do you appreciate that an array of booleans uses one byte for each value and that the boolean vales are not packed into bytes, one bit per boolean.

How about posting an example of how the array is populated.

Hi,
Did you look at and run my code?

Tom... :slight_smile: