I am trying to write a program to do the following functions....
- when beam between two sensors is broken for 4 secs, I want to:
a) turn on relay to turn on light that remains on until reset
b) turn on relay to turn on transmitter for 25 seconds then turn off for 5 seconds then cycle until reset with same process
this is what i have. this is a first time project and I am still very green! any help and suggestions gladly accepted!
//Pin Numbers will not change:
const int sensor = 8;
const int MERS = 3;
const int LED = 13;
// Variables that will change:
int sensorState = LOW;
int lastsensorState = HIGH;
int MERSState = LOW;
int LEDState = LOW;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(sensor,INPUT);
pinMode(MERS,OUTPUT);
pinMode(LED, OUTPUT);
// initial LED & MERS State
digitalWrite(MERS, MERSState);
digitalWrite(LED, LEDState);
}
void loop() {
int reading = digitalRead(sensor);
if (reading != lastsensorState) {
lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the state has changed:
if (reading != sensorState) {
sensorState = reading;
// only toggle the LED if the new button state is HIGH
if (sensorState == HIGH) {
MERSState = !MERSState;
if (sensorState == HIGH) {
LEDState = !LEDState;
}
}
}
// set MERS & LED
digitalWrite(LED,LEDState);
digitalWrite(MERS,MERSState);
delay(25000);
digitalWrite(MERS,MERSState);
delay(5000);}
}
}