AWOL:
Have a look at, and more importantly play with, the blink without delay example in the IDE, without delay.
ok from your code its hard to transfer what i have done,
i dont know if its better to make another topic or just post it here, i have just added a sensor.
if the analog pir sensor has an output and it trigers in 3 seconds just like the switch before. the led 13 must turned on.
this is the working code with delay.
// Uses a PIR sensor to detect movement, buzzes a buzzer
int ledPin = 13; // choose the pin for the LED
int x = 0;
int inputPin = A0; // choose the input pin (for PIR sensor)
// we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
}
void loop(){
val = analogRead(inputPin); // read input value
if (val < 1) { // check if the pir analog has trigerd (i just put one because the analog pir sensor produces
morethan 1 ohms when it detects something),.
x++;
delay(1000);
if (x==3){
digitalWrite(ledPin, HIGH); // turn LED ON
x=0;
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
x=0;
delay(300);
}
}
this code worked, the light turns on when the sensor sens a person in 3 seconds.
without delay style
, implemented to your code.
const int pirbuttonPin = A0;
const int ledPin = 13;
int buttonState = false;
int lastReading = false;
long onTime = 0;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop(){
buttonState = analogRead(pirbuttonPin);
if (buttonState == true && lastReading == false) {
onTime = millis();
}
//held
if (buttonState == true && lastReading == true) {
if ((millis() - onTime) > 3000 ) { //(current time - first pressed time) must be greater than 3000 (3 seconds)
digitalWrite(ledPin, HIGH);
lastReading = LOW; //reset state
}
else {
digitalWrite(ledPin, LOW);
}
}
lastReading = buttonState; //write button to lastreading for compare
}
it did not worked, what is wrong with my code? hope some one helps me.