problems with switch functions at arduino

28 Views Asked by At

My problem: When I click my button m_1 (switch modes) the led from previous task dont turns off ( here i made this, but i think it's not good idea, because I forcibly disable led from previous task....) maybe there is a more correct software approach?

for exmple of problem I have in task_0 second button (r_1), I turn_on ledPin_4 then i change task. ledPin_4 dont turn off ( i want that task_0 stopped completely.). I can turn it off only when I go back to task 0.

I have same function:


#define MODE_AM 3    // количество режимов
const int ledPin_1 =  12;      // номер выхода светодиода
const int ledPin_2 =  11;
const int ledPin_3 =  10;
const int ledPin_4 =  5;
#include <EncButton.h>
// моя библиотека для более удобной работы с кнопкой
// скачать мождно здесь https://github.com/GyverLibs/EncButton
Button m_1(3);
Button r_1(2);

byte mode = 0;       // переменная режима
void setup() {
  Serial.begin(19200);
}
void loop() {
  m_1.tick();
  if (m_1.click()) {
    if (++mode >= MODE_AM) mode = 0;
  }
  // свитч крутится в цикле loop и задачи постоянно вызываются
  switch (mode) {
    case 0: task_0();
      break;
    case 1: task_1();
      break;
    case 2: task_2();
      break;
  }
}
// наши задачи
void task_0() {
  Serial.println("Task 0");
  digitalWrite(ledPin_3, LOW);
  digitalWrite(ledPin_1, HIGH);
  r_1.tick();
  if (r_1.click()) digitalWrite(ledPin_4, !digitalRead(ledPin_4));
}
void task_1() {
  Serial.println("Task 1");
  digitalWrite(ledPin_1, LOW);
  digitalWrite(ledPin_2, HIGH);

}
void task_2() {
  Serial.println("Task 2");
  digitalWrite(ledPin_3, HIGH);
  digitalWrite(ledPin_2, LOW);
}

0

There are 0 best solutions below