buffer in serial comm

Hello :slight_smile:

I’m sending data from my Arduino to a program on Borland Builder C++ and graphing it. But there are some problems, and I want to send it on a buffer on Arduino.
There is something wrong with my code and I’m not being able to find where the problem is.
The code is below… As you can see, I create a vector called buffer, indicated as the analog read, and once is fulled (with 100 bytes), it’s printed on screen. But all I can get it is the same numbers, always. No matter what I do with my potentiometer (which by the way is where I’m getting my data of) the numbers never change.

void setup() {
Serial.begin(9600);
}
void loop() {
int buf[100], i;

for (i=0; i<100; i++) {
buf*=analogRead(A0);
}
_ Serial.print(buf
);
delay(1000);
}[/glow]*
Thanks :slight_smile:_

you're only printing one value, and that's invalid! Don't use A0, use 0

You are printing buffer [100], which doesn’t have a valid value in it, and is out of bounds anyway.
A0 is only used when you’re using analogue pins as digital pins, so it is an incorrect usage.

Better indentation will make things clearer.
Try:

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

void loop() {
 int buf[100], 
      i;
   
 for (i=0; i<100; i++) {
     buf[i]=analogRead(0);
 } 
 Serial.print(buf[0]);
 delay(1000); 
}

but if I put buf[0] won't arduino print only the first number?

But you were only printing one value before, and you hadn't even put a value into it! (and even if you had, it wouldn't have been from the correct analogue pin!)

[edit]If you want all the values, you need another loop.[/edit]

Yes, but I want to print the whole buffer :(

I edited my last post whilst you were posting.

Hmm got it! I'll try it here. Thanks!! :)

I’ve used another loop and it works!! :smiley:

But I was using the Serial.print function, and now I’m trying to use the Serial.write one, and it is not working anymore.
As I saw in the Arduino reference page, I use this command:
Serial.write(buf, len)

So my code stay like this:

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

void loop() {
  int buffer[100], i, j;
i=0;
j=0;
buffer[100]=0;

  for (i=0; i<100; i++) {
      buffer[i]=analogRead(0);
  } 

  Serial.write(buffer,100);
         delay(1000);  
}

Does anybody know why it didn’t work?

Did you read the documentation for the ‘write’ method?

Yes, I did.

Description

"Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead."

Syntax

Serial.write(buf, len)

Parameters

buf: an array to send as a series of bytes len: the length of the buffer

The key point is, I think, in the second word of the description - 'binary'.

Another thing is 'buffer [100]' doesn't exist.

Hm I thought the data could be of any kind, and arduino would convert it to binary to write it... But if I have to do it myself, that's ok.

Thanks :)

It already is binary. The Arduino can't convert something that is already binary into binary.

What do you mean by "buffer[100]" doesn't exist?

And if it's already in binary, what's the problem?

You have declared a 100 element array, so the only valid subscripts are 0 to 99.

Yes, but I want only 100 elements...

So why are you writing a zero to the 101st element?

Am I? I only realize it now... I thought I was clearing the buffer hahah :(

As I couldn't use the command Serial.write(buf,len), I've made a loop to write on the serial comm, and I'm seeing my data on ASCII, is this suppose to happen?

What I want to do is quite simple (but as you can see, I’m not getting much sucess).

I have a trimpot connected at the analogic 0, and I want to make a buffer with 100 data and send it on the serial comm.