do one thing if the state is already set and another if it isn't

This really shouldn't be as hard as I'm making it! :)

I've looked at BlinkWithoutDelay and Googled and Googled but I can't find a tutorial for something simple like the above. Only blinking. Which I don't want. :)

I want to use a single button press to turn an LED on for 10s then shut it off.

Does this code look right theory-wise even if it may not be perfect syntax wise? I'm at work so can't compile to test.

#define ButtKitchen 3
#define LEDKitchen 9

const unsigned long LEDOnTime = 10000;
unsigned long TimeSnapShot;


void setup()
    {
    pinMode(ButtKitchen,INPUT);
    pinMode(LEDKitchen,OUTPUT);
    }

void loop()
    {
    unsigned long currentMillis = millis();
    TimeSnapShot = currentMillis
    if (ButtKitchen == HIGH)
        {
        if (TimeSnapShot + currentMillis > LEDOnTime) {
            digitalWrite(LEDKitchen, HIGH );
        }
    else
        {
        digitalWrite(LEDKitchen, LOW );
        }
    }

er no you are setting TimeSnapShot and currentMillis each time inside loop() so nothing will ever change

so declare the variables outside of loop() set TimeSnapShot when you turn on the LED

just do compares inside loop()

Late, but my 2c anyway

Does this code look right theory-wise

No. There are a number of issues: Buttkitchen (not a stellar choice of name IMHO) is a pin number, you are using it as if it holds the state of the pin - you should be using digitalread. You give timesnapshot and currentmillis the same value based on millis, sum them and check whether they are bigger than 10,000. As your sketch runs and millis rises, this will always be true, so once the Buttkitchen issue is fixed, and 5 seconds have passed, the LED would simply reflect the state of the button.

You can do it with a variable that tells you what state the led is in.

if the led is off, check for a button press if the button is pressed, note the time, set the ledstate variable, turn on the led else (led is on) if difference between millis and the time you noted when you turned the led on is more than 10 seconds, set the ledstate value, turn led off,

Well no, all your checking and led on/off stuff only happens when the button is pressed. Furthermore, whenever it is pressed, you restart the timing interval.

If you’re still having trouble, it may be time for a dose of PaulS’ patent medicine: Figure out how you would perform this task yourself, equipped with a watch and a notepad. Millis is your watch, variables are your notepad. Once you have specified, in painful detail, what steps you have to perform to do it, translate that into code. Alternatively, re-read reply #3.

I think I may have cracked it??

Perhaps not. Looking at that code, consider what will happen when the sketch first starts running, if the buttonPin is LOW.

PaulS: Perhaps not. Looking at that code, consider what will happen when the sketch first starts running, if the buttonPin is LOW.

How about this then?

const int ledPin =  4;      // the number of the LED pin
const int buttonPin 3       // the number of the button pin


int buttonState = LOW;          // buttonState used to set the button state
int ledState = LOW;     // buttonState used to set the LED state
long interval = 10000;          // time to keep the LED on for (milliseconds)
long previousMillis = 0;        // will store last time LED was updated

void setup() {
pinMode(ledPin, OUTPUT);      
pinMode (buttonPin, INPUT);      
}

void loop()
{

unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH)
    {
    ledState = HIGH;
    previousMillis = currentMillis; 
    buttonState = LOW;
    }

if (ledState == HIGH)
    {
    digitalWrite(ledPin, HIGH);
    }

if (previousMillis + interval >= currentMillis) {
    ledState = LOW;
    }
}

How about this then?

As opposed to the code you deleted? Please don't do that.

