DeepZ:
Couple of remarks, hope they can help.
First, the line ' //}' seems weird, with the comment marks it does NOT compile. Taking out the '//' compiles ok.
Next, does it actually print the "length" string? If x is not inside your range, nothing is done, whatever you receive.
Then, the line 'float distance = analogRead(1);', again hmm, analogRead returns a int, not a float. Not critical, but not clean.
But, most critical, you are not looping yourself, but using the Arduino background code to repeatedly call loop(). This means you are not in full control over what the system is doing. Even in the Arduino the microcontroller 'rule' the main function never exits is still a good one to follow.
Now, i am not sure, but it looks like the serial buffer is reset upon entering loop().
If i place the main block in a while(true) { .. } loop, it seems to work; at least it sees the incoming data etc.
Oh, and finally, the lines
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
make no sense, inComingByte is initialise to 0 and never set.
HTH,
Guido
Yes, you are totally right, I just noticed the weird "//}" as well when I went to check if it compiles. It does print the length string. X shouldn't be in range-- all the values outside of that are nonsense (it's an IR sensor), so I just filter them out.
Ah...I see what you are saying, really I could just do int x = analogRead(IRpin);
right? that is a bit silly now that I look at it...I think I was assuming it comes in a float as default, but its probably bytes...? I don't know much about this language...I come from JS background, so this is really foreign to me. I was following some code posted up on stack exchange, but apparently with no luck and I should have caught that silly error too, I didn't notice it was being reset. I usually use split to split this type of data, but I have no idea how to do it in C style
this part confused me the most because I am still terrible at using bytes. Not quite sure how to match "Data" in bytes, or even what best practice is. I'm ok with using bytes if this is fast and standard.
if(inbyte >= '0' && inbyte <= '9')
the serial coming back into arduino looks something like this: Data,0,20,
where second data set "0" would be between 0 and 180 ( I map this in processing)