Не могу обойти delay()

Нет ответов
Kontra
Offline
Зарегистрирован: 10.07.2013

Есть проект - управление 6 светодиодами в разных комбинациях от 8 кнопок.

Написал программу для ардуины, все собрал, работает.

Однако, когда в бесконечном цикле while (1) запускаю перебор эффектов лоя светодиодов, не могу кнопкой это действие прервать.

Грешу на паразитическую функцию delay().

Решил заменить ее функцией SetTimeOutиз либы SimpleTimeout(). Но в нее нельзя передать функцию void с параметрами, только голую. 

Иначе выдает ошибки:

sketch_jul10a.ino: In function 'void FadeLED_1_Lamp(int)':
sketch_jul10a:373: error: invalid use of void expression
sketch_jul10a:383: error: invalid use of void expression

Подскажите как обойти. Скетч ниже. Спасибо, коллеги!

#include <Keypad.h>
#include <SimpleTimer.h>

// the timer object
SimpleTimer timer;


byte L[6] = {3, 5, 6, 9, 10, 11};

const byte ROWS = 4; //four rows
const byte COLS = 2; //three columns
char keys[ROWS][COLS] = {
          {'1','2'},
          {'4','5'},
          {'7','8'},
          {'*','0'}
        };
byte rowPins[ROWS] = {13,12,8,7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4,2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int regim1=1;
int regim2=1;
int regim3=1;
int regim4=1;
int regim5=1;
int regim6=1;
int regim7=1;
int regim8=1;

void setup(){
  pinMode(L[0], OUTPUT);
  pinMode(L[1], OUTPUT);
  pinMode(L[2], OUTPUT);
  pinMode(L[3], OUTPUT);
  pinMode(L[4], OUTPUT);
  pinMode(L[5], OUTPUT);
  digitalWrite(L[0], LOW);
  digitalWrite(L[1], LOW);
  digitalWrite(L[2], LOW);
  digitalWrite(L[3], LOW);
  digitalWrite(L[4], LOW);
  digitalWrite(L[5], LOW);
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
  timer.run();
  char key = keypad.getKey();

}

//take care of some special events
void keypadEvent(KeypadEvent key){
  switch (keypad.getState()){
    case PRESSED:
      switch (key){
        case '1':
        regim2=1;
        regim3=1;
        regim4=1;
        regim5=1;
        regim6=1;
        regim7=1;
        regim8=1;
        regim1++;
        if(regim1>4)//ограничим количество режимов
        {
          regim1=1;
        }
        SingleLamp(regim1, L[1]);
        break;
        case '2':
        regim1=1;
        regim3=1;
        regim4=1;
        regim5=1;
        regim6=1;
        regim7=1;
        regim8=1;
        regim2++;
        if(regim2>4)//ограничим количество режимов
        {
          regim2=1;
        }
        DoubleLamp(regim2, L[0], L[2]);
        break;
        case '4':
        regim1=1;
        regim2=1;
        regim4=1;
        regim5=1;
        regim6=1;
        regim7=1;
        regim8=1;
        regim3++;
        if(regim3>4)//ограничим количество режимов
        {
          regim3=1;
        }
        DoubleLamp(regim3, L[3], L[4]);
        break;
        case '5':
        regim1=1;
        regim2=1;
        regim3=1;
        regim5=1;
        regim6=1;
        regim7=1;
        regim8=1;
        regim4++;
        if(regim4>4)//ограничим количество режимов
        {
          regim4=1;
        }
        SingleLamp(regim4, L[5]);
        break;
        case '7':
        regim1=1;
        regim2=1;
        regim3=1;
        regim4=1;
        regim6=1;
        regim7=1;
        regim8=1;
        regim5++;
        if(regim5>4)//ограничим количество режимов
        {
          regim5=1;
        }
        DoubleLamp(regim5, L[0], L[3]);
        break;
        case '8':
        regim1=1;
        regim2=1;
        regim3=1;
        regim4=1;
        regim5=1;
        regim7=1;
        regim8=1;
        regim6++;
        if(regim6>4)//ограничим количество режимов
        {
          regim6=1;
        }
        DoubleLamp(regim6, L[2], L[4]);
        break;
        case '*':
        regim1=1;
        regim2=1;
        regim3=1;
        regim4=1;
        regim5=1;
        regim6=1;
        regim8=1;
        regim7++;
        if(regim7>4)//ограничим количество режимов
        {
          regim7=1;
        }
        Effects1(regim7);
        break;
        case '0':

        break;
      }
    break;
    case RELEASED:
      switch (key){
        case '*': case '1':break;
      }
    break;
    case HOLD:
      switch (key){
        case '*': AllOff(); break;
      }
    break;
  }
}

void AllOff(){
  digitalWrite(L[0], LOW);
  digitalWrite(L[1], LOW);
  digitalWrite(L[2], LOW);
  digitalWrite(L[3], LOW);
  digitalWrite(L[4], LOW);
  digitalWrite(L[5], LOW);
}

void DoubleLamp(int regim, int l1, int l2){

        if(regim==1)//первый режим
        {
          for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
          analogWrite(l1, fadeValue); analogWrite(l2, fadeValue);
          delay(10);
          }
        }
        if(regim==2)//второй режим
        {
          AllOff(); //выключаем все светодиоды
          for(int fadeValue = 0 ; fadeValue <= 20; fadeValue++) {
          analogWrite(l1, fadeValue); analogWrite(l2, fadeValue);
          delay(10);
          }
        }
        if(regim==3)//третий режим
        {
          for(int fadeValue = 20 ; fadeValue <= 100; fadeValue +=5) {
          analogWrite(l1, fadeValue); analogWrite(l2, fadeValue);
          delay(10);
          }
        }
        if(regim==4)//четвертый режим
        {
          for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=5) {
          analogWrite(l1, fadeValue); analogWrite(l2, fadeValue);
          delay(10);
          }
        }
}