I went back and read what you wanted to do, which was to turn the LED on when the switch was pressed, and off again 10 seconds later. The code you deleted will (I think, I can't check) do that.

I was confusing you with someone else that wanted to turn the LED on 10 seconds after the switch was pressed.

Closer, but you never turn the led off. Also, I note that the timer starts when you release the button, rather than when you press it - may be your intention of course. In addition, the timer starts again if you press the button during the 10 seconds the lid is (supposed to be) lit - may be intentional too.

PaulS:

How about this then?

As opposed to the code you deleted? Please don't do that.

Sorry Paul. I can see why it might be undesirable to delete, but I didn't want to clutter the thread with lots of faulty code in a short space of time. P.S. I won't do it again. Don't want to bite the hand that feeds. Not even nibble.

#define ledPin 8      // the number of the LED pin
#define buttonPin 2       // the number of the button pin

int buttonState = LOW;          // buttonState used to set the button state
int ledState = LOW;		// buttonState used to set the LED state
long interval = 5000;          // time to keep the LED on for (milliseconds)
long previousMillis = 0;        // will store last time LED was updated

void setup() {
  pinMode(ledPin,OUTPUT);      
  pinMode(buttonPin,INPUT);      
}

void loop()
{

unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);

if (buttonState == LOW)
	{
	ledState = HIGH;
	previousMillis = currentMillis;	
	buttonState = LOW;
	}

if (ledState == HIGH)
	{
	digitalWrite(ledPin, HIGH);
	}

if (previousMillis + interval < currentMillis) {
	digitalWrite(ledPin, LOW);
        ledState = LOW;
	}
}

I’ve got there…in a way. I don’t think the code is at all efficient and I’m not at all sure I’m doing things in the right place…but it works and I fixed it myself WITH LOTS OF HELP from you guys.

The circuit does what I want, now…

Weird thing though lads, with a previous version of the code (only slightly different from the above) at the 5000ms mark, the LED light intensified! I can’t work that one out…it started off bright and then at 5000ms got maybe double so bright again! Maybe this is because up until that point it was flickering on and off so quickly I couldn’t see? And at 5000ms it was just…“on”?

Weird thing though lads, with a previous version of the code (only slightly different from the above) at the 5000ms mark, the LED light intensified! I can't work that one out...it started off bright and then at 5000ms got maybe double so bright again! Maybe this is because up until that point it was flickering on and off so quickly I couldn't see? And at 5000ms it was just...."on"?

The flickering theory sounds quite likely - particularly as one version of your code was adding millis to itself and checking against 10,000ms, so a change at 5s is more than plausible.

How does this look for code to do one thing if the button is pressed and the LED isn’t on, and another if button pressed and it is? Is the code as efficient and correct as it can be, now? There’s no snazzy trick I’m missing for optimising and shortening it? I’ve seen some amazing one liners where the tutorials use four lines.

THANK YOU

P.S. The blink code isn’t inserted yet.

#define ledPin 8      		// the number of the LED pin
#define buttonPin 2		// the number of the button pin

int buttonState = LOW;		// used to set the button state
int ledState = LOW;		// used to set the LED state
int ledStateBlink = LOW;	// used to set the LED state if LED was already on
long interval = 5000;		// time to keep the LED on for (5s)
long intervalBlink = 2000;	// time to keep the LED on for (2s)
long previousMillis = 0;	// will store last time LED was updated
long previousMillisBlink = 0;	// will store last time LED was updated

void setup() {
  pinMode(ledPin,OUTPUT);      
  pinMode(buttonPin,INPUT);      
}

void loop()
	{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);

if (buttonState == LOW && ledState == LOW){ 			// If the button's pressed and LED is not on
	ledState = HIGH;
	previousMillis = currentMillis;	
	buttonState = LOW;
	}

if (buttonState == LOW && ledState == HIGH){ 			// if the button's pressed and LED is on
	ledStateBlink = HIGH;
	ledState = LOW;
	previousMillisBlink = currentMillis;	
	buttonState = LOW;
	}

if (ledState == HIGH){						// if the LED status has been set to on
	digitalWrite(ledPin, HIGH);
	}
	else{
	digitalWrite(ledPin, LOW);
	}

if (ledStateBlink == HIGH){					// if the LED status has been set to flash
	digitalWrite(ledPin, HIGH);				// Blink Code to go here.
	}
	else{
	digitalWrite(ledPin, LOW);
	}

if (previousMillis + interval < currentMillis) {		// If the predefined interval has elapsed for the first button press
        ledState = LOW;
	}

if (previousMillisBlink + intervalBlink < currentMillis) {	// If the predefined interval has elapsed for the second button press
        ledStateBlink = LOW;
	}
}

I want to use a single button press to turn an LED on for 10s then shut it off.

Have you tried this yet? Does it do what you said you want it to do?