problem using interrupts

Hello,
I'm trying to use interrupts to change a counter (indirectly).

Problem:
when button2(see code) is pressed a lot, the counter seems to reset (gets value 0).

also included serial monitor.

1.Am i doing this right?
2.Is my approach okay, are there any other ways to do what I want to do?

buttonTest.ino (1.23 KB)

see code)

You mean I've got to download it?
Why not just post it? (In code tags, of course. That goes without saying)

sorry, first time using this forum.
other forums use tools that make attachments visible.

//#include <avr/io.h>
//#include <avr/interrupt.h>
int button1=2;
int button2=19;
int button3=18;
volatile int counter=0;
volatile unsigned long Oldmillis = 0;
volatile unsigned long Oldmillis2 = 0;
volatile unsigned long Oldmillis3 = 0;
volatile boolean change = false;
volatile int bewerkingInt;
void setup()
{
	pinMode(button1,INPUT);
	pinMode(button2,INPUT);
	pinMode(button3,INPUT);
	attachInterrupt(digitalPinToInterrupt(button1), optellen, RISING);
	attachInterrupt(digitalPinToInterrupt(button2), aftrekken, RISING);
	attachInterrupt(digitalPinToInterrupt(button3), resetten, RISING);
	Serial.begin(9600);
}

void loop()
{
	if(change==true){
		bewerking(bewerkingInt);
		Serial.println(counter);
		change=false;
	}
}

void optellen(){
	if(millis()-Oldmillis>200){
		bewerkingInt=1;
		change=true;
	}
	Oldmillis=millis();
}
void aftrekken(){
	if(millis()-Oldmillis2>200){
		bewerkingInt=2;
		change=true;
	}
	Oldmillis2=millis();
}
void resetten(){
	if(millis()-Oldmillis3>200){
		bewerkingInt=3;
		change=true;
	}
	Oldmillis3=millis();
}
void bewerking(int bewerking){
	if(bewerking==1){
		counter++;
	}
	if(bewerking==2){
		counter=counter-1;
	}
	if(bewerking==3){
		counter=0;
	}
}

Is button2 resetting your sketch?

Add a print in setup() to see when you restart.

after editing the setup, button2 doesn't seem to be restarting the arduino.

I seem to have solved the problem by just using another interrupt pin(for button2).
(pin 3)

Which Arduino are you using? There's only 2 interrupts on the UNO.

You may also want to look at debouncing.

I presume this is on a MEGA which has interrupts on pins 2, 3, 18, 19, 20 and 21.
The code uses button3 on pin 18 to reset.
When you set the pinMode, use INPUT_PULLUP unless you have external pullups.
The interrupt should occur when you release the button.

Pete