Push Button with two functions

[quote author=pe lang link=topic=131076.msg985837#msg985837 date=1352238198]
hi Arrch,

you right, but my question is where to start. The basic code is form the button example. My problem is how the same button can turn of the LED within the delay time. Sure the code "stuck" by the delay.... this will not work

i try again to explain:
I have a LED which i can turn on with a Pushbutton. The LED will be on for, lets say 10min and will then turn off automatically. Here my problem: I would like to be able to switch off with the same Pushbutton the LED within the 10min. [/quote]

So you need to replace Delay with the proper usage of millis(). For an example on how to use millis(), see the Blink Without Delay example. You probably also want to use the button as a toggle, rather than a switch, so you will need to detect the signal edges of the button. Here is an example that integrates Button state change, millis() and even debounce for good measure:

/*
 * Sample sketch to show how to detect the state changes of a momentary switch (button). 
 * Debounce is also implemented without the use of delay and the length that the button
 * is pushed down for is printed to the serial monitor.
 */
 
const int buttonPin = 2;
const unsigned long debounceInterval = 20; // Increase until bouncing stops

unsigned long debounceTime = 0;
unsigned long buttonPushedDownTime = 0;
int lastReading = HIGH;
int lastState = HIGH;
int currentState = HIGH;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println("[start]");
}

void loop
{
  // Store the current time in a variable for easy access
  unsigned long currentTime = millis();
  // This variable will be used to store the length of time that the button was depressed for
  unsigned long lengthButtonWasDown = 0;
  // Start by getting the current reading from our button
  int currentReading = digitalRead(buttonPin);
  // If it's different than our last reading, reset the debounce timer
  if (currentReading != lastReading)
    debounceTime = currentTime;
  // If the button has stayed at the same reading for long enough, update the button's state
  if (currentTime - debounceTime > debounceInterval)
    currentState = currentReading;
  // At this point, the debounce is taken care of, we just need to look for the signal edges
  // of the button. That is, when the button goes from LOW to HIGH or HIGH to LOW.
  if (currentState != lastState)
  {
    // First we check if this which transistion we have. If the button is pushed down, then
    // the currentState will be LOW. Otherwise, the button is being released.
    if (currentState == LOW)
    {
      Serial.println("Button pushed down");
      buttonPushedDownTime = currentTime;
    }
    else
    {
      // Calulate how long it was pushed down
      lengthButtonWasDown = currentTime - buttonPushedDownTime;
      // Print the results to the Serial monitor
      Serial.print("Button realeased after ");
      Serial.print(lengthButtonWasDown);
      Serial.print(" seconds");
      
    }
  }
  
  // Update the "last" variables
  lastReading = currentReading;
  lastState = currentState;
}