Hello all, I am a newbie this is only my 3rd sketch. I try doing the research but I have a TBI and am just not as quick on the uptake as I used to be. Basically I am controlling a night light, a fan with external thermostat and a relay to override the thermostat.
If it is low light and the LASER beam is broken I want the light to come on for 10 mins.
If it is above 80° (external thermostat) and the Laser sensor is broken it will turn on a fan.
If it is below 80° and a button is pressed it will fire a relay that will override the thermostat for 10 mins.
This sketch works, but I would like to do away with the Mosfets, so what I want is something like this:
if the LDR value is met AND the LASER beam(input1) is broken turn on the light
if the above 80° (input2) AND LASER beam(input1) is broken turn on the fan
if the below 80°(input2) AND LASER beam(input1) is broken AND the thermostat override(input3) button is pushed turn on fan override
I can’t find how to use a single input to fire multiple outputs or how to require multiple inputs to fire 1 output. Can anyone help with proper coding? THANKS FOR YOUR HELP, Mike.
Please keep in mind this is a retired electrician's drawing, I have very little electronics experience. But here is a quick drawing of what I hope to end up with. Thanks, Mike.
I'm sorry The drawing is what I want after I get rid of the mosfets.
The sketch has the LDR staging lasermosfet1gate now if the laser beam is broken the mosfet fires and lasermosfet1drain goes high and turns on light relay.
Now because I don't know how to use inputs for more than 1 thing I have the laser tube fire another mosfet to trigger the fan relay.
and then of course the button to bypass the thermostat
I hope this helps, I really need the help if need more let me know
BTW All inputs should be standard inputs and not input_pullup all inputs cause a high state, I will use 100K pulldown resistors
A mosfet is a type of transistor which is kinda confussing but if you are saying the input goes high when the beam is broken then we can work with that.
this program should do what you want code wise. the question is can you convert the wiring to match the code.
int LDR = A0; //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0; //that’s a variable to store LDR values
int light_sensitivity = 10; //This is the approx value of light surrounding your LDR
const byte lightRelay = 9; //cuts power to light when low
const byte fanRelay = 11; //cuts power to motor when low
const byte tripSensed = 5; //laser sensor input
const byte thermostat = 6; //need something to tell me temp is over 80
const byte fanOverRide = 7; //button to stop fan
//thermostat switch is pressumed
byte flagHold = 0;//used as a flag
unsigned long previousMillis = 0;//used by timer
void setup() {
pinMode(lightRelay, OUTPUT);
pinMode(fanRelay, OUTPUT);
pinMode(tripSensed, INPUT);
pinMode(thermostat, INPUT);
pinMode(fanOverRide, INPUT);
//100k resistor pull down to be added on inputs
}
void loop() {
LDRValue = analogRead(LDR);
if ((tripSensed == HIGH) && (flagHold == 0)) {//laser beam has been broken
previousMillis = millis();//set time that laser was tripped
flagHold = 1; //used to stop the beam being read again
//check to see if light is required
if (LDRValue > light_sensitivity)
{ //yes light required
digitalWrite(lightRelay, HIGH);//turn on the relay
}
//check to see if fan is required
else if (thermostat == HIGH) {
//yes fan required
digitalWrite(fanRelay, HIGH);//turn on the relay
}
//nothing was required so remove flag
else {
flagHold = 0;
}
}
if (fanOverRide == HIGH) {//push button to shut down fan
digitalWrite(fanRelay, LOW);//turn relay off
//nothing will turn it back on until timer is done
}
if (flagHold == 1)
{ //fan or light is on
if (millis() - previousMillis >= 600000L) {
//check time only run this part of code if 10mins has passed
digitalWrite(fanRelay, LOW);//turn relay off
digitalWrite(lightRelay, LOW);//turn relay off
flagHold = 0;//reset the flag used to block the sensor
}
}
}
Wiring is no problem was industrial electrician for 35 years even know a bit about electronics its the coding that confuses me. thank you so much, Mike. How do I add mills() for 5 min debounce of LDR?, Thanks again, Mike
memaier:
Wiring is no problem was industrial electrician for 35 years even know a bit about electronics its the coding that confuses me. thank you so much, Mike. How do I add mills() for 5 min debounce of LDR?, Thanks again, Mike
How do I add mills() for 5 min debounce of LDR?, which way do you want the debounce to work.
After light has been on 5 mins check to see if its still required or check 5 minutes before the light is required if the light will be required
The code I wrote for you does a crude debounce. If the light was turned on then it will stay on for 10 minutes. It will not get rechecked for 10 minutes. (the reason I wrote it that way was due to having no information about the location of the ldr. If I turn on a light in a workshop where the ldr is installed then the light from the bulb will cause the ldr to read high which in turn will turn the light back off.)
I know that you are thinking in relay logic but arduino is based in C++ it can be coded to do the work of thousands of relays.
We need to teach you the basics of arduino so I would suggest you look at the code posted and ask questions about that code until we can get you to understand how every line of code works and how electrical items are connected to the board.
This is easy to code from
if the LDR value is met AND the LASER beam(input1) is broken turn on the light
if the above 80° (input2) AND LASER beam(input1) is broken turn on the fan
if the below 80°(input2) AND LASER beam(input1) is broken AND the thermostat override(input3) button is pushed turn on fan override
the diagram I couldn't even guess what code was required
write the debounce into the text using if and it will be easier to understand
What I should have said is : If the LDR value has been met for 5mins (without bouncing true false) and the laser beam is broken turn on light. This keeps the light from coming on too early when light levels cause the LDR value to bounce between high and low for milliseconds to several seconds at a time.
This code es completely foreign to me, so perhaps I am doing something wrong but....if I connect pin 5 to 5v+ and place the LDR in the dark shouldn't pin 9 go high? OR pin 5 high and pin 6 high shouldn't pin 11 go high? nothing is happening am I reading it wrong?
memaier:
What I should have said is : If the LDR value has been met for 5mins (without bouncing true false) and the laser beam is broken turn on light. This keeps the light from coming on too early when light levels cause the LDR value to bounce between high and low for milliseconds to several seconds at a time.
This code es completely foreign to me, so perhaps I am doing something wrong but....if I connect pin 5 to 5v+ and place the LDR in the dark shouldn't pin 9 go high? OR pin 5 high and pin 6 high shouldn't pin 11 go high? nothing is happening am I reading it wrong?
pin 9 should go high so we need to check your wiring to see whats happening. Im not sure what type of ldr you have especially since we are reading it on a analog signal so any specs would be nice.
have you used Serial.print before?
if so just connect the ldr then add the code below and load it.
this will allow us to see what the arduino is reading on the analog pin.
if you haven't used serial print before just google it as theres lots of good examples.
ive changed the program to better suit what I think you are after
if a laser beam is broken and its been dusk outside for the last 5 mins
turn on light
if the temp is over 80 turn on fan
if the laser beam is not broken for 10 mins turn off fan and light
if the laser beam is broken reset timer lights stay on 10 mins
if a button is pushed then turn off fan
this way the laser resets the time and the light and fan are constantly checked
int LDR = A0; //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0; //that’s a variable to store LDR values
int light_sensitivity = 10; //This is the approx value of light surrounding your LDR
const byte lightRelay = 9; //cuts power to light when low
const byte fanRelay = 11; //cuts power to motor when low
const byte tripSensed = 5; //laser sensor input
const byte thermostat = 6; //need something to tell me temp is over 80
const byte fanOverRide = 7; //button to stop fan
//thermostat switch is pressumed
byte flagFan = 0;
byte timerTiming = 0;
byte lightAvilable = 0;
byte lightDebounceOff = 0;
unsigned long previousMillis = 0;//used by laser timer
unsigned long previousMillis2 = 0;//used by light on debounce
unsigned long previousMillis3 = 0;//used by light off debounce
void setup() {
pinMode(lightRelay, OUTPUT);
pinMode(fanRelay, OUTPUT);
pinMode(tripSensed, INPUT);
pinMode(thermostat, INPUT);
pinMode(fanOverRide, INPUT);
//100k resistor pull down to be added on inputs
}
void loop() {
LDRValue = analogRead(LDR);
if (LDRValue < light_sensitivity) {
previousMillis2 = millis();//set timer to zero if it senses light
}
if (millis() - previousMillis2 >= 300000L) {//allow timer when dark
lightAvilable = 1;
}
if ((lightAvilable == 1) && (LDRValue < light_sensitivity)) {
//if its senses light set debounce for 3 mins
lightDebounceOff = 1;
}
if (lightDebounceOff != 1) { //if debounce not set reset timer
previousMillis3 = millis();
}
if (millis() - previousMillis3 >= 180000L) {//check its been light 3 mins
lightAvilable = 0;
lightDebounceOff = 0;//tell light its no longer required
}
if (tripSensed == HIGH) {//laser beam has been broken
previousMillis = millis();//set time that laser was tripped
timerTiming = 1;
}
//beam being read again just resets timer
//check to see if light is required
if ((lightAvilable == 1) && (timerTiming == 1))
{ //yes light required
digitalWrite(lightRelay, HIGH);//turn on the relay
} else {//light not required
digitalWrite(lightRelay, LOW);//turn off relay
}
//check to see if fan is required
if ((thermostat == HIGH) && (flagFan == 1) && (timerTiming == 1)) {
//yes fan required
digitalWrite(fanRelay, HIGH);//turn on the relay
}
if (fanOverRide == HIGH) {//push button to shut down fan
digitalWrite(fanRelay, LOW);//turn relay off
flagFan = 1;
//nothing will turn it back on until timer is done
}
if (timerTiming == 1)
{ //fan or light is on
if (millis() - previousMillis >= 600000L) {
//check time only run this part of code if 10mins has passed
digitalWrite(fanRelay, LOW);//turn relay off
digitalWrite(lightRelay, LOW);//turn relay off
timerTiming = 0;//reset the flag used to block the sensor
flagFan = 0;
}
}
}
I think it correct but ive been in a plc cabinet for the last 10hrs upgrading to a newer processor so everything is a bit fuzzy at the moment. If its wrong I will look at it tomorrow.
OK we have a problem I even re copied your sketch into new sketch and started over. With the Serial.println all I get back is a single 0. When I download my original sketch add the Serial.println I get a continuing string of numbers
don't see a problem in the code. read note after loop I wrote about testing. Everything looks correct in the code or run a real test and wait for 5 mins
int LDR = A0; //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0; //that’s a variable to store LDR values
int light_sensitivity = 10; //This is the approx value of light surrounding your LDR
const byte lightRelay = 9; //cuts power to light when low
const byte fanRelay = 11; //cuts power to motor when low
const byte tripSensed = 5; //laser sensor input
const byte thermostat = 6; //need something to tell me temp is over 80
const byte fanOverRide = 7; //button to stop fan
//thermostat switch is pressumed
byte flagFan = 0;
byte timerTiming = 0;
byte lightAvilable = 0;
byte lightDebounceOff = 0;
unsigned long previousMillis = 0;//used by laser timer
unsigned long previousMillis2 = 0;//used by light on debounce
unsigned long previousMillis3 = 0;//used by light off debounce
void setup() {
Serial.begin(9600);
pinMode(lightRelay, OUTPUT);
pinMode(fanRelay, OUTPUT);
pinMode(tripSensed, INPUT);
pinMode(thermostat, INPUT);
pinMode(fanOverRide, INPUT);
//100k resistor pull down to be added on inputs
}
void loop() {
// these little lines // means ignore
//add them to the line //LDRValue = analogRead(LDR);
//and remove them from the next line //LDRValue=50; to test
//few lines down see the 3 second test line comment out line above
//then uncomment that line
LDRValue = analogRead(LDR);// comment out to test
//LDRValue=50; //test code
if (LDRValue < light_sensitivity) {
previousMillis2 = millis();//set timer to zero if it senses light
}
if (millis() - previousMillis2 >= 300000L) {//allow timer when dark. comment out to test
//if (millis() - previousMillis2 >= 300L) {//3 second test line
lightAvilable = 1;
}
if ((lightAvilable == 1) && (LDRValue < light_sensitivity)) {
//if its senses light set debounce for 3 mins
lightDebounceOff = 1;
}
if (lightDebounceOff != 1) { //if debounce not set reset timer
previousMillis3 = millis();
}
if (millis() - previousMillis3 >= 180000L) {//check its been light 3 mins
lightAvilable = 0;
lightDebounceOff = 0;//tell light its no longer required
}
if (tripSensed == HIGH) {//laser beam has been broken
previousMillis = millis();//set time that laser was tripped
timerTiming = 1;
}
//beam being read again just resets timer
Serial.print("light sensor reads = ");
Serial.print(LDRValue);
Serial.print (" light_sensitivity = ");
Serial.print(light_sensitivity);
Serial.print (" 5min check ");//3 seconds if comments are set to test
Serial.println (lightAvilable);
//check to see if light is required
if ((lightAvilable == 1) && (timerTiming == 1))
{ //yes light required
digitalWrite(lightRelay, HIGH);//turn on the relay
} else {//light not required
digitalWrite(lightRelay, LOW);//turn off relay
}
//check to see if fan is required
if ((thermostat == HIGH) && (flagFan == 1) && (timerTiming == 1)) {
//yes fan required
digitalWrite(fanRelay, HIGH);//turn on the relay
}
if (fanOverRide == HIGH) {//push button to shut down fan
digitalWrite(fanRelay, LOW);//turn relay off
flagFan = 1;
//nothing will turn it back on until timer is done
}
if (timerTiming == 1)
{ //fan or light is on
if (millis() - previousMillis >= 600000L) {
//check time only run this part of code if 10mins has passed
digitalWrite(fanRelay, LOW);//turn relay off
digitalWrite(lightRelay, LOW);//turn relay off
timerTiming = 0;//reset the flag used to block the sensor
flagFan = 0;
}
}
}