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