Hey everyone, I'm new here. I just got into programming. I currently have two sketches that I'd like to combine into one, however, I'm having issues implementing the delay function.
I've been able to successfully merge the programs, but it all only works when I remove all delays.
A little background on the mechanism-
There is a motion-activated Laser Diode aimed at a photo resistor. When the light hits the resistor, it triggers a relay. The goal is to have the laser diode activate for about sixty seconds after motion is detected, instead of only a few seconds.
The first Set of code is the Photo Resistor Relay Sketch, the second set is the Motion Activated Laser Sketch, and the third is the combined sketch that I'm having issues with.
Can anyone help or give a suggestion as to what I'm doing wrong?
Thank you!
PHOTORESISTOR_RELAY
int sensorPin = A0;
int sensorValue = 0;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if(sensorValue < 700)
digitalWrite(2,HIGH);
else digitalWrite(2,LOW);
delay(100);
}
MOTION_SENSOR_LASER
int motion = 5;
int motionLed = 7;
void setup() {
pinMode(motion, INPUT);
pinMode(motionLed, OUTPUT);
}
void loop()
{
long sensor = digitalRead(motion);
if(sensor == HIGH){
digitalWrite (motionLed, HIGH);
delay(60000);
}
else
{
digitalWrite (motionLed, LOW);
}
}
COMBINATION (Without Delays)
int sensorPin = A0;
int sensorValue = 0;
int motion = 5;
int motionLed = 7;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
pinMode(motion, INPUT);
pinMode(motionLed, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if(sensorValue < 700)
digitalWrite(2,HIGH);
else digitalWrite(2,LOW);
{long sensor = digitalRead(motion);
if(sensor == HIGH){
digitalWrite (motionLed, HIGH);
}
else
{
digitalWrite (motionLed, LOW);
}
}
}