Help fast. simple arduino

I already have a code, that i want something added to.

Its a rather simple setup but cant get the last parameter to work properly by myself.

The setup uses a debounced button to ON/OFF a LED and a piezo buzzer. Beside that, if the LED and Buzzer is HIGH it'll go low after 10 sek and after that it has to turn on a a relay for a couple seconds.

I've made the debounce and delay function myself using millis. So all i need is help with turning on the relay for a 2 seconds after LED and Buzzer went low.

To someone experienced i suppose its a quickly job.

Either PM me or let me know here, how much it'll cost.

Here is my code.

const int piezoPin = 8;
const int buttonPin = 1;   
const int ledPin = 13;     

boolean outputTone = false; 
boolean ledState = LOW;         
boolean buttonState =  LOW;           
boolean lastButtonState = HIGH;  

unsigned long interval = 100;
unsigned long intervalDelay = 200; 
unsigned long previousMillis = 0;
unsigned long ledOnDelay = 10000;
unsigned long trackLedOnTime = 0; 
unsigned long lastDebounceTime = 0; 
unsigned long debounceDelay = 50;  
 

void setup() {
  
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
      lastDebounceTime = millis();
    }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
        buttonState = reading;
      if (buttonState == HIGH) {
          ledState = !ledState;
        if (ledState){
            trackLedOnTime = millis(); 
            previousMillis = millis();      
        }
      }
    }
  }
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;

  if ((millis() - trackLedOnTime) > ledOnDelay) {
      ledState =  LOW;
  }
  if(ledState == HIGH){
    if(outputTone){
      if ((millis() - previousMillis) >= interval) {
          previousMillis = millis();
          noTone(8);
          outputTone = false;
       }
    }

   else {
     if ((millis() - previousMillis) >= intervalDelay){
         previousMillis = millis();
         tone(8,400);
         outputTone = true;
       }
     }
   }
 else {
  if(ledState == LOW){
    noTone(8);
    }
  }
}

Wew... some seriously deeply nested if statements.

Anyway, the same way you trigger the LEDs upon button press, you can have the LEDs trigger the relay upon LEDs switching off.

Yeah.. tried that, But it somehow doesn't Work atleast when i do it..I just need finished code. Can you help me?

Hi,

You still need help with this?

I just tried to read the code and it's a bit weird what's going on. Maybe I'm too tired but way too many bools that switch state a few times just to result in an LED switching on. I guess.

Can you please tell me in detail what it does - and where the extension comes in?

Is the button active LOW or active HIGH?

It seems what this does:

  • upon button press:
  • LED on for 10 seconds.
  • buzzer off for 100 ms, then on for 200 ms, repeat this for as long as the LED is on.

And you want to add:

  • when LED switches off, turn on relay for 2 seconds.

Does a second button press during this sequence do anything? It seems not - which would mean the whole debounce part is unnecessary.