Newbie - method to interrupt a count

Hi. Can someone please help me with a sketch I'm working on.

I have a push button and would like to increment the count as I press it. The same button held down for 1 second would reset it. I've got the counting working, but currently it's continuous and I want to be able to do single increments with my code if possible. Could someone please point me in the right direction. Can someone please point me in the right direction. Part code below.

void loop() {

  // home score - 2 x 7 segment displays - currently set to auto count as 1 second interval
  if (increment == 1 && currentMillis1 - previousMillis1 > interval) {
    if (a < 9) {
      a++;
    }
    else {
      a = 0;
      b++;
    }
    if (a == 0 && b == 10) {
      b = 0;
    }
    else {}
    shiftDisplay1();  // pass on the result to the first 2 shift registers
    previousMillis1 = currentMillis1;
  }
  
  // away score - 2 x 7 segment displays - currently set to auto count as 1 second interval
  if (currentMillis2 - previousMillis2 > interval/2) {
    if (c < 9) {
      c++;
    }
    else {
      c = 0;
      d++;
    }
    if (c == 0 && d == 10) {
      d = 0;
    }
    else {}
    previousMillis2 = currentMillis2;
    shiftDisplay2();  // pass on the result to the second 2 shift registers
  }

The second code below is for the buttons that I ended up writing up to incorporate above somehow for increment (quick press) and reset (long press). Trying to incorporate with above.

  currentMillis = millis();
  buttonState = digitalRead(buttonL);
  if (switchEnabled == 0 && buttonState == LOW && currentMillis - previousMillis > debounce)
  {
    buttonStart = millis();
    digitalWrite(ledL, HIGH);
    delay(10);
    digitalWrite(ledL, LOW);
    switchEnabled = 1;
    previousMillis = currentMillis;
  }

  if (switchEnabled == 1 && buttonState == HIGH && currentMillis - previousMillis > debounce) {
    buttonEnd = millis();
    if (buttonEnd - buttonStart > holdTime) {
      digitalWrite(ledL, HIGH);
      delay(1000);
      digitalWrite(ledL, LOW);
    }
    switchEnabled = 0;
    buttonStart = 0;
    buttonEnd = 0;
    previousMillis = currentMillis;

Once you have detected the button press, wait for the button to be released before continuing. In the wait loop use millis() to time the one second.

Weedpharma

    else {}

Why?

weedpharma:
Once you have detected the button press, wait for the button to be released before continuing. In the wait loop use millis() to time the one second.Weedpharma

I don't really understand. If I wait for the button to be released, it will still run the counting in the void loop. I'm wanting to use something like "while pressing a button and letting go, use the variable that it set as a condition to running the count in the void loop. Then in this loop I'd want to turn it off once it has run once". I just can't work out how to write that.

PaulS:
...else {} - Why?

Sorry - originally I thought you had to have an "else" with each if statement (i.e. if x, do y, else z). I've been indoctrinated by excel !!! Yes, it redundant code - I'll delete it. This sketch for me however is work in progress so it's not all "cleaned up" yet. Sorry for the confusion. I'll clean it up better in future before posting a question.

Ok, I've just worked out how to use a "while" loop, so I now should be able to stop and start parts of functions as needed.

Just another quick question if I may? How is the code read? Does it work in ladder format, starting from top and going all the way to the bottom (repeatedly) seeing what it can satisfy in each sweep?

Also, is there an initial sweep just for "reading"? If so, when it then carries out the actions, say 5 out of 10 could be carried out immediately after having read the code, can it carry out those 5 immediately and simultaneously?

The code starts at the first statement and then follows the logic as coded.

It is possible that in certain circumstances some code will never be read in the running program.

Weedpharma

weedpharma:
The code starts at the first statement and then follows the logic as coded. It is possible that in certain circumstances some code will never be read in the running program. Weedpharma

Is this the case for "switch" as well (as I haven't used this function)? From what I read (and correct me if I'm wrong), each switch case gets read, and if the condition for a variable satisfies any one switch case, that switch/break is run immediately, no matter what part of the sketch is currently being read (or even waiting on the dreaded "delay" variable?

Delta_G:
No, that's not right. You follow logically through the program. When you get to the switch statement, that's when it gets evaluated.

Interesting you should say that. I've just been working on a push button debounce code for a singl/double/long press. It currently has 6 "if" statements. For fault finding, I put in "Serial.println" line inside each "if" statement giving me some values. I also put one just after the end of each "if" statement. The way it read was:

  • Read first if statement. Satisfied and directed to 2nd if...

  • Read 2nd if. Not satisfied yet (due to debounce time), so instead read through 3rd, 4th, 5th, 6th whilst waiting, and keep looping till statisfied.

  • Read 2nd if again. Satisfied, and directed to 4th if...

  • Read 4th if. Not satisfied, so go through 5th, 6th, 6th, 1st, 2nd, etc, etc.

I learnt something new...

The code in this Thread has a simple system for different switch presses.

...R

People getting started with Arduinos may find the following discussion of use. It addresses "switch bounce", but has ramifications for what is being discussed in this thread....

Also, the following... which shows how a button can "interrupt" (in the sense used in everyday conversation, not as an electronics engineer would use it) something you have the computer doing....

Robin2:
The code in this Thread has a simple system for different switch presses.

Thanks for that link "R". I have seen it before. Initially I started another thread asking for suggestions on what switch library people recommended. After going through a stack, understanding the principles, I ended up writing my own. But that's for the suggestion.

tkbyd:
People getting started with Arduinos may find the following discussion of use. It addresses "switch bounce", but has ramifications for what is being discussed in this thread....

Thanks tkbyd. Very helpful links, but I have worked out how to interrupt my code by a pushbutton press setting a variable value that then satisfies an if statement elsewhere. This then resets at the end of the if statement each time. Hence I end up getting the "one shot" that I was after. So all good. Cheers.