Using Serial.read()

I'm starting my first personal Arduino project. Though, I'm sorta stuck.. I'm trying to move Serial input into an array, but I can't figure out how. Here's what I have so far:

int incomingByte = 0

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

void loop() {
  if(Serial.available() > 0) {
    incomingByte = Serial.read();
  }
}

Now, say I want to see my input in hexadecimal; I can change the 'loop' a bit.

void loop() {
  if(Serial.available() > 0) {
    incomingByte = Serial.read();

    Serial.println(incomingByte, HEX);
  }
}

The output of "Hello" (minus quotes) as an input:

48
65
6C
6C
6F

So, to me it seems like 'incomingByte' is already some kind of an array. But say I change 'Serial.println(incomingByte, HEX);' to:

Serial.println(incomingByte[1]);

I get this error:

invalid types 'int[int]' for array subscript

Can someone help me understand this?

Do a search for Serial posted by PaulS.

He has by far the best serial read code you will find on here and he posts it all the time. In fact, he will likely post it here in the morning.

Basically you are trying to read your bytes into an integer and you only have one.

So declare you incomingbyte as a char array like so:

char incomingbyte[20];      //Make sure this number is big enough to handle all the incoming data
int bufferindex = 0;


void loop()
{
while(Serial.available() > 0)
{
 Serial.read(incomingbyte[bufferindex];
 bufferindex ++;

}


}

That will populate your buffer with your incoming data but it is by no means complete cause now you have to do something with it. Also, you should include a start and end character of some sort so you know when to start reading data and when to stop.

Loop up Paul's code, it will solve your problem.

@Sacman

When I try to compile now I get this error:

no matching function for call to 'HardwareSerial::read(char&)'

That's because this:

 Serial.read(incomingbyte[bufferindex];

Should really have been:

 incomingbyte[bufferindex]=Serial.read();

Oops.