• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Zliczanie impulsów i pomijanie krótszych niż 150ms
#1
Witam,

udało mi się naskrobać kawałek kodu [Obrazek: smile.gif] jednak nie do końca działa tak jak powinien [Obrazek: sad.gif] Na początku miał tylko zliczać kliknięcia przycisków i po zliczeniu odpowiedniej ilości kliknięć wykonać mrugnięcie diodą, następnie próbowałem zmodyfikować kod tak aby pomijał impulsy krótsze niż 150ms. Do momentu zliczania impulsów wszystko śmigało aż miło, po modyfikacji już niestety gdzieś jest problem. Prośba o sprawdzenie kodu [Obrazek: smile.gif]    Kod testuję na Digisparku.   



Kod:
const int button1 = 1; //zielony
const int button2 = 4; //niebieski
const int led1 = 0; //brazowy//
const int led2 = 2; //bialy//

int button1PushCounter = 0;
int button2PushCounter = 0;
int button1State = 0;
int button2State = 0;
int lastButton1State = 0;
int lastButton2State = 0;
int currentstate = 0;


unsigned long debounceset = 10UL;
unsigned long resettimer = 5000UL;
unsigned long shortpulse1 = 500UL;
unsigned long shortpulse2 = 1000UL;
unsigned long longpulse = 5000UL;


#define STATE_CHECKBUTTONS 0
#define STATE_LEDONE 1
#define STATE_LEDTWO 2
#define STATE_CHECKBUTTON2RELEASED 3
#define STATE_CHECKBUTTON1RELEASED 4

void setup()
{
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
}

void loop()
{
  switch (currentstate)
  {
    case STATE_CHECKBUTTONS:
      checkButtons();
      break;
    case STATE_LEDONE:
      led1_On();
      break;
    case STATE_LEDTWO:
      led2_On();
      break;
    case STATE_CHECKBUTTON2RELEASED:
      checkButton2Released();
      break;
    case STATE_CHECKBUTTON1RELEASED:
      checkButton1Released();
      break;

  }
}

void led1_On()
{
  static unsigned long starttime = 0;
  int b1 = digitalRead(button1);
  int b2 = digitalRead(button2);

  if (starttime == 0)
  {
    starttime = millis();
    digitalWrite(led1, HIGH);
  }
  if (millis() - starttime >= shortpulse1)
    {
        digitalWrite(led1, LOW);
    }
  if (millis() - starttime >= shortpulse1 + shortpulse2)
    {
        digitalWrite(led1, HIGH);
    }
  if (millis() - starttime >= shortpulse1 + shortpulse2 + longpulse)
  {
    digitalWrite(led1, LOW);
    starttime = 0;
      button1PushCounter = 0;
    button2PushCounter = 0;
    if (b1 == LOW)
    {
      currentstate = STATE_CHECKBUTTON1RELEASED;
    }
    else
    {
      currentstate = STATE_CHECKBUTTONS;
    }
    if (b2 == LOW)
    {
      currentstate = STATE_CHECKBUTTON2RELEASED;
    }
    else
    {
      currentstate = STATE_CHECKBUTTONS;
    }
  }
}


void led2_On()
{
  static unsigned long starttime = 0;
  int b1 = digitalRead(button1);
  int b2 = digitalRead(button2);
  
  if (starttime == 0)
  {
    starttime = millis();
    digitalWrite(led2, HIGH);
  }
  if (millis() - starttime >= shortpulse1)
    {
        digitalWrite(led2, LOW);
    }
  if (millis() - starttime >= shortpulse1 + shortpulse2)
    {
        digitalWrite(led2, HIGH);
    }
  if (millis() - starttime >= shortpulse1 + shortpulse2 + longpulse)
  {
    digitalWrite(led2, LOW);
    starttime = 0;
    button1PushCounter = 0;
    button2PushCounter = 0;
    if (b1 == LOW)
    {
      currentstate = STATE_CHECKBUTTON1RELEASED;
    }
    else
    {
      currentstate = STATE_CHECKBUTTONS;
    }
    if (b2 == LOW)
    {
      currentstate = STATE_CHECKBUTTON2RELEASED;
    }
    else
    {
      currentstate = STATE_CHECKBUTTONS;
    }
  }
}

void checkButtons()
{
  static unsigned long starttime = 0;
  static unsigned long debouncestarttime = 0;

  if (starttime != 0 && millis() - starttime >= resettimer)
  {
    starttime = 0;
    button1PushCounter = 0;
    button2PushCounter = 0;
  }

  if (debouncestarttime == 0)
  {
    debouncestarttime = millis();
  }
  
  if (millis() - debouncestarttime < debounceset)
  {
    return;
  }
  debouncestarttime = 0;
  
  button1State = digitalRead(button1);

  if (button1State != lastButton1State )
  {
    starttime = millis();

    if (button1State != LOW)
    {
      button1PushCounter++;
    }
  }
  lastButton1State = button1State;

  button2State = digitalRead(button2);

  if (button2State != lastButton2State )
  {
    starttime = millis();

    if (button2State != LOW)
    {
      button2PushCounter++;
    }
  }
  lastButton2State = button2State;
  
  

  if ((button1PushCounter == 3)&&(button2PushCounter == 3))
  {
    starttime = 0;
    button1PushCounter = 0;
    button2PushCounter = 0;
    currentstate = STATE_LEDONE;
  }


  if ((button1PushCounter == 0)&&(button2PushCounter == 3))
  {
    starttime = 0;
    button2PushCounter = 0;
    button1PushCounter = 0;
    currentstate = STATE_LEDTWO;
  }
}

void checkButton1Released()
{
  static unsigned long debouncestarttime = 0;

  if (debouncestarttime == 0)
  {
    debouncestarttime = millis();
  }

  if (millis() - debouncestarttime < debounceset)
  {
    return;
  }
  
  debouncestarttime = 0;

  if (digitalRead(button1) == HIGH)
  {
    currentstate = STATE_CHECKBUTTONS;
  }
}


void checkButton2Released()
{
  static unsigned long debouncestarttime = 0;

  if (debouncestarttime == 0)
  {
    debouncestarttime = millis();
  }

  if (millis() - debouncestarttime < debounceset)
  {
    return;
  }
  
  debouncestarttime = 0;
  
  if (digitalRead(button2) == HIGH)
  {
    currentstate = STATE_CHECKBUTTONS;
  }
}
 
Odpowiedź
#2
A możesz opisać problem?
Bo gdzieś zapodziałem magiczną kulę.

Z opisu wynika, że jak wywalę te instrukcje:
if (millis() - debouncestarttime < debounceset)
{
return;
}
to wszystko działa jak należy? RLY?

A cha, i jeszcze jedno: aby na pewno funkcja checkButtons(); jest przerywana dla impulsów mniejszych od 150ms?
Bo mi się wydaje, że dla <10ms.
Jeśli masz problem z kodem lub sprzętem, zadaj pytanie na forum. Nie odpowiadam na PW, jeśli nie dotyczą one spraw forum lub innych tematów prywatnych.

[Obrazek: SsIndaG.jpg]
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości