#include <RFduinoBLE.h>
#include<Wire.h>
const int MPU_addr=0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
RFduinoBLE.advertisementData = "x";
RFduinoBLE.deviceName = "RFDUINO1";
RFduinoBLE.begin();
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
axis_X=Wire.read()<<8|Wire.read();
axis_Y=Wire.read()<<8|Wire.read();
axis_Z=Wire.read()<<8|Wire.read();
int xAng = map(axis_X,minVal,maxVal,-90,90);
int yAng = map(axis_Y,minVal,maxVal,-90,90);
int zAng = map(axis_Z,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
String stringVal= "";
stringVal+=String(int(x))+ "."+String(getDecimal(x)); //combining both whole and decimal part in string with a fullstop between them
Serial.print("stringVal: ");Serial.println(stringVal); //display string value
char charVal[stringVal.length()+1]; //initialise character array to store the values
stringVal.toCharArray(charVal,stringVal.length()+1);
RFduinoBLE.send(charVal,5);
Serial.print("Angle x = ");
Serial.print(x);
Serial.println((char)223);
/* Serial.print("Angle y = ");
Serial.print(y);
Serial.println((char)176); */
/* Serial.print("Angle z = ");
Serial.print(z);
Serial.println((char)176); */
delay(1000);
}
long getDecimal(float val)
{
int intPart = int(val);
long decPart = 1000*(val-intPart); //I am multiplying by 1000 assuming that the foat values will have a maximum of 3 decimal places.
//Change to match the number of decimal places you need
if(decPart>0)return(decPart); //return the decimal part of float number if it is available
else if(decPart<0)return((-1)*decPart); //if negative, multiply by -1
else if(decPart=0)return(00);
}
I need help. Serial monitor captures the data I want it to display. However, there is a reversed question mark at the back.
I want to display a "°" at the back. Can anyone help me ?