for some reason my delay loop is not cycling. it should stay on for 10 sec then off for 5 then back on... any thoughts?
[code]const int LEDS = 13; // choose the pin for the LED
const int MERS = 10;
const int inputPin = 7; // choose the input pin (for the PIR sensor)
void setup() {
pinMode(LEDS, OUTPUT); // declare LED as output
pinMode(MERS, OUTPUT);
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(LEDS, HIGH); // turn LED on if motion detected
{
digitalWrite(MERS, HIGH); // turn MERS on (HIGH is the voltage level)
delay(10000); // wait for a 10 second
digitalWrite(MERS, LOW); // turn MERS off by making the voltage LOW
delay(5000); // wait for 5 second
}
}
}
[/code]
MERS is going HIGH and LOW. LED I dont know what you want to do with.
/T
I need to cycle the MERS.... it is a driveway sensor that I want to contiue to say alert unitl I turn it off.... basically the cycle of alerts goes for about 10 seconds and turns off.... so i want it to turn the MERS off and then wait 5 seconds and then turn it back on the cycle it again...
It looks like your code is turning the "MERS" on and off. Connect a LED instead of "MERS" to pin 10 to validete. Your loop is only cycling once every 15 seconds.
what does it actually do when you run this sketch ?
I actually have LED's in 13 and 10 for testing purposes. When I run the sketch the sensor turns on 13 / LEDs as it should as well as 10/MERS. MERS stays on for 10 sec and then turns off. But it never comes back on like I want it to. I want it to continue to cycle until i reset.
Its something with your IF and brackets. The whole lot is in IF statement as it looks from here. Try to edit and repaste as CODE, easier to read that way.
You're reading the inputPin and if it's high you do something, if it is low you do nothing. I am assuming that inputPin is a momentary switch so after the first cycle completes nothing happens unless inputPin is high again. You either need to latch the input, or use a mantained input, or write another if statement to do something when the input is low.
If the inputPin is a button, there's a possiblity it needs to be de-bounced either with hardware or within the sketch.
found one problem.... left a " ; " out after the if statement.... now the problem is it is coming on without motion on the sensor. I am working on de bounce but am lost! I want the sensor beam to be broken for 4 seconds before any of the process starts.
The brackets associated with the OF statement does not look right.
kutterato:
found one problem.... left a " ; " out after the if statement.... now the problem is it is coming on without motion on the sensor. I am working on de bounce but am lost! I want the sensor beam to be broken for 4 seconds before any of the process starts.
Is the intention to only turn on the LED when inputPin is high and cycle MERS regardless of the state of inputPin? If so you need to clean up the brakcets so that only turning on the LEDS is within brackets:
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH){ // check if the input is HIGH
digitalWrite(LEDS, HIGH); // turn LED on if motion detected
}
digitalWrite(MERS, HIGH); // turn MERS on (HIGH is the voltage level)
delay(10000); // wait for a 10 second
digitalWrite(MERS, LOW); // turn MERS off by making the voltage LOW
delay(5000); // wait for 5 second
}
If you want the LEDS to turn on and MERS to cycle whenever inputPin is high and not when inputPin is low, you need to clean up the brackets so that the apropriate lines of code are contained within the brackets:
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH){ // check if the input is HIGH
digitalWrite(LEDS, HIGH); // turn LED on if motion detected
digitalWrite(MERS, HIGH); // turn MERS on (HIGH is the voltage level)
delay(10000); // wait for a 10 second
digitalWrite(MERS, LOW); // turn MERS off by making the voltage LOW
delay(5000); // wait for 5 second
}
}
If you want to turn on the LEDS when inputPin is high then cycle MERS until you acknowledge you'll need another input or you'll need to develop some way of stopping MERS using the existing input.
de-bounce might not be necessary, what is the hardware connected to inputPin? It's really a matter of housekeeping to debounce the input, the program will still work, but might give inconsistent results when a button is pressed. This is only true of mechanical switches, if the input is a transistor or some other solid state input, debouncing isn't typically necessary.
basically the idea of all of this is to have photo electric beam between two sensors and once the beam is broken it starts the entire process with a strobe light and setting off the MERS alarm. I want the beam to have to be broken for 3-4 seconds so that it takes something like a car to break it rather that a human or animal walking through it. That is why I figured I would have to have a debounce built into all of this. But once the beam is broken I want it to stay open until I physically go and reset it (turn system on and off for reset and make sure that the sensor beam is not broken. The only piece I have to figure out now is the debounce so that the beam has to be broken for 4 seconds..
I think I understand what you're looking for between this and the other thread you started. I assume the photo beam uses a solidstate switch so debouncing isn't an issue, although if it is a mechanical switch it's really of no consequence. Although it might be a misnomer to call the 4 second noise filter a debounce, the code is the same you just use a 4 second delay time instead of 50 milliseconds.
I'm not sure how you plan on disarming the alarm, if you'll just pull the plug and reset the arduino or if you've planned on having a button. In the sketch below I added a reset button connected to pin 9 (I used the internal pullup resistor so you don't need an external resistor be aware this inverts the switch logic, when the button is not pressed the pin will read a HIGH state and when the button is pressed the state will become LOW.) The button could be wired to any unused pin, it seems like a more elegant solution to have a pushbutton to disarm the alarm than to reset the arduino. Technically the reset button should be debounced, but in the real world it shouldn't matter if the input bounces because all we're looking for is a momentary high status, if you'd like to debounce the button you will gain a little bit of stability.
Using delay is frowned upon because the microprocessor can't do anything while processing a delay, for example with the sketch below if you press the reset button while the alarm is active nothing is going to happen because the arduino can't read the reset button pin while the delay is being processed, you'll have to hold the button down for as much as 30 seconds depending on when you press the button. Read the blink without delay example http://arduino.cc/en/Tutorial/BlinkWithoutDelay and try to make the alarm cycle without using delay. This will let you disarm the alarm with a single momentary button press instead of having to hold the reset button.
//constants that don't change
const int sensor = 8;
const int MERS = 3;
const int LED = 13;
const int resetButton = 9;
//variables that may change
int sensorState = LOW;
int lastSensorState = LOW;
boolean alarmTrigger = false;
long lastSensorTime = 0;
long startDelay = 4000;
void setup() {
//set pins as input or output
pinMode(sensor,INPUT);
pinMode(MERS,OUTPUT);
pinMode(LED, OUTPUT);
pinMode(resetButton, INPUT_PULLUP);
// initial LED & MERS State
digitalWrite(MERS, LOW);
digitalWrite(LED, LOW);
}
void loop() {
sensorState = digitalRead(sensor); //read sensor state
if (sensorState != lastSensorState) { //detect if sensor state has changed since last iteration of loop
lastSensorTime = millis(); //set last senor time
}
if ((millis() - lastSensorTime) > startDelay) { //subtract last sensor time from current time, compare to start delay
alarmTrigger = true; //set alarm trigger to true
}
int disarm = digitalRead(resetButton); //read reset button
if (disarm == HIGH && alarmTrigger == true){ //if disarm button has not been pressed and alarm has been triggered
digitalWrite(LED,HIGH); //turn on LED
digitalWrite(MERS,HIGH); //turn on MERS
delay(25000); //Delay 25 seconds
digitalWrite(MERS,LOW); //turn off MERS
delay(5000); //delay 5 seconds
}
if (disarm == LOW){ //if disarm button has been pressed
digitalWrite(LED, LOW); //turn off LED
digitalWrite(MERS, LOW); //turn off MERS
alarmTrigger = false; //set alarm trigger to false so alarm won't turn on until beam has been broken again
}
lastSensorState = sensorState; //set last sensor state to current sensor state for next iteration of loop
}
I didn't take the time to comment the code with a lot of detail, hopefully it makes sense.