Press and hold button help

Can anyone help me with a small issue I'm having with a push button code.

I am trying to use two button to increase and decrease a value, and I've managed to that, but it only in-/decrease every time I push the buttons. I want it to automatically go up or down if I hold the buttons.
Let's say if I hold the button for more than 2 seconds, it should in-/decrease the value by 100 every 500 milliseconds.

Can anyone help me with the code or point me in the right direction?

insert a variable in your code that start counting every time you push the button. if a certain threshold is reached then you will increase the variable you want to set. in this way if you press and release the button the "dummy" variable will not reach the threshold and when you release you increase just by one. if you keep it pressed the dummy var will reach the threshold you set and the variable you want to change will start increase/decrease automatically. I used this system to have a button in a clock that will enter the "set" mode if pressed for more than 2 seconds.
hope this helps

Thank you for the fast reply. It helped a bit I think, but is it possible to dumb it down a bit? :slight_smile:
I'm pretty new at this :slight_smile:

Does anyone have a code for this? I'm stuck...

What is your budget?

My budget? How about just beeing friendly and help someone. That's the problem with the world... GREED...
Just forgot about it, I'll figure something out.

frodv:
My budget? How about just beeing friendly and help someone. That's the problem with the world... GREED...
Just forgot about it, I'll figure something out.

Now before you rant off you have, after all, come to the section called "Gigs and Collaborations" in this section people normally expect to get paid for their advice and help. There are other section in the forum where people offer help for free.

But in the spirit of helping here is my advice to you .....

i think one of the "bounce" libraries had a hold function so that it returned a state of up,down,hold. You could set the time before it switched state from down to hold in the setup.

Cheers Pete.

if you read well what I wrote you should be able to do it AND learn something. If you just ask for the code you will learn nothing. I was thinkin the main point of this forum is learning something

cloxart:
if you read well what I wrote you should be able to do it AND learn something. If you just ask for the code you will learn nothing. I was thinkin the main point of this forum is learning something

I get the count part to work, but only when I press and release it increase/decrease by 1 (or whatever I set it to), it's the auto up when I hold the button I can't figure out...

If you hold the button down for 2 seconds how fast do you then want to do a increment or a decrement?

You read the state of the button every X milliseconds.
Then ask the question is the button held and has it been held longer than 2 seconds.
If the above is true, you increment/decrement a variable by 1 or 2 or 3 . . .
Maybe you then run some code.
Maybe then you delay a bit.
Then you go back and read the button again . . .

For each line above, which one are you having trouble with?

It's :sleeping: time.

  1. Do you know what millis() is?

  2. What does "myTime" contain when the program finishes with this line of code?
    unsigned long myTime = millis();

  3. Assuming myTime = millis(); is in setup() and the next line of code is in loop().
    What is printed whenever this line of code is run?
    Serial.println(millis() - myTime);

LarryD:

  1. Do you know what millis() is?

  2. What does "myTime" contain when the program finishes with this line of code?
    unsigned long myTime = millis();

  3. Assuming myTime = millis(); is in setup() and the next line of code is in loop().
    What is printed whenever this line of code is run?
    Serial.println(millis() - myTime);

Thanks for trying to help me, but like I said, I'm a total noob and I do not understand this. The code I've got to work so far is mainly copy and paste from different codes... Just lucky I got it work I guess.
I understand most of my code now that I have it, because I like to play around with it and change settings to see what happens, thas how I learn....

The thing I'm trying to do is to make a controller to fill a tank of water in a brewery. I want to be able to set a spesific amount of water that runs through a flow meter and an electric valve, when the set water amount is reached, the valve closes. The hold button part that I'm having trouble with is to adjust the amount of water that will be filled in the tank, because this is different from one type of beer to another.
It will also be displayed on a lcd display, so that I know the value. It will also have a reset button.

Most of this I have got working. The flow meter, the LCD, the valve, the reset.... But, not the hold button to auto in-/decrease the amount of water.

One other thing I'm wondering if is possible to do with the code. Can it be set so it starts by the push of a button? I meen if I set the amount of water value, the valve wont open before I push the start button.

Some code I used in a LED project.
You should be able to use portions of it.
Do you want to learn how it works?

unsigned long lastDebounceTime;
unsigned long debounceDelay = 100UL;
unsigned long pushDelay;
unsigned long pushTime;

const byte upSwitch = 2; //LOW equals pushed
//let's start out in the none pushed state 
byte upSwitchLastSate = 1;
unsigned int myCounter = 0;

//=========================================================
void setup()
{  
  Serial.begin(9600);
  pinMode(upSwitch, INPUT_PULLUP);

  lastDebounceTime = millis();

} //               E N D   o f   s e t u p ( ) 

//=========================================================
void loop()
{
  //is it time to check the switches?
  if(millis() - lastDebounceTime > debounceDelay)
  {
    //note there is no glitch filter in this e.g.
    readSwitches(); 
    //get ready for the next switch time check
    lastDebounceTime = lastDebounceTime + debounceDelay; 
  }

  //              Other loop code goes here

} //              E N D   o f   l o o p ( ) 

//=========================================================
//                M Y    F U N C T I O N S
//=========================================================
void readSwitches()
{
  byte readSwitch = digitalRead(upSwitch);
  //has the switch state changed since the last time it was checked?
  if (readSwitch != upSwitchLastSate)
  {
    upSwitchLastSate = readSwitch; //yes it has
    
    //has the switch just been pressed?
    if(readSwitch == LOW)
    {
      myCounter = myCounter + 1;
      Serial.println(myCounter);
      //set the initial auto increment delay to 1 second
      pushDelay = 1000UL; 
      //set the starting time when the switch was first pressed
      pushTime = millis();
    } 
  }
  
  //has the switch been pushed for a period of time?
  if ( readSwitch == LOW && (millis() - pushTime >= pushDelay) )
  {
    myCounter = myCounter + 1;
    Serial.println(myCounter);
    //set time for the next check
    pushTime = millis();
    //set the subsequent auto increment delay to 1/4 second
    pushDelay = 250UL;
  }

  return;

} //      E N D   o f   r e a d S w i t c h e s ( ) 

//=========================================================


//=========================================================
//               E N D   O F   S K E T C H
//=========================================================

Thanks. Yes I would like to learn how it works. But now I at least have some idea, and bits to play around with. I'll give it a go and see what I can make happen.

About that last part I mentioned, do you know if it is possible to use a button to start, or perhaps I could use a statechange bit to just open the valve or something....

One other thing I'm wondering if is possible to do with the code. Can it be set so it starts by the push of a button? I meen if I set the amount of water value, the valve wont open before I push the start button.

I think I understand you but you will need to flesh this out a bit more

I got it to work on my own... I did not use any of the suggested codes.

Many hours and cups of coffee, but I figured it out. Not bad for a beginner huh? :slight_smile:

I did not use any of the suggested codes.

Good for you.

Do you understand how the code I posted works?

.

LarryD:
Good for you.

Do you understand how the code I posted works?

.

Not really, I have not looked at it much. If you want to explain it to me, maybe I can try it. If not, that's fine too.

Try the sketch to see what it does.

LarryD:
Try the sketch to see what it does.

Will do... I'll post when I've tried it...