Untested
#define buttonPin 2 //button attached to pin 2
#define event1 13 //ssr to be connected to pin 9 for lights
int buttonState = 0;//variable to store the state of button
unsigned long eventtime1 = 0;//clock value to be stored at the time milli() is called
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(event1, OUTPUT);
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH && eventtime1 == 0)//when button is pushed...
{
eventtime1 = millis() + 500; //Now, plus 500ms
digitalWrite(event1, HIGH); //turn on pin 13
}
if ((millis() >= eventtime1) && (eventtime1 != 0))
{
eventtime1 = 0;
digitalWrite(event1, LOW); //turn off pin 1
}
}
Shorter still untested
#define buttonPin 2 //button attached to pin 2
#define event1 13 //ssr to be connected to pin 9 for lights
unsigned long eventtime1 = 0; //clock value to be stored at the time milli() is called
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(event1, OUTPUT);
}
void loop()
{
if (digitalRead(buttonPin) == HIGH && eventtime1 == 0)//If button is pushed, and eventtime1 is 0 then...
{
eventtime1 = millis() + 500; //Set eventtime1 to now + 500ms
digitalWrite(event1, HIGH); //turn on event1 pin
}
if ((millis() >= eventtime1) && (eventtime1 != 0)) //If eventtime1 is now or later than now, and eventtime1 isn't 0, then...
{
eventtime1 = 0;
digitalWrite(event1, LOW); //turn off event1 pin
}
}
Another untested
#define buttonPin 2 //button attached to pin 2
#define event1 13 //ssr to be connected to pin 9 for lights
unsigned long eventtime1 = 0; //clock value to be stored at the time milli() is called
void setup()
{
pinMode(event1, OUTPUT);
attachInterrupt(0, btnDown, RISING); //0 = digital pin 2 or 1 = digital pin 3
}
void loop()
{
if ((millis() >= eventtime1) && (eventtime1 != 0)) //If eventtime1 is now or later than now, and eventtime1 isn't 0, then...
{
eventtime1 = 0;
digitalWrite(event1, LOW); //turn off event1 pin
}
}
void btnDown()
{
if (eventtime1 != 0) return;
eventtime1 = millis() + 500; //Set eventtime1 to now + 500ms
digitalWrite(event1, HIGH); //turn on event1 pin
}
Last untested
#define buttonPin 2 //button attached to pin 2
#define event1 13 //ssr to be connected to pin 9 for lights
unsigned long eventtime1 = 0; //clock value to be stored at the time milli() is called
void setup()
{
pinMode(event1, OUTPUT);
attachInterrupt(0, btnDown, RISING); //0 = digital pin 2 or 1 = digital pin 3
}
void loop()
{
if ((millis() - eventtime1 >= 500) && (eventtime1 != 0)) //If eventtime1 is now or later than now, and eventtime1 isn't 0, then...
{
eventtime1 = 0;
digitalWrite(event1, LOW); //turn off event1 pin
}
}
void btnDown()
{
if (eventtime1 != 0) return;
eventtime1 = millis(); //Set eventtime1 to now + 500ms
digitalWrite(event1, HIGH); //turn on event1 pin
}
They should all do the same (if they work at all), just different ways of doing it.