Here is the code, with some extra stuff because I was just trying to experiment with buttons:
/* This is an example sketch provided to test the application of dual button commands
as well as the application of a requirement for the user to hold down certain button(s)
for a specific amount of time. */
int programmingHold = 2000;
unsigned long startHoldTime;
unsigned long time = millis();
int buttonOne = 11;
int buttonTwo = 12;
void setup()
{
Serial.begin(9600);
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
}
void loop()
{
int button = digitalRead(buttonOne);
Serial.print(button);
Serial.print(" | ");
Serial.print(time);
Serial.print(" | ");
Serial.print(startHoldTime);
Serial.print(" | ");
Serial.print(time - startHoldTime);
Serial.println();
delay(2);
if (button)
{
startHoldTime = time;
if (button && ((time - startHoldTime) >= programmingHold))
{
digitalWrite(13, HIGH);
}
}
else if (button == LOW)
{
startHoldTime = time;
if ((button == LOW) && ((time - startHoldTime) >= programmingHold))
{
digitalWrite(13, LOW);
}
}
}
The idea being that if you connect power (in this case I'm just using a jumper to connect pin 2 to pin 11, but the idea would be to use a button) for 2 seconds, it will turn the LED on pin 13 on, and if you remove power for more than 2 seconds, the LED will then shut off. The code is not working at all, currently, but even when I remove the timing aspects, as seen below, my serial monitor shows that pin 11 is reading as HIGH for several seconds after I remove power.
/* This is an example sketch provided to test the application of dual button commands
as well as the application of a requirement for the user to hold down certain button(s)
for a specific amount of time. */
int programmingHold = 2000;
unsigned long startHoldTime;
unsigned long time = millis();
int buttonOne = 11;
int buttonTwo = 12;
void setup()
{
Serial.begin(9600);
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
}
void loop()
{
int button = digitalRead(buttonOne);
Serial.print(button);
Serial.print(" | ");
Serial.print(time);
Serial.print(" | ");
Serial.print(startHoldTime);
Serial.print(" | ");
Serial.print(time - startHoldTime);
Serial.println();
delay(2);
if (button == HIGH)
{
startHoldTime = time;
digitalWrite(13, HIGH);
}
else if (button == LOW)
{
startHoldTime = time;
digitalWrite(13, LOW);
}
}
I'm very confused as to why the pin seems to still be getting power several seconds after I remove all connection from it. It has to be something I'm missing, but I'm just having a hard time understanding what is going on.