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.