Need help with printing an array

Hi!
I am new to Arduino programming language.

I am trying to input values to an array which get printed simultaneously, and later, after all values of array have been inputted, the array is printed.
On running this, the arduino is not waiting for me to input data, except it just continously keeps on printing “Your array is 0”.

Any help would be appreciated
Here’s my code

byte incomingByte[7];   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        for(int i=0; i<7;i++)
        {
        while (Serial.available() ) {
                // read the incoming byte:
                incomingByte[i] = Serial.read();
        
       
              Serial.print(incomingByte[i]);
        }
        }

        for(int i=0;i<7;i++)
        {
          Serial.print("Your array is");
          Serial.print(incomingByte[i]);
        }
        Serial.println(" ");
      
}

What happens in your first for loop if Serial.available returns zero? (Which it most likely will)

By writing while(Serial.available()) , isn't it supposed to wait till anything becomes available?

No. Why do you think that?

I read it over here http://forum.arduino.cc/index.php?topic=108803.0 Clearly, I misinterpreted it. Need help.

mansimar01: By writing while(Serial.available()) , isn't it supposed to wait till anything becomes available?

That would be written like this:

while( ! Serial.available() ) {}

but that would tie up the processor.

mansimar01: Need help.

It just returns the number of characters that are available. There is no mystery to it.

aarg: It just returns the number of characters that are available. There is no mystery to it.

I understand. But what I want the processor to do is, to wait till I input the first byte, then print then first byte, then wait till I input the second byte, print the second byte and so on. After 7 inputs, I want the processor to print the whole array together.

The first time a while loop is initiated, the test is performed. If the test fails, the entire block is skipped. So the code block of a while loop may never run. That is what is happening to you while the program loops and waits for you to send a character.

aarg: The first time a while loop is initiated, the test is performed. If the test fails, the entire block is skipped. So the code block of a while loop may never run. That is what is happening to you while the program loops and waits for you to send a character.

I understand the problem now. But how do I make the processor wait till new values are inputted?

Make the processor wait until there is something available to read.

aarg: That would be written like this:

while( ! Serial.available() ) {}

but that would tie up the processor.

It worked.! Thanks

Spend some time with this an see if you can explain it.

byte incomingByte[7];   // for incoming serial data

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  static int i = 0;
  // send data only when you receive data:
  while (Serial.available() ) {
    // read the incoming byte:
//    incomingByte[i] = Serial.read();          // These two lines save and show ASCII digit characters
//    Serial.println( (char) incomingByte[i]);

    incomingByte[i] = Serial.read() - '0';      // These two line save and show numeric values
    Serial.println(incomingByte[i]);
    i++;
  }

  if (i == 7) {
    i = 0;  
    Serial.println("Your array is");
    for (int j = 0; j <7; j++)
    {
//      Serial.print((char) incomingByte[j]);   // This shows the ASCII codes as digit character
      Serial.print(incomingByte[j]);            // This shows the numeric value
      Serial.println(" ");
    }
  }
}

mansimar01:
Hi!
I am new to Arduino programming language.

I am trying to input values to an array which get printed simultaneously, and later, after all values of array have been inputted, the array is printed.
On running this, the arduino is not waiting for me to input data, except it just continously keeps on printing “Your array is 0”.

Any help would be appreciated
Here’s my code

byte incomingByte[7];   // for incoming serial data

void setup() {
       Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
       for(int i=0; i<7;i++)
       {
       while (Serial.available() ) {
               // read the incoming byte:
               incomingByte[i] = Serial.read();
       
     
             Serial.print(incomingByte[i]);
       }
       }

for(int i=0;i<7;i++)
       {
         Serial.print(“Your array is”);
         Serial.print(incomingByte[i]);
       }
       Serial.println(" ");
     
}

Study this and you wiill certainly get it working :wink: