Program stops after button press

Loop is executed very frequently. Instead of incrementing the counter when the switch is HIGH, you need to increment if it is HIGH and was LOW. In other words, a transition.

Don't do "== todelay", just do "> todelay"

Thanks all for your replies.
Doing > or >= indeed works perfectly.
However, I noticed that my LED blings really really poorly (like at 10% light emitting).
Whenever I hold 1 of the buttons, the LED seems to emit at pretty much 90%, which is good.
Would that be because I only lit it for 1 millisecond when the buttons are not pressed, or is there another explanation?

@Nick Gammon
Thanks for the reply. I don't really understand exactly what you mean, but it seems to alost work perfectly now :).

Your schematic is hard to follow. You should have a resistor in series with the LED.

For better help you need to draw what you have actually done.

Since I have no clue how to draw this correctly, I took a picture.
The picture is about 1 mega byte, so it could take a while to load.

The wiring for the switch doesn't look correct to me. Typically people wire like this, relying on the internal pull-up which you can enable:

This example code enables the pull-up and also detects the transition between the switch being up (and HIGH because of the pull-up) and pressed (thus, LOW).

const byte mySwitch = 8;

byte oldMySwitch = HIGH;

void setup ()
  {
  pinMode (mySwitch, INPUT_PULLUP);
  }  // end of setup

void loop ()
  {
  byte switchReading = digitalRead (mySwitch);
  
  if (switchReading != oldMySwitch)
    {
    if (switchReading == LOW)
      {
      delay (15);  // debounce
      
      // handle switch press here
        
      }  // end of switch down
      
    oldMySwitch = switchReading;
    } // end of switch changed
  }  // end of loop

@Nick Gammon
He has a resistor in there, I just can't tell if it is pull up or down though. You have to look very closely to see the connections, but it's there.

OK, but he still needs to detect the state change.

@Nick Gammon
Right, otherwise he'll go through 50 or so numbers (depending on size of program) with just one press.

OP, study Nick's sketch! It looks for when the switch/button's state changes from HIGH to LOW, (or vice versa), and only when it see that the state did change, it does an action.

Hi

Thanks for the replies!
To prevent the program going 50 numbers with 1 press, I have put the delay(100) there.
With trial and error, I concluded that 100 is perfect speed and I can still go up with only 1 when pressing and releasing quickly.

I will however study the code put above. Thanks a lot!

Er, yes, but if you hold the button down it will still slowly creep up (maybe you want that, eh?).

Indeed, the slow increments are exactly what I wanted :).
This is because if you want to set the BPM from 90 to 150, I don't want to push the button 60 times :).
I have tried some things in the meantime for the weird led behaviour, but it didn't work, I'll keep things posted.

Indeed, the slow increments are exactly what I wanted smiley.
This is because if you want to set the BPM from 90 to 150, I don't want to push the button 60 times

So, measure the time between discrete presses. A relatively long time means increment by 1. A medium amount of time means increment by 3. A very short time means increment by 5.

PaulS:

Indeed, the slow increments are exactly what I wanted smiley.
This is because if you want to set the BPM from 90 to 150, I don't want to push the button 60 times

So, measure the time between discrete presses. A relatively long time means increment by 1. A medium amount of time means increment by 3. A very short time means increment by 5.

No, it is now as I like it. Press 1 time, increase by 1. Keep pressing, keep going up rapidly. Why would you want to go up by 5 when you press short time and only by 1 if you keep it pressed in? I can't see a logical use for that :D.

try this

uint8_t Switch = 2;
uint8_t Led =13;

boolean LedState =LOW;
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;

unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;

void setup()
{
  pinMode(Switch,INPUT);
  digitalWrite(Switch,HIGH);
  pinMode(Led,OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  } 
  if ((millis() - LastDebounceTime) > DebounceDelay) 
  {
    if (CurrentSwitch != LastSwitchState) 
    {
      if (CurrentSwitch == LOW)
      {
        CountNumber++;
      } 
    }
    LastSwitchState=CurrentSwitch;
  }
  if ( !CurrentSwitch && !LastSwitchState) CountNumber++;
  Serial.println(CountNumber);
  LastSwitchDebounce = CurrentSwitch;
}

@ash901226
No, he wants this.

const int buttonPin = 2;     

int buttonState = 0;
int  lastReading = 0;
long onTime=0;
int count = 0;

void setup() { 
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);  
}

void loop(){ 
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastReading == LOW) { // single press
    onTime = millis();
    count++;
  }

  //held
  if (buttonState == HIGH && lastReading == HIGH) { //single press + hold
    if ((millis() - onTime) > 500 ) { // looks to see if button is pressed for more than half a second.
      delay(200); // **the rate in which the numbers increase**
      count++;
      lastReading = LOW;
    } 
  }
  Serial.println(count);
  lastReading = buttonState;
}

The code presented doesn't work.
Right now, the code I have is perfect, because that works perfectly for everything (keeping it pressed etc).
The only problem now must be in the circuit. Because when I press 1 of the buttons or both, the LED flashes faaaaar more brightly.
So I think that somewhere by pressing the button, the circuit gets closed and therefore more voltage flowsthrough the LED. I have no electronical knowledge, so there is my problem :P.
First I thought I didn't need a branch from the 5V for the LED or for the buttons, but I need it, or I have no parallel I think.

I have found what the problem was in the end.
The problem was that the LED was turned on and off too fast after another.
What I mean by that is that by the time the LED was set FROM HIGH to LOW, it barely had gotten time to go HIGH, resulting in only a super small blink.
I have added a delay which does not affect my timer code.
The sleep could not be bigger or equal than 60000 (ms) / BPM.

The resulting code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int BPM = 120;
int minSwitchState;
int plusSwitchState;
float toDelay;
long previousMillis = 0;
boolean buttonPressed;
int counter = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  updateDisplay();

  //red output LED
  pinMode(13, OUTPUT);
  //green output LED
  pinMode(10, OUTPUT);

  //input buttons
  pinMode(7, INPUT);
  pinMode(8, INPUT);
}

void loop() {
  buttonPressed = false;
  minSwitchState = digitalRead(8);
  plusSwitchState = digitalRead(7);
  
  if(minSwitchState == HIGH) {
    buttonPressed = true;
    BPM++;
    updateDisplay();
  }
  if(plusSwitchState == HIGH) {
    buttonPressed = true;
    if(BPM > 1) {
      BPM--;
      updateDisplay();
    }
  }
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= toDelay && !buttonPressed) {
    counter = counter % 4 + 1;
    if(counter == 1) {
      digitalWrite(10, HIGH);
    }else{
      digitalWrite(13, HIGH);
    }
    delay(100);
    previousMillis = currentMillis;
    Serial.println(currentMillis / 1000.0);
  }else{
    digitalWrite(13, LOW);
    digitalWrite(10, LOW);
  }
}

void updateDisplay() {
  toDelay = 60000.0 / (float) BPM;
  lcd.clear();
  lcd.print("Kies BPM: ");
  lcd.setCursor(0, 1);
  lcd.print(BPM);
  delay(100);
}

Thanks a lot all, this can be put to [SOLVED] :)!

There was something wrong with my code?, I thought it did EXACTLY what you wanted. You press it once, it increments once, you press and hold it and it increments at the delay(200) rate.

Ok, well if you got it to work on your own, then thats even better. You found the problem and was able fixed it.