Hi all! I'm completely new to Arduino and I got to create a simple project tied to my studies. I'd like to request some help with the code.
The idea is as follows:
There is one (or multiple) motion detector(s) installed in the ceiling. I am using HC-SR501 sensors.
When the sensor detects a person it sends a HIGH to my Arduino Uno.
The Arduino then switches a relais from NO to NC. NO toggles a line of red LEDs and NC a line of green LEDs. These LEDs are supposed to light up a glass panel to show whether the room is occupied or free.
I am trying to implement a timer so that when the sensor does not receive an input after let's say 60s it automatically turns to LOW and the red LEDs light up again. If however the sensor receives another signal it resets the timer.
As said I'm completely new to coding but I have added a "protoype" version of what I have so far. I am aware of it not working yet but hopefully it provides enough information for you to understand what I have in mind. If I should provide more info let me know.
Maybe you can help me with the code or rework the entire idea. Thanks a lot!
CODE:
const int GreenRed = 4; // GreenRed (LEDs) to Pin4
const int SensorInput = 2; // SensorInput to Pin2
int GRState = LOW; // starting with State to LOW (RED)
int SensorState = LOW; // starting with State to LOW
unsigned long currentMillis; // Millis Timer to count
unsigned long previousMillis = 0; // Reset Millis Timer to 0
const long interval = 60000; // Millis Timer counting up to 1 min
void setup() {
pinMode(GreenRed, OUTPUT); // GreenRed (Pin4) as Output
pinMode(SensorInput, INPUT); // SensorInput (Pin2) as Input
}
void loop() {
currentMillis = millis(); // declare currentMillis as Millis Timer
if (SensorState == HIGH) {
GRState = HIGH; // GRState to HIGH -> GREEN
currentMillis = previousMillis; // set currentMillis to 0
if (currentMillis >= interval) { // When currentMillis reaches 60000ms:
GRState = LOW; // GRState to LOW -> RED
}
}
}
That sensor outputs 3.3v on the DOUT pin. I'm not sure if that's enough to trigger the Uno. If I remember correctly, the lower trigger threshold is 3.5v.
Someone correct me if I've made a mistake.
bool ImOn = false;
unsigned long ImOnMillis = millis();
unsigned long ImOnTime = 6000;
voiding loopies()
{
currentMillis = millis(); // declare currentMillis as Millis Timer
if (SensorState == HIGH && ImOn==false)
{
ImOn=true;
GRState = HIGH; // GRState to HIGH -> GREEN
currentMillis = previousMillis; // set currentMillis to 0
ImOnMillis=millis();
//if (currentMillis >= interval) { // When currentMillis reaches 60000ms:
// GRState = LOW; // GRState to LOW -> RED
//}
}
if ( ((millis() - ImOnMillis) >= ImOnTime) && ImOn )
{
do the LEd is the other color thingies here.
ImOn=false;
}
}
That was my thinking, I wanted to let others provide their experience before I put together something like that.
Has the bonus of being a ground triggered pin, instead of hot triggered.
After digging into it for a bit, I now finished my simplified code using 3 sensors. Seems to be working so far. The settings for the timing can be adjusted on the sensors themselves.
int SensorA = 5;
int SensorB = 6;
int SensorC = 7;
int StatusA = LOW;
int StatusB = LOW;
int StatusC = LOW;
int RGRelay = 2;
void setup() {
pinMode(SensorA, INPUT);
pinMode(SensorB, INPUT);
pinMode(SensorC, INPUT);
pinMode(RGRelay, OUTPUT);
}
void loop() {
StatusA = digitalRead(SensorA);
StatusB = digitalRead(SensorB);
StatusC = digitalRead(SensorC);
if( StatusA || StatusB || StatusC == HIGH) {
digitalWrite(RGRelay, LOW);
}
else {
digitalWrite(RGRelay, HIGH);
}
}