สร้างปุ่มด้วยฟังก์ชั่น task ตัวบอร์ดรีบูดเองตลอด (ESP32)

ผมสร้าง ฟังก์ชั่นปุ่มกด มา 2 ปุ่มครับโดยใช้การสร้าง task ครับ ผลที่ได้คือ ปุ่มสามารถสั่ง LED กดครั้งแรกเปิด และ กดอีกทีดับ ได้ทั้ง 2 ปุ่มครับ แต่ตัวบอร์ดก็จะรีบูดตัวเองเรื่อยๆเหมือนกัน แสดงข้อความบนมอนิเตอร์ ดังนี้ครับ

ปล. ผมลองสร้าง task จากโค็ดตัวตัวอย่างต่างๆ สามารถใช้งานได้ปกติไม่มีปัญหาใดๆนะครับ

นี่โค็ดที่มีปัญหาครับ รบกวนด้วยครับ :pray:

/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached from pin 2 to +5V
  - 10 kilohm resistor attached from pin 2 to ground

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 25;    // the number of the pushbutton pin
const int buttonPin1 = 33;    // the number of the pushbutton pin
const int ledPin = 4;      // the number of the LED pin
const int ledPin1 = 2;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int ledState1 = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int buttonState1;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int lastButtonState1 = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long lastDebounceTime1 = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
unsigned long debounceDelay1 = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);

  xTaskCreate(taskOne_SW, "taskOne_SW", 20000, NULL, 1, NULL);


  // set initial LED state
  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin1, ledState1);
}

void taskOne_SW(void * parameter)
{
  for (;;)
  {  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  int reading1 = digitalRead(buttonPin1);
  
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  if (reading1 != lastButtonState1) {
    // reset the debouncing timer
    lastDebounceTime1 = millis();
  }

  if ((millis() - lastDebounceTime1) > debounceDelay1) {
    if (reading1 != buttonState1) {
      buttonState1 = reading1;

      if (buttonState1 == HIGH) {
        ledState1 = !ledState1;
      }
    }
  }

  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin1, ledState1);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
  lastButtonState1 = reading1;
}
   vTaskDelete(NULL);
}

void loop() {
}

เพิ่ม delay ใน void loop () หน่อยครับ

void loop() {

delay(1000);

}

เป็นเหมือนเดิมเลยครับ รบกวนช่วยดูอีกรอบนะครับ

ทดสอบเบื้องต้นบอร์ดผมก็เป็นครับ เดียวว่างจะลองแก้ดูนะครับ

1 Likes

ขอบคุณมากครับ

เบื้องต้นลองย้ายคำสั่งจาก tesk ที่ไม่จำเป็นไปไว้ใน void loop ดูก่อนครับ

คือ จริงๆผมจะนำไปไว้ในโค็ดหลักครับ อยากให้เป็น task แล้วไปใส่อะครับ ผมเคยเอาคำสั่งพวก debounce ไปใส่ใน loop แล้วมันไม่สามารถเปิด-ปิด จาก sw ได้เลยครับ

ใช้งานได้ปกติแล้วครับ ด้วยการเพิ่ม

vTaskDelay(10 / portTICK_PERIOD_MS)

แต่ผมก็ไม่รู้เป็นเพราะอะไรอยู่ดีครับ พี่พอจะบอกได้ไหมครับ

ลองใส่แค่

vTaskDelay(100);

ดูว่าได้ไหม

อย่างที่บอกครับ การไม่หน่วงเวลาเลยทำให้การทำการ เกาะ wifi อาจทำงานไม่ได้ ระบบมันจึง ทำ watdog เพื่อให้ระบบ wifi สามารถทำงานได้ แม่จะไม่ได้สั่งให้จับ wifi ก็ตาม

1 Likes

ขอบคุณครับ :blush: