Hello all, back again with a teeny problem I would love some insight on.
My first post on the forum was about making one button do a latching state and a quick momentary state. With the help with the forum, I got a result that worked pretty well. I later found a problem with trying to incorporate a 5v latching low signal relay in the mix.
//LATCHING & MOMENTARY CODE
// constants won't change. They're used here to set pin numbers:
const int SW = 2; // the number of the pushbutton pin
const int RELAYP = 4; //+ RELAY COIL
const int RELAYN = 7; //- RELAY COIL
const int LED = 13; // the number of the LED pin
const unsigned long Debounce = 20;
const unsigned long LongPress = 500;
unsigned long ButtonStateChangeTime = 0; // Debounce timer
boolean ButtonWasPressed = false;
boolean Effect = false;
void setup() {
//SET PINS:
pinMode(SW, INPUT);
pinMode(LED, OUTPUT);
pinMode(RELAYP, OUTPUT);
pinMode(RELAYN, OUTPUT);
//Set SW pin to HIGH to monitor state.
digitalWrite(SW, HIGH);
//RESET BACK TO EFFECT BYPASS AFTER START UP:
Serial.begin(9600);
Serial.println("Start");//DEBUGGING
digitalWrite(RELAYP, LOW);
digitalWrite(RELAYN, HIGH);
delay(1);
digitalWrite(RELAYN, LOW);
}
void loop(){
unsigned long currentTime = millis();
boolean buttonIsPressed = digitalRead(SW) == LOW; // Active LOW
// Check for button state change debounce.
if (buttonIsPressed != ButtonWasPressed &&
currentTime - ButtonStateChangeTime > Debounce){
// Button state has changed.
ButtonWasPressed = buttonIsPressed;
if (ButtonWasPressed){
Serial.println("Press");
digitalWrite(LED, HIGH);
digitalWrite(RELAYP, HIGH);
delay(1);
digitalWrite(RELAYP, LOW);
}
else{
//Released after 500ms has passed:
if (currentTime - ButtonStateChangeTime >= LongPress){
Serial.println("Momentary");
digitalWrite (LED, LOW);
digitalWrite(RELAYN, HIGH);
delay(1);
digitalWrite(RELAYN, LOW);
}
if (currentTime - ButtonStateChangeTime <= LongPress && Effect == false){
Serial.println("Latch On");
Effect = true;
digitalWrite(LED, HIGH);
digitalWrite(RELAYP, HIGH);
delay(1);
digitalWrite(RELAYP, LOW);
}
else{
if (Effect == true){
Serial.println("Latch off");
Effect = false;
digitalWrite(LED, LOW);
digitalWrite(RELAYN, HIGH);
delay(1);
digitalWrite(RELAYN, LOW);
}
}
}
//Time reset.
ButtonStateChangeTime = currentTime;
}
}//End.
Basically what I'm trying to get around is the fact when the LED is on in the latch mode, if you press the button again, it'll activate the Relay positive pin again and then neutral pin, causing the coil to lag.
I'm mainly trying to use an ATtiny13/85, otherwise I'd use multiple loops which I haven't been able to get to work on the chip.
Relay is driven by the microcontroller and works fine that way.
I have a simulation on Tinkercad to illustrate the operation:
The two multimeters in the sketch show the signal pulse in volts.
How would I go about isolating the momentary part of the code and the latching part?
Thank you!
Edit: Taking a look at the serial monitor, when it’s in latch on mode and the switch is pressed again, it refers to this part of the code:
if (ButtonWasPressed){
Serial.println("Press");
digitalWrite(LED, HIGH);
digitalWrite(RELAYP, HIGH);
delay(1);
digitalWrite(RELAYP, LOW);
}
before going back to:
if (Effect == true){
Serial.println("Latch off");
Effect = false;
digitalWrite(LED, LOW);
digitalWrite(RELAYN, HIGH);
delay(1);
digitalWrite(RELAYN, LOW);
}
I don’t know how to go about avoiding this. Another variable storing the button press?