点灯APP使用滑动条控制舵机角度
本帖最后由 hun517 于 2020-11-21 10:59 编辑大家好!
下面是我现在的代码,现在代码是点击一个按钮舵机就旋转代码中设定的角度,我想用点灯里的滑动条来控制舵机角度,比如我滑动条滑动到10.然后我点ON按钮,舵机就读取滑动条的数值,然后舵机旋转滑动条数值角度。需要在代码里怎么加啊,小白拜托大家教教。感谢!!!
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
#include <Servo.h>
Servo myservo;//定义舵机
char auth[] = "f65ffe013a54";//根据需要自行修改
char ssid[] = "OPPOR17";
char pswd[] = "12345678";
// 新建组件对象
BlinkerButton Button1("test");
int counter = 0;
void miotPowerState(const String & state)//电源类操作
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myservo.write(115);//收到“on”的指令后舵机旋转140度
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();//反馈状态
delay(2000);//延时两秒
myservo.write(90);//舵机归零,回到垂直状态
}
else if (state == BLINKER_CMD_OFF) {
myservo.write(55);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
delay(2000);
myservo.write(90);
}
}
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on")
{
myservo.write(115);//收到“on”的指令后舵机旋转130
delay(2000);//延时
myservo.write(90);//舵机归零垂直
} else if(state=="press")
{
myservo.write(55);//长按开关按键后舵机旋转130
delay(2000);//延时
myservo.write(90);//舵机归零垂直
}
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化舵机
myservo.attach(0);//舵机的IO口,nodemcu的D6口
myservo.write(90);//上电时舵机归零垂直
BlinkerMIOT.attachPowerState(miotPowerState);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
}
void loop() {
Blinker.run();
}
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
#include <Servo.h>
Servo myservo;//定义舵机
Servo myservo1;//定义舵机
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
// 新建组件对象
BlinkerButton Button1("test");
BlinkerSlider Slider_servo("ran-servo");//滑块
BlinkerSlider Slider_servo1("ran-servo1");//滑块
int counter = 0;
int32_t servo_val = 90;
int32_t servo_val1 = 90;
void miotPowerState(const String & state)//电源类操作
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myservo.write(servo_val);//收到“on”的指令后舵机旋转140度
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();//反馈状态
delay(2000);//延时两秒
myservo.write(90);//舵机归零,回到垂直状态
}
else if (state == BLINKER_CMD_OFF) {
myservo.write(servo_val);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
delay(2000);
myservo.write(90);
}
}
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on")
{
myservo.write(servo_val);//收到“on”的指令后舵机旋转130
Button1.color("#00FF00"); /*设置app按钮颜色*/
Button1.text("开"); /*设置app显示文本*/
Button1.print("on"); /*设置app状态为on*/
delay(2000);//延时
myservo.write(90);//舵机归零垂直
} else if(state=="press")
{
myservo.write(servo_val);//长按开关按键后舵机旋转130
Button1.color("#FF0000"); /*设置app按钮颜色*/
Button1.text("关"); /*设置app显示文本*/
Button1.print("off"); /*设置app状态为on*/
delay(2000);//延时
myservo.write(90);//舵机归零垂直
}
}
void Slider_servo_callback(int32_t value)
{
servo_val = value;
}
void Slider1_servo_callback(int32_t value)
{
myservo1.write(value);
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化舵机
myservo.attach(13);//舵机的IO口,nodemcu的D6口
myservo.write(90);//上电时舵机归零垂直
myservo1.attach(12);
BlinkerMIOT.attachPowerState(miotPowerState);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Slider_servo.attach(Slider_servo_callback);
Slider_servo1.attach(Slider1_servo_callback);
}
void loop() {
Blinker.run();
} https://blog.csdn.net/weixin_51397215/article/details/116542951
看看这个大佬的 老是报servo对应多个库怎么解决:'(
#include <Servo.h>
volatile int item;
Servo servo_6;
#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
BlinkerSlider slider1("SliderKey");
void slider1_callback(int32_t value) {
item = value;
}
BlinkerButton Button1("btn-abc");
void Button1_callback(const String & state) {
if (state == "on") {
servo_6.write(item);
delay(100);
}
}
void setup(){
item = 0;
servo_6.attach(6);
servo_6.write(0);
delay(100);
Blinker.begin(auth,ssid,pswd);
slider1.attach(slider1_callback);
Button1.attach(Button1_callback);
}
void loop(){
Blinker.run();
}
201716070206 发表于 2021-5-2 18:53
老是报servo对应多个库怎么解决
把看着不像是需要的删掉,或者移动到别的地方 本帖最后由 paulhe 于 2021-5-26 00:04 编辑
楼主实现这个功能了吗,我也想做一个滑动条控制舵机,求交流 paulhe 发表于 2021-5-26 00:03
楼主实现这个功能了吗,我也想做一个滑动条控制舵机,求交流
没有呢。。 maker167 发表于 2021-5-3 19:15
#include
volatile int item;
大佬,看不懂,加在哪里啊 看看这个大佬的
https://blog.csdn.net/weixin_51397215/article/details/116542951
页:
[1]