我是arduino的新手,我用加速度计来测量移动火箭上的加速度。我正在使用HC12无线发送器来做这件事。
我想要做的是,只有当z轴加速度发生一定变化时,才开始通过HC12发射机将数据发送到计算机。
我不知道如何处理我所做的将x、y、z和数据的值传递到函数Hc12中的函数。
这个Hc12函数仅意味着每30秒运行一次,而循环函数应该每秒运行3次。
任何帮助都将不胜感激。我的代码如下:
#include <SoftwareSerial.h>
SoftwareSerial HC12(7,6);
//initialising variables and functions
const int numReadings = 15;
int readings[numReadings];
int index = 0;
float total = 0;
int inputPin = A1;
float test;
const int numReadings1 = 15;
int readings1[numReadings1];
int index1 = 0;
float total1 = 0;
int inputPin1 = A2;
const int numReadings2 = 15;
int readings2[numReadings2];
int index2 = 0;
float total2 = 0;
int inputPin2 = A3;
float x;
float y;
float z;
void setup(){
Serial.begin(115200);
if ((readings2[index2]-readings2[index2-1])>0.5){
HC12.begin(115200);
}
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
float Hc12(float x, float y, float z, float data)
{
float test;
readings2[index2] =analogRead(inputPin2);
test =readings2[index2]-readings2[index2-1];
if ((test)>0.1){
HC12.println(data);
}//send data
return test;
delay(30000);
}
void loop() {
//x moving avg
total = total-readings[index];
readings[index] =analogRead(inputPin);
total = total +readings[index];
index = index + 1;
if (index>=numReadings)
index = 0;
x = total/numReadings;
//y moving avg
total1 = total1-readings1[index1];
readings1[index1] =analogRead(inputPin1);
total1 = total1 +readings1[index1];
index1 = index1 + 1;
if (index1>=numReadings1)
index1 = 0;
y = total1/numReadings1;
//z moving avg
total2 = total2-readings2[index2];
readings2[index2] =analogRead(inputPin2);
total2 = total2 +readings2[index2];
index2 = index2 + 1;
if (index2>=numReadings2)
index2 = 0;
z = total2/numReadings2;
//z moving avg
total2 = total2-readings2[index2];
readings2[index2] =analogRead(inputPin2);
total2 = total2 +readings2[index2];
index2 = index2 + 1;
if (index2>=numReadings2)
index2 = 0;
z = total2/numReadings2;
String data = "";
data = data + x + "," + y + "," + z ;
delay(30);
}发布于 2022-03-29 08:22:29
您没有调用下面的函数。还是我漏掉了一些要点?
float Hc12(float x, float y, float z, float data)顺便说一下,我没有检查剩下的代码是否有其他错误。
https://stackoverflow.com/questions/71655068
复制相似问题