|
本帖最后由 wangyirun 于 2021-7-3 20:52 编辑
程序:#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#include <Adafruit_NeoPixel.h>
#include <Blinker.h>
#include <Servo.h>
#include "SR04.h"
char auth[] = "f424ebfe5a66";
char ssid[] = "CHENLL";
char pswd[] = "24681357";
const int servo1pin=26;
const int servo2pin=27;
#define touch 36
#define TRIG1 10
#define ECHO1 13
#define TRIG2 5
#define ECHO2 2
#define led 25
#define sig 9
#define button 39
Adafruit_NeoPixel strip=Adafruit_NeoPixel(5, led, NEO_GRB + NEO_KHZ800);
BlinkerButton Button1("btn-1");
BlinkerButton Button2("btn-2");
BlinkerNumber Number1("num-1");
BlinkerNumber Number2("num-2");
BlinkerNumber Number3("num-3");
SR04 sr041=SR04(ECHO1,TRIG1);
SR04 sr042=SR04(ECHO2,TRIG2);
Servo servo1;
Servo servo2;
int a,b,count1=0,count2=0,flag=0,f1,f2,j;
void setup()
{
Serial.begin(9600);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
Blinker.begin(auth, ssid, pswd);
Number1.print(count1);
Number2.print(count2);
Number3.print(flag);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Blinker.attachHeartbeat(heartbeat);
servo1.attach(servo1pin);
servo2.attach(servo2pin);
pinMode(touch,INPUT);
pinMode(button,INPUT);
pinMode(sig,OUTPUT);
servo1.write(180);
servo2.write(0);
strip.begin();
strip.show();
for(int i=0;i<=5;i++)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
}
}
void play()
{
digitalWrite(sig,HIGH);
delay(200);
digitalWrite(sig,LOW);
delay(2900);
}
void hulanon()
{
servo1.write(1);
servo2.write(180);
delay(300);
}
void hulanoff()
{
servo1.write(179);
servo2.write(1);
delay(300);
}
void yled()
{
for(int i=0;i<=4;i++)
{
strip.setPixelColor(i, strip.Color(255, 255, 0));
strip.show();
}
}
void rled()
{
for(int i=0;i<=4;i++)
{
strip.setPixelColor(i, strip.Color(255, 0, 0));
strip.show();
}
}
void bled()
{
for(int i=0;i<=4;i++)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
}
}
void heartbeat()
{
Number1.print(count1);
Number2.print(count2);
Number3.print(flag);
}
void button1_callback(const String & state)
{
BLINKER_LOG("button1:",state);
count1++;
if(count1%2==1)
{
rled();
play();
flag=1;
Blinker.vibrate(1000);
}
else
{
bled();
flag=0;
}
Number1.print(count1);
}
void button2_callback(const String & state)
{
BLINKER_LOG("button2:", state);
count2++;
if(count2%2==1)
{
hulanon();
flag=1;
Blinker.vibrate(1000);
}
else
{
hulanoff();
flag=0;
}
Number2.print(count2);
}
void loop()
{
Blinker.run();
a=sr041.Distance();
b=sr042.Distance();
if(a>=22&&a<=27) f1=0;
else f1=1;
Serial.print("f1:");
Serial.println(f1);
if(b>=22&&b<=27) f2=0;
else f2=1;
Serial.print("f2:");
Serial.println(f2);
if(f1==1&&f2==0)
{
yled();
play();
count1++;
flag=1;
Blinker.vibrate(1000);
Serial.println("Children enter balcony!");
if(digitalRead(touch)==HIGH)
{
Serial.println(digitalRead(touch));
for(j=1;j<=2;j++)
{
flag=1;
hulanon();
count2++;
Blinker.vibrate(1000);
rled();
play();
Serial.println("Children touch guardrail!");
if(f1==1&&f2==1&&digitalRead(button)==HIGH)
{
flag=0;
hulanoff();
Serial.println("Children leave the balcony!");
}
}
Blinker.delay(200);
}
}
Blinker.delay(200);
if(f1==1&&f2==1&&digitalRead(button)==HIGH)
{
bled();
hulanoff();
flag=0;
Serial.println("Children leave the balcony!");
}
Blinker.delay(500);
}
错误:
哪位大神能帮忙解决一下?谢谢!
|
|