One Button Two Functions bug fix

You didn't post a complete sketch.

sorry. edited

did it print, "Start"? Wait, did it even compile?

yes it did

if (ButtonWasPressed) {

void button();

}

did you mean

if (ButtonWasPressed) {
button();
}

Please get rid of the ugly blank lines and use ctrl-T to auto format your code.

Sorry, editing in tinkercad and they dont have an autoformat function.

ah yeah that's it, now its working. I'll work with this new format and see what happens.

You can download and use the IDE, you don't actually have to upload to a board to use it. Tinkertoy will be perfectly happy if you delete your blank lines, at least.

alright back again, implementing multiple loops.

still having trouble with the flow. cant wrap my brain around it. still hitting relay pin p before going back to relay pin n.
tried to also build a timer for the relay pins so im not using delay but cant get that to work either.

//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 relaytime = 1000; //for testing purposes, this is a second for serial monitor results.
const unsigned long LongPress = 500;

//variables that will change
unsigned long currentTime = millis();
unsigned long currentRelay = millis();
unsigned long ButtonStateChangeTime = 0; // Debounce timer
unsigned long relaystate = 0; // relay timer
bool ButtonWasPressed = false;
bool effect;

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(5);
  digitalWrite(RELAYN, LOW);
}//End setup.

void relayp() {

  digitalWrite(RELAYP, HIGH);
  //Serial.println("RELAYPon");
  delay(5);
  //if (currentRelay - relaystate > relaytime) {
  digitalWrite(RELAYP, LOW);
  //Serial.println("RELAYPoff");
  //}
  //currentRelay = relaystate;
}//END

void relayn() {

  digitalWrite(RELAYN, HIGH);
  //Serial.println("RELAYNon");
  delay(5);
  //if (currentRelay - relaystate > relaytime) {
  digitalWrite(RELAYN, LOW);
  //Serial.println("RELAYNoff");
  //}
  currentRelay = relaystate;
}//END

void buttonstate() {

  bool buttonIsPressed = digitalRead(SW) == LOW; // Active LOW
  // Check for button state change debounce.
  if (buttonIsPressed != ButtonWasPressed &&
      currentTime  -  ButtonStateChangeTime > Debounce) {
    // Button state has changed.
    ButtonWasPressed = buttonIsPressed;
    //Serial.println("debounce");
    if (ButtonWasPressed) {
      Serial.println("press");
      effectON();
    } else {
      if (currentTime - ButtonStateChangeTime > LongPress) {
        Serial.println("release");
        effectOFF();
      }
      if (currentTime - ButtonStateChangeTime < LongPress && effect == false) {
        effectON();
        effect = true;
      } else {
        if (effect == true);
        effectOFF();
      }
    }
    ButtonStateChangeTime = currentTime;
  }
}//end debounce

void effectON() {
  digitalWrite(LED, HIGH);
  relayp();
  Serial.println("on");
}//end

void effectOFF() {
  digitalWrite(LED, LOW);
  relayn();
  Serial.println("off");
}//end

void loop() {

  currentTime = millis();

  buttonstate();

}//end

anyone help me map this out so i can close this out? thank you!

See reply #16. It's your only hope. Even if you try it and fail, you'll find people will help because it will become an approachable problem. As I explained, there is no obstacle to doing that with the ATTiny.

First, leave out all the switch code, and just make some functions that turn the relay on and off. Make a sketch with those, that just toggles the relay every few seconds. Then add the switch logic.

i already get the logic on how to switch on and off the relay, i just need help with the order since i can’t get around the fact that when the button is pressed again after a latched press, it writes relayp high again when it should only be relayn. maybe another variable that indicates the on position (if (relayp/led high…))?

right now the flow is: press, wait (debounce), if passed the debounce, activate LED and relayp, depending on how long the button was held, if held for more than 500ms, write relayn high and led low, or less than 500ms, latch led and relayp on and if pressed again, turn on relayn and led low. the problem is getting around that second press. how would i word a state that would get around the first initial part of the code? like two reading the switch points?