as you can see from the question of @sherzaad:
a very precise description of the wanted functionality i srequired.
You are working on an informatic project. And what is most needed in an informatic project
is sufficient detailed and sufficient precise information.
Mixing non-blocking timing based on millis() with blocking timing with delay() is a very bad idea.
If your functionality is strictly sequential and no functions are overlapping using delay() is sufficient.
If your functionality has overlapping parts that can vary in time like a user pressing a button at different times
example:
switch on yellow LED
if a button is pressed switch on speaker
switch off speaker 5 seconds after button-press
switch off LED after 20 seconds
switch on blue led 12 seconds after button-press
this requires non-blocking timing.
default is everything is off.
1)after a certain amount of time the blue light and speaker turn on
2)turn off blue light and speaker after 5 seconds (roughly) or
2)turn off blue light and speaker (before the 5 seconds are up) if sensor is past threshold (we use a lever here)
and if lever is pressed, wait 1 second.
then turn on yellow light for 3 seconds.
if the code does all what the TO wants she/he will take the code but I doubt that she/he understands how code works.
If it does not what the TO wants she/he will still be lost due to the complex nature of the code and the too less comments that would explain the code in all details
#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);
const byte threshold = 10;
const int blueledPin = 12;
const int yellowledPin = 6;
unsigned long starttime = 0;
const unsigned long looptime = 10000;
void setup() {
Serial.begin(9600);
pinMode(blueledPin, OUTPUT);
pinMode(yellowledPin, OUTPUT);
digitalWrite(blueledPin, LOW);
digitalWrite(yellowledPin, LOW);
noTone(8);
starttime = millis(); // initialise variable starttime with actual vlaue of function millis()
}
void loop() {
long sensorValue = capSensor.capacitiveSensor(30);
if ((millis() / 1000) == 0)Serial.println(sensorValue);
if (millis() - starttime >= looptime) { //check how much milliseconds have passed by since starttime = millis();
// if more millisecods HAVE passed by as value stored in variable looptime
tone(8, 294, 5000);
digitalWrite(blueledPin, HIGH);
starttime = millis(); // update starttime with actual milliseconds
}
// if NOT more millisecods HAVE passed by as value stored in variable looptime
else if (digitalRead(blueledPin) == HIGH) { // check if blue LED is ON
// if blue led IS ON
if (sensorValue > threshold) { // check if sensorValue is above threshold
//if sensorValue IS above threshold turn off blue light and speaker
digitalWrite(blueledPin, LOW);
noTone(8);
delay(1000); // wait 1 second
digitalWrite(yellowledPin, HIGH); //turn on yellow light for 3 seconds.
delay(3000);
digitalWrite(yellowledPin, LOW);
}
// if sensorValue is BELOW threshold
else if (millis() - starttime >= 5000) { //check if more than 5000 milliseconds have passed by
// if more than 5000 milliseconds HAVE passed by
digitalWrite(blueledPin, LOW);
noTone(8);
}
}
}
thank you for this reply! It seems my thought process was lacking these precise questions as well and your questions helped me think through how the code should probably look.
It seems like I was also confused with my way of approaching the project and couldn't think through my confusion, but after reading through your questions, I'm going to have another go at my code and set-up.
But to answer your questions.
the blue light and speaker turn on after a minute and both should turn off after 5 seconds. the light currently, even without line describing how long it should be on, turns off roughly around the same time the speaker turns off which is not an issue I'm too worried about.
what I meant by "use" is that instead of a touch/capacitive sensor or a button, we use a lever. "pressing" the lever is just means the sensor value surpasses the threshold value, which we define as a small value so any movement of the lever sets off the next tasks.
After the yellow light is on for 3 seconds we want it to turn off and restart the whole process again of waiting a minute, turn on the blue light and the speaker for 5seconds, etc. etc.
Sorry, my ideas were not effectively communicated in my post. I was unsure of how to explain it properly, but your questions helped a lot in sharing that information and clearing up the way I should see this issue.