|
闲暇之时将孩子儿时的线控玩具坦克改成了blinker控制的遥控坦克.首先更换了原来耗电量很大的旧电机,然后新设计增加了声光炮击电路和探照灯电路.又选
用舵机电路来控制炮台旋转功能,通过滑块来调节方向和角度.最后对新增的6V镍氢电池组设计了充电保护电路,利用了8266的ADC端口监控电池电压,确保充电终止
电压在7V左右.而且在blinker app上也设有电压数据块可实时显示电池电压.
程序与电路图如下:
#define BLINKER_WIFI
#include <Blinker.h>
#include <Servo.h>
Servo myservo;
char auth[]="******";
char ssid[]="******";
char pswd[]="******";
BlinkerNumber Battery("dy");
BlinkerSlider Slider1("fx");
BlinkerButton Button("dd");
BlinkerButton Button1("rb");
BlinkerButton Button2("lb");
BlinkerButton Button3("lf");
BlinkerButton Button4("rf");
BlinkerButton Button5("ff");
int k0=95;
int k1=95;
int val=0;
int pos=95;
int dy_read=0;
void slider1_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
k1=180-value;
}
void button_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on")
{
Button.icon("fas fa-lightbulb");//fa-lightbulb
Button.color("#FF0000");
Button.text("开");
digitalWrite(0, HIGH);
Button.print("on");
}
else if(state=="off")
{
Button.icon("fal fa-lightbulb");
Button.color("#FF0000");
Button.text("关");
digitalWrite(0, LOW);
Button.print("off");
}
}
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_PRESSED)
{
Button1.icon("fas fa-arrow-alt-down");//fa-lightbulb
Button1.color("#00FF00");
Button1.text("开");
digitalWrite(14, HIGH);
Button1.print("on");
}
else if(state == BLINKER_CMD_BUTTON_RELEASED)
{
Button1.icon("fal fa-arrow-alt-down");
Button1.color("#0000FF");
Button1.text("关");
digitalWrite(14, LOW);
Button1.print("off");
}
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_PRESSED)
{
Button2.icon("fas fa-arrow-alt-down");//fa-lightbulb
Button2.color("#00FF00");
Button2.text("开");
digitalWrite(12, HIGH);
Button2.print("on");
}
else if(state == BLINKER_CMD_BUTTON_RELEASED)
{
Button2.icon("fal fa-arrow-alt-down");
Button2.color("#0000FF");
Button2.text("关");
digitalWrite(12, LOW);
Button2.print("off");
}
}
void button3_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_PRESSED)
{
Button3.icon("fas fa-arrow-alt-up");//fa-lightbulb
Button3.color("#00FF00");
Button3.text("开");
digitalWrite(13, HIGH);
Button3.print("on");
}
else if(state == BLINKER_CMD_BUTTON_RELEASED)
{
Button3.icon("fal fa-arrow-alt-up");
Button3.color("#0000FF");
Button3.text("关");
digitalWrite(13, LOW);
Button3.print("off");
}
}
void button4_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_PRESSED)
{
Button4.icon("fas fa-arrow-alt-up");//fa-lightbulb
Button4.color("#00FF00");
Button4.text("开");
digitalWrite(16, HIGH);
Button4.print("on");
}
else if(state == BLINKER_CMD_BUTTON_RELEASED)
{
Button4.icon("fal fa-arrow-alt-up");
Button4.color("#0000FF");
Button4.text("关");
digitalWrite(16, LOW);
Button4.print("off");
}
}
void button5_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_PRESSED)
{
Button5.icon("fas fa-burn");//fa-lightbulb
Button5.color("#FF0000");
Button5.text("开");
digitalWrite(2, HIGH);
Blinker.delay(100);
digitalWrite(2, LOW);
Button5.print("on");
}
else if(state == BLINKER_CMD_BUTTON_RELEASED)
{
Button5.icon("fal fa-burn");
Button5.color("#FF0000");
Button5.text("关");
digitalWrite(2, LOW);
Button5.print("off");
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print(BlinkerTime);
Blinker.print("millis", BlinkerTime);
}
void heartbeat()
{
Battery.print(dy_read);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
pinMode(2, OUTPUT);
pinMode(0, OUTPUT);
pinMode(12, OUTPUT);
pinMode(16, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(4,OUTPUT);
digitalWrite(2, LOW);
digitalWrite(0, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(14, LOW);
digitalWrite(16, LOW);
digitalWrite(15, LOW);
pinMode(A0,INPUT);
myservo.attach(4);
Blinker.begin(auth,ssid,pswd);
Blinker.attachHeartbeat(heartbeat);
Button.attach(button_callback);
Slider1.attach(slider1_callback);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Blinker.attachData(dataRead);
}
void loop()
{
Blinker.run();
val=analogRead(A0);
dy_read=val*70/8;
if(val>800) digitalWrite(15,LOW);
else digitalWrite(15,HIGH);
if(k1>k0) {
for (pos = k0; pos <= k1; pos += 1) {
myservo.write(pos);
delay(50);
}
k0=k1;
}
else {
for (pos = k0; pos >= k1; pos -= 1) {
myservo.write(pos);
delay(50);
}
k0=k1;
}
Blinker.delay(1000);
}
|
|