pot value

Is this line not in the correct place?

Yes, it is in the right place. But, if you are debugging code that stores and parses what was read, the first thing you want to do is to display what was read. You are not doing that. Therefore, you are assuming that the reader code is perfect, which may not be a valid assumption.

well, I am getting or something similar...also will get greek letters if I change to read or available:

<

<




2
7
2




>
<
27
>

Using this code, without availabable and read commented, I get available '0' and read is '-1'.

int potPin = 0; // select input pin 
int val; // variable to store the value
int lastVal = 0;

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

void loop() 
{
  val = analogRead(potPin); // read the value from the pot
  if(abs(val - lastVal) > 5)
  {
    Serial.println("<");
    Serial.println(val); 
    Serial.println(">");
    lastVal=val;
  }
}

receiver

//read in value from first arduino of slide pot
//#include "RoboClaw.h"
//#include "BMSerial.h"
 
//read till end of packet
char string[4]; //can be 4, 8 is fine
int var;
int index = 0;
boolean started=false;
boolean ended=false;


void setup()
{
  Serial.begin(9600);
    //int v=Serial.available();
    //Serial.println(v);
      //var=Serial.read();
      //Serial.println(var);
}

void loop()
{

    while(Serial.available() > 0)
    {
      var=Serial.read();
      if(var=='<') 
      {
        started = true;
        index=0;
        string[index]='\0';
      }
      else if(var=='>')
      {
        ended = true;
        break; //break out of while loop when '>' received
      }
      
      else if(started)
      {
        string[index]=var;
        index++;
        string[index]='\0';
        string[index]=var; //store character
      }
    //x = Serial.read(); //read another string
    }
    
    if(started && ended)
    {
      //convert portion of string to integer representation
      int val=atoi(string); 
      Serial.println("<");
      Serial.println(val);
      Serial.println(">");
      
      //next time
      started = false;
      ended = false;
      
      index = 0;
      string[index]='\0';
    }
  }

Testing the sender code and I seems to be correct as I get the correct output.

475
>
<
487
>
<
499
>
<
510
>
<
521

So, can I now suggest that you do two things? First, learn the difference between Serial.println() and Serial.print(). It is not always necessary to use Serial.println().

Second, please do not print anonymous data. When a number appears on the serial monitor, you have no idea which Serial.pint(ln) statement caused it to appear.

Serial.print("I read a [");
Serial.print(var);
Serial.println("]");

should show

I read a [<]
I read a [4]
I read a [7]
I read a [5]
I read a [>]

on the serial monitor when you send <475>, and it is very obvious what is being printed.

<
4
7
5

conveys no where near as much information.

well, I have that figured out. Now for some reason, val is 4 characters printing out always.

<1266>
<1200>
<1144>
<1200>
<1144>
<1233>
<1177>

Seems I got the print statements correct now.

You are printing ltr and val even when started and ended are not true.

Delete code not being used. Don't just comment it out.

Use the Tools + Auto Format... menu item to properly indent your code.

Read the code; try to understand what it is doing. Don't just stick new stuff in anywhere.

PaulS:
You are printing ltr and val even when started and ended are not true.

Read the code; try to understand what it is doing. Don't just stick new stuff in anywhere.

Right. I had mentioned that in previous post but i edited it in there. I put the if statements inside the if(started && ended). It seems to work now but it executed for about 5 seconds pretty well but decided to not read anything from the pot after that. Not sure what happened. Here is a sample and then it decided to not print anything.

This is letter: A
<402>
This is letter: A
<408>
This is letter: A
<414>
This is letter: A
<420>
This is letter: A
<426>
This is letter: A
<432>
  if(started && ended)
  {
    ltr = ' ';
    if(strlen(string) > 0)
    {
      ltr = string[0];
      string[0] = ' ';
      //val=atoi(string); 
    }
    //convert portion of string to integer representation
    val=atoi(string);

    if(ltr == 'A')
    {
      Serial.print("This is letter: ");
      Serial.println(ltr);
      Serial.print("<");
      Serial.print(val);
      Serial.println(">");
    }

    else if(ltr == 'B')
    {
      Serial.print("This is letter: ");
      Serial.println(ltr);
    }


    //next time
    started = false;
    ended = false;

    index = 0;
    string[index]='\0';
  }
char string[4]; //can be 4, 8 is fine

You are now sending up to 5 characters plus there needs to be room for the NULL.

        string[index]=var;
        index++;
        string[index]='\0';
        string[index]=var; //store character

You are stomping on the NULL. You have already stored the character.

    val=atoi(string);

Because you are stomping on the NULL, the array string is NOT a string, so it is not valid input to atoi().

Not sure about this. So if I do not stomp on the NULL this still applies about the atoi()? I don't need it?

You still need atoi(). But, you need to stop stomping on the NULL, so that string really is a string, so that it is valid input to atoi().

Ok, got it. Well it seems to work almost perfectly now. I twisted the pot for about 10-15 seconds and it recorded all the data near perfectly. Although it displayed some weird values two or three times....is this expected?

This is letter: A
<527>
This is letter: A
<0>
This is letter: A
<555>
This is letter: A
<0>
This is letter: A
<557>
This is letter: A
<0>
This is letter: A
<546>
This is letter: A
This is letter: A
<222>
This is letter: A
<228>
This is letter: A
<234>
This is letter: A
<-21897>
This is letter: A
<5>
This is letter: A
<277>
This is letter: A
<283>
This is letter: A

I don't have the wires soldered yet so this might be a reason of improper values as the connection?

You are still not printing the incoming data, to assure that you are receiving what you think you are. Without that, we're just whistling in the dark. It makes sense to try to debug the storing/parsing code only after you know that what is being received matches what is being sent.

Remind me which XBees you have, and how they are configured. Baud rate? What things did you configure?

So, I am needing to print the data that is being sent from the arduino1 also?

Not once it's working. Until then, how else can you debug the problem?

I figured I needed to be plugged in with the usb to the arduino to read off serial and I was doing that with arduino2.

You need to have the USB cable plugged in to write to the Serial Monitor, too. The sender to receiver communication is via the XBees. The Arduino to PC communication is independent of the Arduino to Arduino communication.

Well, I have been just keeping the usb to arduino2 plugged in after I transfer the file. I do not send anything in the serial monitor. I just open the arduino2 serial monitor and test the pots display using This is letter: A and This is letter: B to make sure they are working and it records which pot is being executed which it works 90% of the time and then you get those weird values I showed above and then the big jump from 800 to 8 sometimes.

So now I need to open both serial monitors and check to see the problem and see why arduino2 might not be displaying the correct values as arduino1 sometimes correct?