Arduino Accelerometer Usb

int x=0;
char xx='0';
String datax,datay,dataz;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // establish connection
datax="";
datay="";
dataz="";
xx='0';
}
void loop() {

// put your main code here, to run repeatedly:
if(Serial.available()){
x=Serial.read();
if(x!='x' && xx=='0'){
datax+=(char)x;
}
else if (x=='x' && xx=='0'){
float xvalues;
xvalues=datax.toFloat(); // Do something with "xvalues"
// use "xvalues" for accelrometer x-axis

datax="";
xx='1';
}
else if (xx=='1' && x!='y'){
datay+=(char)x;

}
else if (x=='y' && xx=='1'){
float yvalues;
yvalues=datay.toFloat(); // Do something with "yvalues"
//use "yvalues" for accelrometer y-axis
datay="";
xx='2';
}
else if (x!='z' && xx=='2'){
dataz+=(char)x;
}
else if (x=='z' && xx=='2'){
float zvalues;
zvalues=dataz.toFloat(); // Do something with "zvalue"
// use "zvalues" for accelerometer y-axis
dataz="";
xx='0';
}
}
}

The Example Project :

Example Code :

int x=0;
char xx='0';
String datax,datay,dataz;
float xvalues,yvalues,zvalues;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);      // establish connection
datax="";
datay="";
dataz="";
xx='0';
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
xvalues=yvalues=zvalues=0;
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
}
void loop() {
  
  // put your main code here, to run repeatedly:
if(Serial.available()){
  x=Serial.read();
  if(x!='x' && xx=='0'){
  datax+=(char)x;
}
 else if (x=='x' && xx=='0'){
   float xvalues;
 xvalues=datax.toFloat();   // Do something with "xvalues" 
 // use "xvalues" for accelrometer x-axis
if(xvalues>0){
   digitalWrite(13,HIGH);
 }else digitalWrite(13,LOW);


   datax="";
   xx='1';
 }
 else if (xx=='1' && x!='y'){
   datay+=(char)x;
   
 }
 else if (x=='y' && xx=='1'){
  yvalues=datay.toFloat();    // Do something with "yvalues"
  //use "yvalues" for accelrometer y-axis
  if (yvalues>0){
   digitalWrite(12,HIGH);
 }else digitalWrite(12,LOW);

   datay="";
  xx='2';
 }
 else if (x!='z' && xx=='2'){
   dataz+=(char)x;
 }
 else if (x=='z' && xx=='2'){
  zvalues=dataz.toFloat();   // Do something with "zvalue"
  // use "zvalues" for accelerometer y-axis
 if (zvalues>0){
  digitalWrite(11,HIGH); 
 }else digitalWrite(11,LOW);
    dataz="";
  xx='0';
 }
}
}
 

You May Also Like

About the Author: quzal

Leave a Reply

Your email address will not be published. Required fields are marked *