Hello from Germany dear forum users,
I just created an account out of despair, after reading the rules at How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum
I literally searched almost 3.5 hours for a solution, no library and no website seemed to have an answer for a simple thing.
I always had my problems with the millis() function, but everytime I thought I understood it to 100%, I was thrown back when I started a new project with another use of millis.
Long story short: What I wanted to build is a simple light detection for my house and garage.
So 2 lamps shall be installed inside of 3 garages. A third lamp will be attached outside to light up when I bring out the garbage.
I'll be using one ultrasonic sensor for both of the garages and another one outside.
Whenever I come into the garage from one of the doors, the ultrasonic sensor detects something and should light up the light for a certain amount of time (called "intervallgarage" in my code). Same thing should happen when the sensor outside detects me and turns the light on and waits for "intervallmuell" ("Müll" is "garbage" in German).
The Code:
const int triggergarage = 2;
const int echogarage = 3;
const int lampe2 = 12;
const int lampe3 = 11;
long dauergarage = 0;
long entfernunggarage = 0;
const int triggermuell = 4;
const int echomuell = 5;
const int lampe1 = 13;
long dauermuell = 0;
long entfernungmuell = 0;
unsigned long previousMillismuell = 0;
unsigned long previousMillisgarage = 0;
const long intervallmuell = 2000;
const long intervallgarage = 120000;
void setup() {
Serial.begin(9600);
pinMode(lampe1, OUTPUT);
pinMode(lampe2, OUTPUT);
pinMode(lampe3, OUTPUT);
pinMode(triggermuell, OUTPUT);
pinMode(echomuell, INPUT);
pinMode(triggergarage, OUTPUT);
pinMode(echogarage, INPUT);
}
void loop() {
muell();
}
void muell(){
digitalWrite(triggermuell, LOW);
delay(5);
digitalWrite(triggermuell, HIGH);
delay(10);
digitalWrite(triggermuell, LOW);
dauermuell = pulseIn(echomuell, HIGH);
entfernungmuell = (dauermuell/2) * 0.03432;
if (entfernungmuell >= 2 && entfernungmuell <= 70)
{
unsigned long currentMillismuell = millis();
digitalWrite(lampe1, HIGH);
Serial.println(currentMillismuell);
}
if (currentMillismuell - previousMillismuell >= intervallmuell){
digitalWrite(lampe1, LOW);
previousMillismuell = currentMillismuell;
}
}
void garage(){
digitalWrite(triggergarage, LOW);
delay(5);
digitalWrite(triggergarage, HIGH);
delay(10);
digitalWrite(triggergarage, LOW);
dauergarage = pulseIn(echogarage, HIGH);
entfernunggarage = (dauergarage/2) * 0.03432;
if (entfernunggarage >= 2 && entfernunggarage <= 300)
{}
}
You can ignore everything that has to do with garage yet, it's just a duplication of the (yet not working) code above for the garbagelight. What happens if I use my code is following:
1.) Ultrasonic Detector sends sound - nothing is within the range of 2-70cm - LED stays off - so far so good.
2.) Ultrasonic Detector sends sound- I'm inside the measuring range - LED goes on - also okay.
But what I noticed, is that the counter when the LED should go on is running permanently, that means when I trigger the sensor at the currentTime of 1000ms, it just lights up the LED for the rest of the intervall, not the full intervall. That also means in a very unlucky case, the lamp is on for just a fracture of a second. The lamp should stay on for the given time (which is unfortunately called interval, because I started the sketch inside of the blink without delay example).
It's not about using the wrong or right sensors for this project, just about the function, it could've also been a push button or whatever else gives me an input.
Thank you very very much ![]()