#define BUTTON_1 21
#define BUTTON_2 13
//#define BUTTON_2 12
#define LED_1 2
#define LED_2 33
#define LED_BLINK 14
int led_state = LOW; // the current state of LED
int button_state; // the current state of button
int last_button_state; // the previous state of button
int led2_state = LOW; // the current state of LED
int button2_state; // the current state of button
int last_button2_state; // the previous state of button
// define two tasks for Blink & AnalogRead
void TaskButton( void *pvParameters );
void TaskBlinkLED( void *pvParameters );
//void Task3( void *pvParameters );
void setup() {
pinMode(LED_BLINK, OUTPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
//pinMode(12, OUTPUT);
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
Serial.begin(115200);
button_state = digitalRead(BUTTON_1);
// Now set up two tasks to run independently.
xTaskCreatePinnedToCore(
TaskButton
, "TaskButton" // A name just for humans
, 2048 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 1 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, 0);
xTaskCreatePinnedToCore(
TaskBlinkLED
, "Blynk"
, 4096 // Stack size
, NULL
, 0 // Priority
, NULL
, 1);
// xTaskCreatePinnedToCore(
// Task3
// , "Task3" // A name just for humans
// , 2048 // This stack size can be checked & adjusted by reading the Stack Highwater
// , NULL
// , 1 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
// , NULL
// , 1);
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
void loop()
{
}
void TaskButton(void *pvParameters) // This is a task.
{
(void) pvParameters;
for (;;) // A Task shall never return or exit.
{
last_button_state = button_state; // save the last state
button_state = digitalRead(BUTTON_1); // read new state
if (last_button_state == HIGH && button_state == LOW) {
//Serial.println("The button is pressed");
// toggle state of LED
led_state = !led_state;
// control LED arccoding to the toggled state
digitalWrite(LED_1, led_state);
}
// if (last_button_state == LOW && button_state == HIGH) {
// led_state = 0;
// digitalWrite(LED_1, led_state);
// }
vTaskDelay(5); // one tick delay (15ms) in between reads for stability
//
last_button2_state = button2_state; // save the last state
button2_state = digitalRead(BUTTON_2); // read new state
if (last_button2_state == HIGH && button2_state == LOW) {
//Serial.println("The button is pressed");
// toggle state of LED
led2_state = !led2_state;
// control LED arccoding to the toggled state
digitalWrite(LED_2, led2_state);
}
// if (last_button2_state == LOW && button2_state == HIGH) {
// led2_state = 0;
// digitalWrite(LED_2, led2_state);
// }
vTaskDelay(5); //
}
}
void TaskBlinkLED(void *pvParameters) // This is a task.
{
(void) pvParameters;
for (;;)
{
digitalWrite(LED_BLINK, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay(50); // one tick delay (15ms) in between reads for stability
digitalWrite(LED_BLINK, LOW); // turn the LED off by making the voltage LOW
vTaskDelay(50);
}
}
// void Task3(void *pvParameters) // This is a task.
// {
// (void) pvParameters;
// for (;;)
// {
// // digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
// // vTaskDelay(50); // one tick delay (15ms) in between reads for stability
// // digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
// // vTaskDelay(50);
// }
//}