Hi all, First time posting here, so apolgies if i'm in the wrong place. I've got a small bit of code that i'm looking for help or pointers in. First off, this is my first project using an arduino so i'm a complete novice!
I'm using a 'UNO' to program an attiny85 chip. The chip will g into a pcb and will be used to trigger a small lacthing relay with the use of a SPST momentary switch. Everything works fine, but was wondering if there was a way to implement a code aswell as the code already there, so the relay would latch whilst the footswitch is being held down after a couple of seconds of holding or so, then release when let go.
Here's the code:
const int led = 2;
const int button = 4;
const int setRelay = 1;
const int clearRelay = 0;
const int muteOutput = 3;
int led_state = LOW;
// Button states and debounce
int buttonState = 0;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
pinMode(setRelay, OUTPUT);
pinMode(clearRelay, OUTPUT);
pinMode(muteOutput, OUTPUT);
digitalWrite(muteOutput, LOW);
relayOff();
// Blink LED to show code running
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
}
void loop() {
int reading = digitalRead(button);
if (reading != lastButtonState)
lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > 50) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
led_state = !led_state;
if (led_state == LOW) { relayOff(); }
if (led_state == HIGH) { relayOn(); }
}
}
}
lastButtonState = reading;
digitalWrite(led, led_state);
}
void relayOn() {
digitalWrite(muteOutput, HIGH);
digitalWrite(setRelay, HIGH);
delay(50);
digitalWrite(setRelay, LOW);
digitalWrite(muteOutput, LOW);
}
void relayOff() {
digitalWrite(muteOutput, HIGH);
digitalWrite(clearRelay, HIGH);
delay(50);
digitalWrite(clearRelay, LOW);
digitalWrite(muteOutput, LOW);
}