Reading multiple pots

PaulS was a great help in giving guidance writing the code to send 1 pot value using xbee. Now, I need to send 3 pots over Xbee and receive them and be able to determine which pot was changed using if statements and run separate code for each.

int potPin0 = 0; int potPin1=1; int potPin2=2;// select input pin 
int val0 = 0; int val1 = 0; int val2 = 0;// variable to store the value

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

void loop()
{
  val0 = analogRead(potPin0);
  Serial.println(val0);
  val1=analogRead(potPin1);
  Serial.println(val1);
  val2=analogRead(val2);
  Serial.println(val2);
  delay(1);
}

receiving arduino will receive 3 pots but will also have 1 pot that is sent directly to it that I use in the first if statement.

//#include "RoboClaw.h"
//#include "BMSerial.h"

//RECEIVE VALUES FROM ARDUINO1
char string[8];
int var;
int index;
boolean started=false;
boolean ended=false;

void setup()
{
  Serial.begin(9600);
}
//pin0 will be steering wheel
//pin1 will be brake
//pin2 will be gas
void loop()
{
  
   while(Serial.available() >= 0) //since 0-1023
    {
      var=Serial.read(); //read the character
      if(var=='<') //not sure what to put in if statement to run until end
      {
        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
      }
    }
    
    if(started && ended)
    {
      //convert portion of string to integer representation
      int val=atoi(string); 
      Serial.print("<");
      Serial.print(val);
      Serial.print(">");
      //*****Need to assign a variable to this value
      
      //next time
      started = false;
      ended = false;
      
      index = 0;
      string[index]='\0';
    }
    
    if(.....) //if steering wheel changed, do this using value above
    {   
        double steeringInput = Serial.read(); //Need to use the value calculated above
        steeringInput = constrain(steeringInput, steeringpotMin, steeringpotMax);
        double targetPos = map(steeringInput, steeringpotMin, steeringpotMax, -100, 100);
        int potPin3 = 3; //use pin3 on arduino2 for 'current' pot
        double wheelInput = analogRead(potPin3);
        wheelInput = constrain(wheelInput, wheelpotMin, wheelpotMax);
        double currentPos = map(wheelInput, wheelpotMin, wheelPotMax, -100,100);
        int error = targetPos - currentPos;
        
        if(error > 10)
        {
          int Turn = Kp*error;
          Turn = constrain(Turn,-63,63);
          Turn = Turn+64;
          //send to roboclaw, value between 1 and 127
          //roboclaw datasheet: 1 = fullreverse, 127=fullforward, 64=stop
        }
    }
    
    else if(.....) //if pot1 changed, do this using value above
    {
      
    }
  
    else if(......) //if pot2 changed, do this using value above
    {
      
    }
  
}

First question, how to I assign a variable to the value that was received using this code, is it just val?

      int val=atoi(string); 
      Serial.print("<");
      Serial.print(val);
      Serial.print(">");
      //*****Need to assign a variable to this value

Second question, how do I use the if statement to determine if this pot was changed do this, if this pot was changed do this? Since it is being sent over xbee I am not sure how I determine which pot was changed, but depending on which one was changed I need to run separate code to determine things using the value the receiving arduino has.

Last question, in the first if statement line double steeringInput = Serial.read(); I am sure this is incorrect as since this pot is being sent with xbee that .read will be the character. I need to assign this to the value that is related to my first question above.

Look for "Example state-machine code"

Seems a little too advanced for me. This is really my first arduino project. PaulS guided me through the first piece of code and the second piece(without the if statements). All I have added is the extra pots in the first piece of code and the if statements and had a few questions dealing with what I am trying to accomplish. Want to know if I am it doing it correctly, and if so there is probably a better way but probably would require for knowledge of the arduino and coding than I have.

If your sender sends <3, val1, val2, val3> or <1, val>, then you can use strtok to extract the tokens, and test whether the first token is 1 or 3, then use strtok to extract that number of tokens from the rest of the string.

well, if I do something like this:

int potPin0 = 0; int potPin1=1; int potPin2=2;// select input pin 
int val0 = 0; int val1; int val2;// variable to store the value
int lastVal0 = 0; int lastVal1=0; int lastVal2=0;
//pin0 will be steering wheel
//pin1 will be brake
//pin2 will be gas

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

void loop()
{
  //So depending on which pot is turned then it will run this code one pot at a time, <val>
  //if two pots are turned almost simultaneously then whichever pot is turned first it runs, then runs second pot immediately after
  val0 = analogRead(potPin0);
  if(abs(val0-lastVal0) >10)
  {
    Serial.print("<");
    Serial.print(val0); 
    Serial.println(">");
    lastVal0=val0;
  }
  
  val1=analogRead(potPin1);
  if(abs(val1-lastVal1) > 10)
  {
    Serial.print("<");
    Serial.print(val1); 
    Serial.println(">");
    lastVal1=val1;
  }

    val2=analogRead(val2);
    if(abs(val2-lastVal2) > 10)
  {
    Serial.print("<");
    Serial.print(val2); 
    Serial.println(">");
    lastVal2=val2;
  }
   
}

I would assume it would send as , so only one token at a time. What my question is, since it is not sending <val1,val2,val3> how will I determine which val was sent for which pot if I only send ? I have never used strtok but have read about it since you mentioned it and seems I will need to apply it.

Would this work or not even close?

Did you even try compiling it?

 if(Serial.available == 'A') //if steering wheel changed, do this using value above

Serial.available is the address of a function. It is highly unlikely that it will equal 'A'.

The code you had for reading data expects all the data for a packet to be between the < and the >. Sending data between the > and the < simply guarantees that it will be ignored.

could be sent. Then, when started and ended are true:

char ltr = ' ';
if(strlen(inData) > 0)
{
   ltr = inData[0];
   inData[0] = ' ';

   val = atoi(inData);
}

Then, ltr will be 'A', 'B', or 'C', and val will be the numeric value sent.

I don't think the } line up correctly/enclose the right blocks on the receiver, but, otherwise, it looks okay.

Got that fixed now, so seems this part of the project is complete. Do you mind checking out my other thread again...I got the values to display correctly just seems like a smaller error in printing the value of the pots. Thanks for the help again.