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
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 timevoid 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 outputvoid 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;)