I'm working on several different projects that use serial communication. So I created a little side project that incorporates all the elements and functions I want to use in my other projects as a way to learn and test all of my code/hardware. I have been using a section of "Arduino and Kinect Projects" by Enrique Ramos Melgar and Ciriaco Castro Diaz, titled "Writing your own communication protocol" (pg 67-69). Again, this project is for learning and testing purposes so it doesn't really do much.
Setup:
I have an Arduino UNO connected to three potentiometers (A0, A2, A4), it reads the values and divides by 4 to get a range between 0-255, then transmits the values via the TX pin to an Arduino Nano RX pin. The Nano reads the values, then passes them on via the Nano TX pin to an HC-06 Bluetooth module, the Bluetooth module then sends it to my computer where I have a simple Processing program that receives the values and displays them on my screen.
The UNO and Nano have been tested and are working.
The 3 pots have been tested and are sending values to the UNO.
The HC-06 has been tested with the Nano before and worked, but I am still pretty new to this little piece of hardware so maybe I'm missing something.
Please forgive the illustration, I know it's pretty rough.
Arduino UNO code
const int XIn = 0;
const int YIn = 2;
const int ZIn = 4;
int XVal, YVal, ZVal;
void setup(){
Serial.begin(9600);
pinMode(XIn, INPUT);
pinMode(YIn, INPUT);
pinMode(ZIn, INPUT);
}
void loop(){
XVal = int(analogRead(XIn)/4); //read pot value and divide by 4 to get a range between 0-255
YVal = int(analogRead(YIn)/4);
ZVal = int(analogRead(ZIn)/4);
Serial.write('A'); //send trigger character
Serial.write(XVal); //send first value
Serial.write(YVal); //send second value
Serial.write(ZVal); //send third value
}
Arduino Nano code
int StartVal, XVal, YVal, ZVal;
void setup(){
Serial.begin(9600);
}
void loop(){
if(Serial.available()<3){ //check if info available
StartVal = Serial.read();
if(StartVal=='A'){ // check for trigger character
XVal=Serial.read(); //read first value
YVal=Serial.read(); //read first value
ZVal=Serial.read(); //read first value
}
}
Serial.write('A'); //send trigger character
Serial.write(XVal); //send first value
Serial.write(YVal); //send second value
Serial.write(ZVal); //send third value
}
Processing code
import processing.serial.*;
Serial XYZ;
int StartVal, XVal, YVal, ZVal;
void setup(){
size(200,200);
XYZ = new Serial(this,"COM9",9600);
}
void draw(){
if(XYZ.available()>3){
StartVal=XYZ.read();
if(StartVal=='A'){ //check trigger character
XVal = XYZ.read(); //read first value
YVal = XYZ.read(); //read second value
ZVal = XYZ.read(); //read thrid value
}
}
print(XVal); //display values
print(" ");
print(YVal);
print(" ");
println(ZVal);
}
The Goal
Read three different pots, send the values from one Arduino to another, and on to a computer via Bluetooth.
Results
In the processing serial window it continuously displays "0 0 0". I used the serial monitor to check the UNO and it is reading the pot values correctly. So it seems the issue is with my "communication protocol". Maybe you can spot the problem. Specific answers and suggestions are great, resources that I can read or study are good too.
Thanks
-J

