PullupDev/src/main.cpp

103 lines
2.4 KiB
C++
Raw Normal View History

2023-04-11 19:16:14 +08:00
#include "HX711.h"
#include "OneButton.h"
#include "Serialcommand.h"
#include "config.h"
2023-04-11 19:16:14 +08:00
// 角度传感器
// 收放线电机控制
// 控制串口直接使用Serial2用法和Serial一样如需要还可以用Serial1但需要映射引脚
//---------------------------------
OneButton button_up(BTN_UP, true);
OneButton button_down(BTN_DOWN, true);
OneButton button_checktop(BTN_CT, true);
HX711 scale;
Serialcommand sercomm;
2023-04-11 19:16:14 +08:00
void upbtn_click();
void downbtn_click();
void ctbtn_pressend();
void ctbtn_pressstart();
void led_tip(bool onled);
void setup()
{
// 调试串口
Serial.begin(115200);
Serial.println("Starting PullupDevice...");
2023-04-11 19:16:14 +08:00
// 初始化按钮
button_up.attachClick(upbtn_click);
button_down.attachClick(downbtn_click);
button_checktop.setPressTicks(10); // 10毫秒就产生按下事件用于顶部按钮检测
button_checktop.attachLongPressStart(ctbtn_pressstart); // 按下马上产生事件,
button_checktop.attachLongPressStop(ctbtn_pressend); // 抬起
// 初始化称重传感器
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(516.f); // 这是缩放值,根据砝码实测
scale.tare(); // 重置为0
// 关闭灯光LED
pinMode(LED_TIP, OUTPUT);
led_tip(false);
Serial.println("PullupDevice is ready!");
}
float Value;
#define FILTER_A 0.5
// 低通滤波和限幅后的拉力数值,单位:克
float Get_PullUnits()
{
float NewValue;
if (scale.wait_ready_timeout(100)) // 等待数据ok,100ms超时
NewValue = scale.get_units();
else
NewValue = 0;
Value = NewValue * FILTER_A + (1.0 - FILTER_A) * Value; // 低通滤波
Value = constrain(Value, 0.0, 6000.0); // 限制到0-6公斤
return Value;
}
// 提示灯光控制
void led_tip(bool onled)
{
if (onled)
digitalWrite(LED_TIP, LOW); // 亮灯
else
digitalWrite(LED_TIP, HIGH); // 亮灯
}
void loop()
{
button_checktop.tick();
button_down.tick();
button_up.tick();
sercomm.getcommand();
2023-04-11 19:16:14 +08:00
delay(10);
// Serial.print("PullUnits:\t");
// Serial.println(Get_PullUnits(), 1);
}
void ctbtn_pressstart()
{
Serial.println("ctbtn_pressstart");
led_tip(true);
}
void ctbtn_pressend()
{
Serial.println("ctbtn_pressend");
led_tip(false);
}
void downbtn_click()
{
Serial.println("downbtn_click");
}
void upbtn_click()
{
Serial.println("upbtn_click");
}