本帖最后由 lll.l 于 2022-5-9 20:10 编辑
#include <SoftwareSerial.h> //包含软串口头文件,硬串口通信文件库系统自带
SoftwareSerial mySerial(A3,A2); //创建一个软串口的类,数字引脚 A2.A3 分别代表RX, TX引脚
float x_value;
float y_value;
const int x_pin = A5; //x-axis of the accelerometer
const int y_pin = A2; // y-axis
#define BEEP_PIN //定义蜂呜器引脚 D12
//const int z_pin = A2; // z-axis (only on 3-axis models)
void dida(u8 times, u8 frequency);
void handle_ADXL3350( )
{
int x = analogRead(x_pin);
delay(1);
int y = analogRead(y_pin);
delay(1);
//int z= analogRead(z_pin);
float zero_G =337.92; //ADC is o~1023 0g输出等于Vs/2,基准电压为5V时(3.3/5*1024 / 2)
float scale = 67.584; //ADXL335 330mv/g, 330/1000/5.0*1024
x_value = ((float)x - zero_G)/scale;
y_value = ((float)y - zero_G)/scale;
}
void handle_actoin(unsigned char action_num){
switch (action_num)
{
case 0: mySerial.println("#6GCI"); break; //右
case 1: mySerial.println("#5GCI"); break; //左
case 2: mySerial.println("#3GC1"); break; //前
case 3: mySerial.println("#4GCI"); break; //后
default:mySerial.println("error"); //如果要调用的动作这里没有,则向串口打印“error”
}
}
void handle_moveWood( );
void handle_actoin(unsigned char action_num);
void setup( )
{
//初始化串口通信:
pinMode(13, OUTPUT); //设置板上led13号IO口为输出
pinMode(x_pin, INPUT); //设置X轴所在IO为输入
pinMode(y_pin, INPUT); //设置Y轴所在IO为输入
//pinMode(z_pin, INPUT); //设置Z轴所在IO为输入
pinMode(BEEP_PIN, OUTPUT);
digitalWrite(BEEP_PIN, HIGH);
Serial.begin(115200);
mySerial.begin(115200); //设置软串口波特率
delay(1000); //延时,等待
}
void loop( )
{
handle_ADXL335( );
handle_moveWood( );
//Serial.println(x_value,2);
//delay(1000);
}
void dida(u8 times,u8 frequency)
{
for(byte i=0;i<times;i++)
{
digitalWeite(BEEP_PIN,LOW);
Delay(frequency);
digitalWeite(BEEP_PIN,HIGH);
Delay(frequency);
}
}
有颜色的这一行编译有问题,哪位大佬可以帮忙看看有什么问题呢 |