Source Code - TanPitch/ButtonKing GitHub Wiki

Diagram

picture above : ButtonKing diagram chart (up to ButtonKing version 1.0.2)

ButtonKing Machine Stage

you can select the stage that you wants to read here, or just read through all stages.

Stage 0

  • stage that wait for button be pressed.
  • if button was pressed, stage will become 1.
  • source code
case 0: //Wait for 1st click
  if( digitalRead(Button_Pin) == LOW ){ Timer01 = millis(); Bt_Mode = 1; } //Button was pressed
break;

Stage 1

  • this stage will wait for button debouncing by delaying the time. (see setTimeDebounce)
  • after the delaying, ButtonKing will read the button input stage again.
    • if button was pressed, stage will become 2.
    • if button was released, stage will return to 0. (because after the debounce, button input stage could be he same as before debounce.)
  • source code
case 1: //Wait for debounce
  if(millis() - Timer01 >= debounce_time){
    if( digitalRead(Button_Pin) == LOW ){ Timer01 = millis(); Bt_Mode = 2; } //Button still pressed after debounce
    else if( digitalRead(Button_Pin) == HIGH ){ Bt_Mode = 0; } //Return
  }
break;

Stage 2

  • in this stage, ButtonKing will wait for a short period (see setTimeShort) to decide button be long pressed or just a simple click.
  • after waiting for a short time, ButtonKing will read the button input stage again.
    • if button still pressed, stage will become 3. (this means button start long press)
    • if button was released, stage will become 4. (just a simple click)
  • source code
case 2: //Wait for long press
  if(millis() - Timer01 >= short_time){
    if( digitalRead(Button_Pin) == HIGH ){ Timer01 = millis(); Bt_Mode = 4; } // Simple Click
    else{ Serial.println("Start 1st Button Pressing"); Timer01 = millis(); Bt_Mode = 3; } //Long Press
  }
  else{
    if( digitalRead(Button_Pin) == HIGH ){ Timer01 = millis(); Bt_Mode = 4; } // Simple Click
  }
break;

Stage 3

  • this stage, ButtonKing will wait for a short period (see setTimeLong) to give a button action to be a Long Pressed.
  • after waiting, ButtonKing will read the button input stage again.
    • if button still pressed, that means button has been pressed for long time. so ButtonKing will give the button Long Pressed.
    • if button released, button was given Long Press Stoped and return to stage 0.
  • source code
case 3:
break;

Stage 4

  • this stage will wait for next click (2nd click) to determine single click or double click that will call out in Stage 6.
  • ButtonKing will wait for a long time that called TimeDouble, and you can set it by using function setTimeDouble.
  • after waiting, ButtonKing will read the button input stage again.
    • if button was pressed again, that means 2nd pressed but it has to debounce before confirm by going to stage 5.
    • but if button does not press before time-out, that means no 2nd press so ButtonKing will give a button single click action and return to stage 0.
  • source code
case 4:
break;

Stage 5

  • this stage will wait for button debouncing like a stage 1 by delaying the time. (see setTimeDebounce)
  • after the delaying, ButtonKing will read the button input stage again.
    • if button was pressed, stage will become 6.
    • if button was released, stage will return to 0. (because after the debounce, button input stage could be he same as before debounce.)
  • source code
case 5:
break;

Stage 6

  • in this stage, ButtonKing will wait for a short period (see setTimeShort) to decide button be second long pressed or double click.
  • after waiting for a short time, ButtonKing will read the button input stage again.
    • if button still pressed, stage will become 7. (this means button start second long press)
    • if button was released, ButtonKing will give button a double click action and stage will become 0.
  • source code
case 6:
break;

Stage 7

  • this stage, ButtonKing will wait for a short period (see setTimeLong) to give a button action to be a 2nd Long Pressed.
  • after waiting, ButtonKing will read the button input stage again.
  • source code
case 7:
break;