Need help with turning on/off a light!

General idea: we want to have an if statement to turn on a yellow light one second after turning off a blue light and a speaker.

Our current code looks like this: the problem is mainly in the if statement at the end

#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(ledPin, OUTPUT);
}

void loop() {
  long sensorValue = capSensor.capacitiveSensor(30);
  if ((millis() / 1000) == 0)Serial.println(sensorValue);
  if (millis() - starttime >= looptime) { 
    digitalWrite(blueledPin, LOW); 
    noTone(8); 
    delay(5000); 
    tone(8, 294, 5000); 
    digitalWrite(blueledPin, HIGH); 
    starttime = millis();  
  } else {
    if (sensorValue > threshold) { 
      digitalWrite(blueledPin, LOW); 
      noTone(8); 
      delay(1000); 
      digitalWrite(yellowledPin, HIGH); 
      delay(3000);
    }
  }
}

What is the problem?

from the code you shared it is unclear what is condition to turn off blue light and speaker.

Could you please confirm if this is intended operation of code

  1. turn on Blue light (yellow light off?)
  2. turn on speaker after 'x' seconds
  3. turn OFF Blue light and speaker after 'y' seconds
  4. wait 1s.
  5. Turn ON yellow light.
  6. if 10s passed loop back to 1.

As a general advice:

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.

best regards Stefan

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.

This sounds very similar to your other thread.
still this descriptiion is unprecise

specify: how much time? How many seconds??

blue led was turned on
specify: after how much time shall the blue led be turned off again???

specifiy: is there a difference between "using" the level and "pressing" the lever??
or does using / pressing mean the same?

pressed sounds like analog input is at lowest value
using sounds like analog input is somewhere above lowest value

does the program terminate after switching off the yellow LED after 3 seonds?

or should there happen something else?

As you can see from my questions: posting quick slows things down
taking time to write a precise posting speeds things up

best regards Stefan

Consider this - the simple question first asked in reply #2 has not been answered.

something like this maybe then:
(Compiles, NOT tested!)

#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();
}

void loop() {
  long sensorValue = capSensor.capacitiveSensor(30);

  if ((millis() / 1000) == 0)Serial.println(sensorValue);

  if (millis() - starttime >= looptime) { //after a certain amount of time the blue light and speaker turn on
    tone(8, 294, 5000);
    digitalWrite(blueledPin, HIGH);
    starttime = millis();
  }
  else if (digitalRead(blueledPin) == HIGH) {
    if (sensorValue > threshold) { //turn off blue light and speaker if sensor is past threshold
      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);
    }
    else if (millis() - starttime >= 5000) { //or turn off blue light and speaker after 5 seconds
      digitalWrite(blueledPin, LOW);
      noTone(8);
    }
  }
}

hope that helps...

1 Like

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);
    }
  }
}

best regards Stefan

I would recommend you to use ezOutput library output.pulse(pulseTime, delayTime) function to simplify the time management.

1 Like

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.

thanks

Hi @robodog1,

you are very welcomed. With your answer you show that you can reflect about your work.
In my humble opion this is a very good and important thing.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.