Very weird problem with analogRead();

I got this openFrameworks-Arduino app, I send a Handshake and depends on the byte sent my arduino responds with the analogRead(A0) or sends an PWM to pin 3… that sounds simple, doesn’t it?

the parameters are:
pvalue| (for example 50% PWM: p50|)
a| (arduino sends its analogRead on pin A0)

if I send a| multiple times (without previously sending any PWM value) it does what it’s supposed to.
BUT if I send a PWM value (psomething|) first and then asks for analogRead (a|) it sends always 1023 besides the real input.

I started to erase bits of code to see the root of the problem… and then I found this:

This is the code simplified:

char message[] = "";
int i=0;

int value=0;

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

void loop(){
  Serial.println(analogRead(A0));
  delay(30);
  if(Serial.available() > 0)
  {
    while(Serial.available() > 0)
    {
      char cRead = Serial.read();
      if(cRead != '|')
      {
        message[i]=cRead;
        i++;
      }
    }    
  }
}

if you open your serial monitor and send p1| (pwm 1% don’t forget your “|” character at the end) and you will see that the analog pin reads only 1023…

Why is this??? how ca I fix it??

Thank you!!

Daniel

p1| does nothing, send 100 in the Serial monitor, and you will see 1023, send p0| and it goes back to normal...

What is Serial.println(analogRead(A0)); supposed to accomplish?

[quote author=Runaway Pancake link=topic=170539.msg1268079#msg1268079 date=1370490110] What is Serial.println(analogRead(A0)); supposed to accomplish? [/quote]

Read the analog pin A0 and print the returned results, seems fine to me?

Lefty

This program is just to prove my point, my actual code is much larger, but I started to delete some lines to find the problem and I ended with that code that actually does nthing... but proves my point... The code looks fine, but if you run it and send something like "100" in the serial monitor you'll see what I mean.

You've declared a char array with one element.

char message[] = "";

There's a Null going there, so one element.

then you try to fill it with reads from serial. If you try to put in more than one character you're going to corrupt memory and then strange and undetermined things may happen.

Try declaring your char array to have enough chars to hold everything you're sending and that will probably help.

retrolefty: [quote author=Runaway Pancake link=topic=170539.msg1268079#msg1268079 date=1370490110] What is Serial.println(analogRead(A0)); supposed to accomplish?

Read the analog pin A0 and print the returned results, seems fine to me?

Lefty [/quote]

I haven't tried that before. I may be mistaken, but isn't it necessary to first place the analogRead result into a variable and then print that?

I may be mistaken, but isn't it necessary to first place the analogRead result into a variable and then print that?

No. You can serial.print an int and analogRead returns one. No need for an intermediate variable.

Of course, if you want to do anything with the analogue value other than display it, then you would be wise to read it into a variable first, otherwise you have to read it again which takes time, and by which time the value may have changed.