void SingleLamp(int regim, int l){

        if(regim==1)//первый режим
        {
          for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
          analogWrite(l, fadeValue);
          delay(10);
          }
        }
        if(regim==2)//второй режим
        {
          AllOff(); //выключаем все светодиоды
          for(int fadeValue = 0 ; fadeValue <= 20; fadeValue++) {
          analogWrite(l, fadeValue);
          delay(10);
          }
        }
        if(regim==3)//третий режим
        {
          for(int fadeValue = 20 ; fadeValue <= 100; fadeValue +=5) {
          analogWrite(l, fadeValue);
          delay(10);
          }
        }
        if(regim==4)//четвертый режим
        {
          for(int fadeValue = 100 ; fadeValue <= 255; fadeValue +=5) {
          analogWrite(l, fadeValue);
          delay(10);
          }
        }
}

void Effects1(int regim){

        if(regim==1)//первый режим
        {
          while (keypad.keyStateChanged()==1){
            FadeLED_2_Lamps(L[0], L[2]);
            FadeLED_1_Lamp( L[1]);
            FadeLED_2_Lamps(L[0], L[2]);
            FadeLED_1_Lamp( L[0]);
            FadeLED_1_Lamp( L[1]);
            FadeLED_1_Lamp( L[2]);
            FadeLED_1_Lamp( L[1]);
            FadeLED_1_Lamp( L[0]);
            FadeLED_1_Lamp( L[1]);
            FadeLED_1_Lamp( L[2]);
            FadeLED_1_Lamp( L[3]);
            FadeLED_1_Lamp( L[4]);
            FadeLED_1_Lamp( L[5]);
            FadeLED_1_Lamp( L[4]);
            FadeLED_1_Lamp( L[3]);
            FadeLED_1_Lamp( L[2]);
            FadeLED_1_Lamp( L[1]);
            FadeLED_1_Lamp( L[0]);
            FadeLED_2_Lamps(L[0], L[1]);
            FadeLED_2_Lamps(L[1], L[2]);
            FadeLED_2_Lamps(L[2], L[3]);
            FadeLED_2_Lamps(L[3], L[4]);
            FadeLED_2_Lamps(L[4], L[5]);
            FadeLED_2_Lamps(L[4], L[3]);
            FadeLED_2_Lamps(L[3], L[2]);
            FadeLED_2_Lamps(L[2], L[1]);
            FadeLED_2_Lamps(L[1], L[0]);
            FadeLED_3_Lamps(L[0], L[1], L[2]);
            FadeLED_3_Lamps(L[1], L[2], L[3]);
            FadeLED_3_Lamps(L[2], L[3], L[4]);
            FadeLED_3_Lamps(L[3], L[4], L[5]);
            FadeLED_3_Lamps(L[2], L[3], L[4]);
            FadeLED_3_Lamps(L[1], L[2], L[3]);
            FadeLED_3_Lamps(L[0], L[1], L[2]);
          }
        }
        if(regim==2)//второй режим
        {
           FadeLED_2_Lamps(L[0], L[2]);
            FadeLED_1_Lamp( L[1]);

          }
        if(regim==3)//третий режим
        {
            FadeLED_1_Lamp( L[0]);
            FadeLED_1_Lamp( L[1]);
            FadeLED_1_Lamp( L[2]);
            FadeLED_1_Lamp( L[1]);
        }
        if(regim==4)//четвертый режим
        {

        }
}


void FadeLED_3_Lamps(int l1, int l2, int l3){
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=2) {
    // sets the value (range from 0 to 255):
    analogWrite(l1, fadeValue);
    analogWrite(l2, fadeValue);
    analogWrite(l3, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(5);
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=2) {
    // sets the value (range from 0 to 255):
    if (fadeValue <3 ) fadeValue=0;
    analogWrite(l1, fadeValue);
    analogWrite(l2, fadeValue);
    analogWrite(l3, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(5);
  }
}
void FadeLED_2_Lamps(int l1, int l2){
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=2) {
    // sets the value (range from 0 to 255):
    analogWrite(l1, fadeValue);
    analogWrite(l2, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(5);
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=2) {
    // sets the value (range from 0 to 255):
    if (fadeValue <3 ) fadeValue=0;
    analogWrite(l1, fadeValue);
    analogWrite(l2, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(5);
  }
}
void FadeLED_1_Lamp(int l1){
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=2) {
    // sets the value (range from 0 to 255):
    timer.setTimeout(1000, llamp(l1, fadeValue));
    // wait for 30 milliseconds to see the dimming effect
    //delay(5);

  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=2) {
    // sets the value (range from 0 to 255):
    if (fadeValue <3 ) fadeValue=0;
    timer.setTimeout(1000, llamp(l1, fadeValue));
    // wait for 30 milliseconds to see the dimming effect
    //delay(5);
    //timer.setTimeout(10000, OnceOnlyTask);

  }
}

void llamp(int pin, byte brightness){
 analogWrite(pin, brightness);
}

void callMeLater() {
    // do something
}