[Solved] HoldButton in AnalogButton ?

I want to know if its posible, and how, I tried with ''while'' function, but cant get it working fine. I found that holdbuttons is easy to use in digitalreading, but cant found anything about analogbutton system.

If you want my code, thats it:

/*
The Circuit:
 
         Analog pin
            |
Ground--1K--|--------|--------|-------|-------|-------|-------|-------|
            |        |        |       |       |       |       |       |
           btn1     btn2     btn3    btn4    btn5    btn6    btn7    btn8
            |        |        |       |       |       |       |       |
         220 Ohm  390 Ohm  680 Ohm   1.0K    1.5K    2.2K    4.7K    10K
            |--------|--------|-------|-------|-------|-------|-------|-- +5V

*/
int j = 1;
int Button[8][3] = {{1, 834, 845}, // button 1
                     {2, 730, 741}, // button 2
                     {3, 600, 611}, // button 3
                     {4, 505, 516}, // button 4
                     {5, 403, 414}, // button 5
                     {6, 315, 326}, // button 6
                     {7, 172, 183}, // button 7
                     {8, 84, 95}, // button 8
};
int analogpin = 1; // analog pin to read the buttons
int label = 0;  // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0;  // the last time the output pin was sampled
int debounce_count = 25; // number of millis/samples to consider before declaring a debounced input
int current_state = 0;  // the debounced input value
int ButtonVal;

int jB = 1;
int ButtonB[8][3] = {{1, 834, 845}, // button 1
                     {2, 730, 741}, // button 2
                     {3, 600, 611}, // button 3
                     {4, 505, 516}, // button 4
                     {5, 403, 414}, // button 5
                     {6, 315, 326}, // button 6
                     {7, 172, 183}, // button 7
                     {8, 84, 95}, // button 8
};
int analogpinB = 2; // analog pin to read the buttons
int labelB = 0;  // for reporting the button label
int counterB = 0; // how many times we have seen new value
long timeB = 0;  // the last time the output pin was sampled
int debounce_countB = 25; // number of millis/samples to consider before declaring a debounced input
int current_stateB = 0;  // the debounced input value
int ButtonValB;

void setup()
{
  Serial.begin(9600); 
  Keyboard.begin();
}

void loop()
{
  if (millis() != time)
  {
    ButtonVal = analogRead(analogpin);
    if(ButtonVal == current_state && counter >0)
    { 
      counter--;
    }
    if(ButtonVal != current_state)
    {
      counter++;
    }
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = ButtonVal;
      if (ButtonVal > 0)
      {
        ButtonCheck();
      }
    }
    time = millis();
  }

  if (millis() != timeB)
  {
    ButtonValB = analogRead(analogpinB);
    if(ButtonValB == current_stateB && counterB >0)
    { 
      counterB--;
    }
    if(ButtonValB != current_stateB)
    {
      counterB++;
    }
    if (counterB >= debounce_countB)
    {
      counterB = 0;
      current_stateB = ButtonValB;
      if (ButtonValB > 0)
      {
        ButtonCheckB();
      }
    }
    timeB = millis();
  }

}

void ButtonCheck()
{
  for(int i = 0; i <= 8; i++)
  {
    if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
    {
      label = Button[i][0];
      Action();      
    }
  }
}

void ButtonCheckB()
{
  for(int iB = 0; iB <= 8; iB++)
  {
    if(ButtonValB >= ButtonB[iB][jB] && ButtonValB <= ButtonB[iB][jB+1])
    {
      labelB = ButtonB[iB][0];
      ActionB();      
    }
  }
}

void Action()
{
  if(label == 1)
  {
    Keyboard.press('q');
    delay(25);
  }
  if(label == 2)
  {
    Keyboard.press('w');
    delay(25);
  }
  if(label == 3)
  { 
    Keyboard.press('a');
    delay(25);
  }
  if(label == 4)
  {
    Keyboard.press('s');
    delay(25);
  }
  if(label == 5)
  {
    Keyboard.press('z');
    delay(25);
  }
  if(label == 6)
  {
    Keyboard.press('y');
    delay(25);
  }
  if(label == 7)
  {
    Keyboard.press('e');
    delay(25);
  }
  if(label == 8)
  {
    Keyboard.press('r');
    delay(25);
  }  
  Keyboard.releaseAll();
  delay(25);
}

void ActionB()
{
  if(labelB == 1)
  {
    Keyboard.press('d');
    delay(25);
  }
  if(labelB == 2)
  {
    Keyboard.press('u');
    delay(25);
  }
  if(labelB == 3)
  { 
    Keyboard.press('v');
    delay(25);
  }
  if(labelB == 4)
  {
    Keyboard.press('j');
    delay(25);
  }
  if(labelB == 5)
  {
    Keyboard.press('g');
    delay(25);
  }
  if(labelB == 6)
  {
    Keyboard.press('h');
    delay(25);
  }
  if(labelB == 7)
  {
    Keyboard.press('b');
    delay(25);
  }
  if(labelB == 8)
  {
    Keyboard.press('n');
    delay(25);
  }  
  Keyboard.releaseAll();
  delay(25);
}

Not sure what you are asking here.
Thanks for the code, now what does it do and what do you want it to do?

I have a 16 buttons keypad, configured as AnalogButtons using just 2 analog pin (8 buttons / pin).
They are working as push buttons and are working fine.

I want hold buttons, a switch that you can hold down and arduino hold it down until you release it.

I tryed ''while'' function, but it hold the button forever, not until I release the switch. I saw other ways, but they all are for digital input, and I cant use them, or atleast I dont know how to.

Read the button.
If it's down, set a boolean variable to indicate it's down.
Save the value read from the analogue pin
Keep reading the buttons.
As long as the same button is down, leave the boolean variable alone.
If it changes to show that no button is down, set the boolean to show it's up.

It will help if you split the read button into a function that returns the button number if one is pressed and something like -1 when there is nothing pressed. Call it readButton.
Then a simple

while(readButton() != -1) { }

Will hold until released

I have it working with this one on every button I want that being holded:

  if(labelB == 2)
  {
    val = analogRead(analogpinB);
    while (val > 10)
    {
      Keyboard.press('u');
      val = analogRead(analogpinB);
    }

That will give you a rather rapid auto repeat, is that what you want?

Yes, is exactly what I want, it works like a charm now :slight_smile:
I know is not the same as holding the button, but for what I want is the same holding the button or pressing it quickly multiple times (like on notepad if you hold a character-key it repeats quickly).

Thanks you for your help.