Hi,
I'm trying to control a robot with a remote that uses a joystick and a gyro scope. I'm having a hard time getting readable values. I took the idea from the arduino cookbook where they send the pin values with a comma to separate them and then use serialparseint to separate them but my values are not coming out on the serial monitor. I am just wondering if there is a simple way to send multiple values at once. I am doing this from my phone but can post my code if needs be. Thank you.
Sender (Using Arduino Uno and an Xbee Shield)
#include <SPI.h> // Included for SFE_LSM9DS0 library
#include <Wire.h>
//#include <SoftwareSerial.h>
#include <SFE_LSM9DS0.h>
#define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW
#define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW
LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);
#define PRINT_CALCULATED
#define PRINT_SPEED 50
const int VERT = 0; // analog
const int HORIZ = 1; // analog
const int SEL = 7; // digital
int pwr = 13;//power led
float x;//x gyroscope
float y;//y gyroscope
float z;//z gyroscope
float pitch, roll;//calculate the pitch and the roll
int p, r;//convert the pitch and roll to integers
//SoftwareSerial Xbee(2,3);
void setup() {
//Xbee.begin(9600);
Serial.begin(9600);
uint16_t status = dof.begin();
pinMode(SEL,INPUT);
pinMode(pwr,OUTPUT);
digitalWrite(SEL,HIGH);
}
void loop() {
digitalWrite(pwr,HIGH);
int vertical, horizontal, select;
dof.readAccel();
//Take accelerometor readings
x = dof.calcAccel(dof.ax);
y = dof.calcAccel(dof.ay);
z = dof.calcAccel(dof.az);
//Calculate the pitch and the roll
pitch = atan2(x, sqrt(y * y) + (z * z));
roll = atan2(y, sqrt(x * x) + (z * z));
pitch *= 180.0 / PI;
roll *= 180.0 / PI;
//Map the pitch and the roll
pitch = map(pitch,-90,90,0,180);//Makes it so I am not sending a negative number
roll = map(roll,-90,90,0,180);//Ditto
p = round(pitch);//turns the float into an integer
r= round(roll);//turns the float into an integer
vertical = analogRead(VERT); // will be 0-1023
horizontal = analogRead(HORIZ); // will be 0-1023
select = digitalRead(SEL); // will be HIGH (1) if not pressed, and LOW (0) if pressed
vertical = map(vertical,0,1023,0,255);//Makes it so the data can be sent by ASCII
horizontal = map(horizontal,0,1023,0,255);//Ditto
//Xbee.print(p);
//Xbee.print(',');
//Xbee.print(r);
//Xbee.print(',');
//Xbee.print(vertical);
//Xbee.print(',');
//Xbee.print(horizontal);
//Xbee.print(',');
//Xbee.print(select);
Serial.print(p);
Serial.print(',');
Serial.print(r);
Serial.print(',');
Serial.print(vertical);
Serial.print(',');
Serial.print(horizontal);
Serial.print(',');
Serial.print(select);
}
Reciever Code Arduino Mega
//#include <SoftwareSerial.h>
//SoftwareSerial xbee(2,3);
int value[5];//an array set up to bring in the values
int index = 0;
void setup() {
Serial.begin(9600);
//xbee.begin(9600);
}
void loop() {
while (Serial.available()>0){
//char c = xbee.read();
char c = Serial.read();//Read the data
Serial.println(c);//Print out data to show the xbees are communicating
//Serial.write(xbee.read());
//Serial.print(xbee.read());
for (index = 0; index < 5; index++){
value[index] = Serial.parseInt();//Skip the comma and take the next numeric value
}
for (int i = 0; i < index; i++)
{
Serial.print(value[i]);
Serial.print('\t');
}
Serial.println();
index = 0; //Reset the process
}
}