这是连续采集传感器数据的代码,我想采集某个时间段的数据应该怎么写啊?就是判断那一时间段的数据,然后执行其他事件,不影响传感器数据的连续采集。
unsigned char Re_buf[11],counter=0;
unsigned char sign=0;
int r=1;
float A,a[3],w[3],angle[3],T;
void setup() {
// initialize serial:
Serial.begin(115200);
}
void loop() {
serialEvent();
if(sign)
{ //Serial.println(r);
sign=0;
if(Re_buf[0]==0x55) //检查帧头
{
switch(Re_buf [1])
{
case 0x51:
a[0] = (short(Re_buf [3]<<8| Re_buf [2]))/32768.0*16;
a[1] = (short(Re_buf [5]<<8| Re_buf [4]))/32768.0*16;
a[2] = (short(Re_buf [7]<<8| Re_buf [6]))/32768.0*16;
T = (short(Re_buf [9]<<8| Re_buf [8]))/340.0+36.25;
break;
case 0x52:
w[0] = (short(Re_buf [3]<<8| Re_buf [2]))/32768.0*2000;
w[1] = (short(Re_buf [5]<<8| Re_buf [4]))/32768.0*2000;
w[2] = (short(Re_buf [7]<<8| Re_buf [6]))/32768.0*2000;
T = (short(Re_buf [9]<<8| Re_buf [8]))/340.0+36.25;
break;
case 0x53:
angle[0] = (short(Re_buf [3]<<8| Re_buf [2]))/32768.0*180;
angle[1] = (short(Re_buf [5]<<8| Re_buf [4]))/32768.0*180;
angle[2] = (short(Re_buf [7]<<8| Re_buf [6]))/32768.0*180;
T = (short(Re_buf [9]<<8| Re_buf [8]))/340.0+36.25;
Serial.print("a:");
A=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
Serial.print(A);Serial.print(" ");
Serial.print("angle:");
Serial.print(angle[0]);Serial.print(" ");
Serial.print(angle[1]);Serial.print(" ");
Serial.println(angle[2]);
break;
delay(5);
}
}
}
}
void serialEvent() {
while (Serial.available()) {
Re_buf[counter]=(unsigned char)Serial.read();
if(counter==0&&Re_buf[0]!=0x55) return;
counter++;
if(counter==11)
{
counter=0;
sign=1;
}
}
}
|