ตอนนี้ติดปัญหาตรงที่ timer ใน blynk ไม่ทำงานครับ (จากที่ผมเข้าใจ Button Switch ถ้ายังไม่กดปุ่มสถานะจะเป็น 1 หากกดเป็น 0) ผมคิดไม่ออกเลยครับ รบกวนช่วยหน่อยครับ
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 16 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 14 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200
Servo servo;
BlynkTimer timer;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
char auth[] = "xxxxxxxxxxx";
const char ssid[] = "xxxxxxx";
const char pass[] = "xxxxxxx";
int buzzer = 0; //D3
int sw = 13; //D7
int btn_prev = 1;
int btn_state;
int btn1;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
servo.attach(12); //NodeMCU ใช้ช่อง D6
pinMode(sw,INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
}
void loop()
{
Blynk.run();
//---------------ปุ่มกด-----------------------------------//
btn_state = digitalRead(sw); //read the state of the button
if ( (btn_state == 0) && (btn_prev == 1) ) { // if is pressed
servo.write(40);
btn1 = btn_state;
Serial.print("LOW = "); // LOW
Serial.println(btn1);
}
if ( (btn_state == 1) || (btn_prev == 0) ) { // if not pressed
servo.write(110);
btn1 = btn_state;
Serial.print("HIGH = "); // HIGH
Serial.println(btn1);
}
//----------------------------------------------------------//
//------------------------ระดับน้ำ-------------------------------------------//
int distance = sonar.ping_cm();
// Serial.print("Ping: ");
// Serial.print(distance); // Send ping, get distance in cm and print result (0 = outside set distance range)
// Serial.println("cm");
Blynk.virtualWrite(V5, distance);
if(distance > 15) {
digitalWrite(buzzer, HIGH);
}
else if(distance <= 15 ) {
digitalWrite(buzzer, LOW);
}
//--------------------------------------------------------------------//
}
BLYNK_WRITE(V1) // Button ให้อาหาร
{
servo.write(param.asInt());
delay(1000);
Serial.println("0");
}
BLYNK_WRITE(V2) // ตั้งเวลาให้อาหาร
{
Serial.print("value : ");
Serial.println(param.asInt());
if( (btn1 == 0) || param.asInt() == 0) {
servo.write(110);
}
if( (btn1 == 1) && param.asInt() == 1) {
servo.write(40);
}
}
BLYNK_WRITE(V3) // ตั้งเวลาเตือนให้อาหาร
{
Serial.print("value : ");
Serial.println(param.asInt());
if(param.asInt() == 1) {
Blynk.notify("Human !!! I need food");
}
}