macgeek:
int ledPin = 13; // LED connected to digital pin 13
int potPin = 0; // white doorbell wire to analog pin 0
int val = 0;
long time = 0;
long debounce = 5000;
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600); // open serial port at 9600 baud
digitalWrite(14 + potPin, HIGH); // set pullup on the analog pin
// (analog 0 = digital 14, a1 = d15, etc)
}
void loop() {
val = analogRead(potPin);
if (val < 50) { // if the circuit is completed
// (for me, it generally drops from 1023 to ~ 15 when 'ringing')
if (millis()-time > debounce) {
Serial.println("A");
digitalWrite(ledPin, HIGH); // sets the LED on
delay(5000); // ... 240000 = 4 mins
digitalWrite(ledPin, LOW); // and turns the LED off
time = millis();
}
}
}
What seems to be happening, is that I have to push the button 3 times to get it to send the "A" to the serial Port,
I played with the delay(5000) to have it ignore the input, but It doesnt seem to be working...
Any ideas?
Change "long time" to "unsigned long time".
On the first pass through your loop, you are trying this
if (millis()-time > debounce)
millis() starts at 0 and counts up, and time starts at 0. So you are waiting until millis() is greater than debounce(5000) before you actually set the fact that the button has been pressed for the first time.
With the current logic, when you first start up, wait 5 seconds after it starts before you try hitting the button and test. You can rearrange your logic to fix this, but this would be generally fine after the first 5 seconds.