Adding a Number to a Variable (once(!)) on the press of a Button

Hi,

I'm sorry, this is probably a simple thing to do, but I've been sitting on it for hours now an I can't get it to work. The program should work as a timer and when a button is pressed, a Pin is switched high for the desired time. This I got to work :wink:

But I have two other buttons, pressing one should add half a second, pressing the other substract half a second. But whenever I push one of the buttons, the addition is very quickly repeated again and again and the time is not really controllable. How can I do this right?

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int testPin = 8; //Relais
int mehrPin = 3; //more time
int wenigerPin = 4; //less time

void setup()
{
pinMode(13,OUTPUT); // LED output
pinMode(2,INPUT_PULLUP); // Button input
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(8,OUTPUT); // Relais as output
}

int buttonState = 0; // variable for reading the pushbutton status
int mehr = 0;
int weniger = 0;
unsigned long addition = 0;
unsigned long mahlzeit = 1000; //initial duration of output

void loop()
{
static unsigned char ledState = LOW;
static unsigned char buttonState = LOW;
static unsigned char lastButtonState = LOW;
static unsigned long ledCameOn = 0;
{
if(digitalRead(3) == LOW)
{

for(int i=0; i < 2; i++)
{
if(i==1)
{
addition += 500; //if button is pressed increment the duration by 500
}
}
}

{
if(digitalRead(4) == LOW)
{
for(int x=0; x < 2; x++){
if(x==1)
{
addition -= 500; //if button is pressed decrement the duration by 500
}
}
}

}

buttonState = digitalRead(2);

if(ledState == HIGH)
{
if(millis()-ledCameOn > (mahlzeit + addition))
{
digitalWrite(13,LOW);
digitalWrite(8,LOW);
ledState = LOW;
}
}
if(buttonState != lastButtonState)
{
lastButtonState = buttonState;
if((buttonState == LOW) && (ledState == LOW))
{
digitalWrite(13,HIGH);
digitalWrite(8,HIGH);
ledState = HIGH;
ledCameOn = millis();
}
}

}
}

Thank You very much for helping out a newbie;)

When i add delays it's more controllable, but there must be a more elegant solution;)

You are changing the value of the variable while the button is pressed not when the button becomes pressed. You need to change the program so that it only changes the variable when the button changes from not pressed to pressed. As a guide to doing this look at the state change detection example in the IDE.

When you post code please put it in code tags not quote tags as it makes it easier to read.

Thank You, I will look into that and report:)

Thanks again, I got it to work, the solution was right before my eyes the whole time :smiley:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int testPin = 8;      //Relais
const int mehrPin = 3;      //more time
const int wenigerPin = 4;   //less time

void setup()
{
  pinMode(13,OUTPUT); // LED output
  pinMode(2,INPUT_PULLUP); // Button input
  pinMode(mehrPin,INPUT_PULLUP);
  pinMode(wenigerPin,INPUT_PULLUP);
  pinMode(8,OUTPUT); // Relais as output
  Serial.begin(300);
}

int buttonState = 0;         // variable for reading the pushbutton status
int mehr = 0;
int weniger = 0;
int mehrState = 0;
int wenigerState = 0;
int lastmehrState = 0;
int lastwenigerState = 0;
unsigned long addition = 0;
unsigned long mahlzeit = 1000; //initial duration of output

void loop()
{
  static unsigned char ledState = LOW; 
  static unsigned char buttonState = LOW;
  static unsigned char lastButtonState = LOW;
  static unsigned long ledCameOn = 0;
 mehrState = digitalRead(mehrPin);
  if(mehrState != lastmehrState) {
    if(mehrState == LOW) {
      addition += 500;
    }
  }
  lastmehrState = mehrState;
wenigerState = digitalRead(wenigerPin);
  if(wenigerState != lastwenigerState) {
    if(wenigerState == LOW) {
      addition -= 500;
    }
  }
  lastwenigerState = wenigerState;
  buttonState = digitalRead(2);
 if(ledState == HIGH)
  {
    if(millis()-ledCameOn > (mahlzeit + addition))
    {
      digitalWrite(13,LOW);
      digitalWrite(8,LOW);
      ledState = LOW;
    }
  }
  if(buttonState != lastButtonState)
  {
    lastButtonState = buttonState;
    if((buttonState == LOW) && (ledState == LOW))
    {
      digitalWrite(13,HIGH);
      digitalWrite(8,HIGH);
      ledState = HIGH;
      ledCameOn = millis();
    }
  }
  Serial.println(addition);
}